Run local AI models with Ollama (and use them from your code)
Install Ollama, pull an open model, chat with it, call it over the REST and OpenAI-compatible APIs, and customize it with a Modelfile. Commands verified against the official Ollama docs.
Who it's for: Builders who want private, offline or zero-marginal-cost inference, and curious power users who want an AI that never sends data to the cloud.
Time: 30 to 45 minutes (plus model download time) · Cost: Free and open source; runs on your own hardware
Tools: Ollama, Inkling 2 Mini, GGUF Radar, LiteParse with OCRia-nano-1, Hermes Agent, OpenRouter, GroqCloud, Hugging Face, OpenAI Agents SDK
Prerequisites:
- macOS, Windows or Linux computer, ideally with 16 GB+ RAM or a modern GPU
- Several GB of free disk space per model
- Python 3 (optional, for the code steps)
Cloud models are brilliant, but every prompt leaves your machine and every token costs money. Ollama runs open models locally with one command, then exposes them through an API your apps (and agents) can call. This playbook follows the Ollama README and docs.ollama.com.
Step 1: Pick the right model size for your machine
Model size (measured in billions of parameters, “B”) drives quality, speed and memory use. Rough guidance that holds across hardware:
- 8 GB RAM laptops: stick to small models (a few billion parameters). Fine for summaries and classification.
- 16 to 32 GB RAM or Apple Silicon with unified memory: mid-size models become comfortable.
- Dedicated GPUs with lots of VRAM: larger models and longer contexts.
Browse ollama.com/library; each model page lists available sizes (tags). Start small. You can always pull a bigger tag later, and a fast small model you actually use beats a slow big one you don’t.
Update, Nov 21, 2026: a stronger option for 32GB machines. Thinking Machines released Inkling 2 Mini on Nov 20 under Apache 2.0. The checkpoint is about 20GB, and its model card lists Apple silicon Macs with 32GB or more memory, or GPUs with 32GB+ VRAM. In Ollama it’s
ollama run inkling-2-mini. The lab says it leads open models under 100B parameters on coding tests (its own numbers) and that it recalls fewer facts than bigger models, so feed it your documents rather than asking it to remember things. Ollama’s page also showsollama launch hermes --model inkling-2-mini, which pairs it with the open-source Hermes Agent so the model behind your agent runs on your own machine. We haven’t run it ourselves. On a 16GB laptop, stay with a smaller model.
Update, Nov 28, 2026: get models from the maker, not a look-alike. Researchers at Malspark scanned every public model on Hugging Face on Nov 26 and found five malicious ones, downloaded 2,428 times. One was a fake “uncensored” Inkling 2 Mini. It hid a Windows trojan after the model weights and was set up to open every reply with a PowerShell line for you to paste. The model file can’t start the malware on its own. You running that line, or an agent with shell access following hidden instructions, is what does it. Four habits cover this:
- Pull from Ollama’s library (
ollama run inkling-2-mini) or follow the maker’s own link. If you useollama run hf.co/owner/repo, check that the owner is the company that made the model.- Skip re-uploads, “uncensored” copies and repos named after models that haven’t been announced. The same uploader parked empty repos named for expected releases.
- Never paste a command a model gives you without reading it first, and keep command approvals on in any agent.
- Scan anything from a third party with the free GGUF Radar before you load it:
gguf-radar scan owner/model. Its own docs say a clean result isn’t a guarantee, so habit 1 still comes first.
Step 2: Install Ollama
From the official README:
# macOS and Linux
curl -fsSL https://ollama.com/install.sh | sh
# Windows (PowerShell)
irm https://ollama.com/install.ps1 | iex
Manual downloads (a .dmg for Mac, an installer .exe for Windows) and an official Docker image, ollama/ollama, are also available. As with any piped install script, read it first if you’re cautious.
Step 3: Run and chat with a model
Running ollama on its own opens an interactive menu that can run a model or connect Ollama to coding tools. To go straight to a chat, the README’s example is:
ollama run gemma4
The first run downloads the model, then drops you into a chat prompt. Try a real task: paste a messy meeting transcript and ask for decisions and action items. Type /bye to exit. Useful commands: ollama list (installed models), ollama pull <model> (download without chatting), and ollama rm <model> (free disk space).
Step 4: Call the local REST API
While Ollama is running it serves an API on localhost:11434:
curl http://localhost:11434/api/chat -d '{
"model": "gemma4",
"messages": [{ "role": "user", "content": "Why is the sky blue?" }],
"stream": false
}'
"stream": false returns one JSON object instead of a token stream, which is easier for scripts. There are official libraries too: pip install ollama for Python and npm i ollama for JavaScript:
from ollama import chat
response = chat(model="gemma4", messages=[{"role": "user", "content": "Summarize: ..."}])
print(response.message.content)
Step 5: Use the OpenAI-compatible endpoint
This is the trick that makes Ollama useful for builders: it supports a subset of the OpenAI API, so most code written for OpenAI can switch to local models by changing two lines:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1/",
api_key="ollama", # required by the client, ignored by Ollama
)
reply = client.chat.completions.create(
model="gemma4",
messages=[{"role": "user", "content": "Say this is a test"}],
)
print(reply.choices[0].message.content)
That means frameworks that accept a custom OpenAI base URL can often run against your laptop for development, then against a hosted provider (OpenRouter, Groq or OpenAI) in production. It’s a “subset”, so test features like tool calling and structured output with your chosen model before relying on them.
Step 6: Customize behavior with a Modelfile
A Modelfile is a small blueprint for a customized model. FROM (required) sets the base; PARAMETER tunes runtime settings; SYSTEM sets a system message. Example for a house-style summarizer:
FROM gemma4
PARAMETER temperature 0.2
SYSTEM You summarize internal documents into 5 bullet points, then list action items with owners. Never invent names.
Build and run it:
ollama create summarizer -f Modelfile
ollama run summarizer
Now your team can call summarizer by name from any script, with the instructions baked in.
Step 7: Tune context length and networking
Two settings trip up most people:
- Context window. Ollama’s FAQ states the default is 4,096 tokens, roughly a few pages. Long documents get silently truncated. Raise it globally with
OLLAMA_CONTEXT_LENGTH=8192 ollama serve, inside a chat with/set parameter num_ctx 8192, or per API request with"options": {"num_ctx": 8192}. Bigger contexts use more memory. - Network access. Ollama binds to
127.0.0.1:11434, so only your machine can reach it. To serve other devices, setOLLAMA_HOST=0.0.0.0:11434(the FAQ shows how per OS). Only do this on a trusted network or behind a firewall or reverse proxy with authentication, since the API has no login of its own.
Step 8: Decide what stays local
A practical split many teams use:
- Local: classifying and tagging, summarizing internal docs, redacting PII before anything goes to the cloud, offline drafts, development and test runs.
- Cloud: hard reasoning, complex coding, very long contexts, and anything customer-facing where top quality matters.
Ollama also offers optional cloud-hosted models through the same tooling; those send data off your machine, so treat them like any cloud provider.
Pitfalls
- Pulling a model too big for your RAM. It will crawl or fail. Drop to a smaller tag.
- Forgetting the context limit. If summaries ignore the end of a document, raise
num_ctx. - Exposing port 11434 to the internet. There’s no built-in auth. Keep it on localhost or put it behind a proxy with authentication. This is no longer a theoretical risk: in November 2026 Amazon reported attackers’ AI agents scanning for exposed AI tools, Ollama among them, to steal and spend the model keys they hold. If you ever changed
OLLAMA_HOSTor opened a tunnel with ngrok or Cloudflare, check it from a phone on cellular data. See our one-hour lockdown for self-hosted AI tools. - Running a model from an uploader you don’t know. A popular model name proves nothing on an open hub. Fake copies with hidden malware were found on Hugging Face in November 2026. Use the maker’s page or Ollama’s library, and scan anything else with GGUF Radar.
- Assuming full OpenAI parity. The compatibility layer is a subset; test your exact features.
- Ignoring licenses. Open models carry their own licenses; check commercial-use terms on each model’s page or on Hugging Face.
Where to go next
Plug your local model into an agent: the OpenAI Agents SDK playbook works the same way once you point it at a compatible endpoint. Or use a local model as the classifier in our n8n inbox triage playbook so email content never leaves your server.
Example: a private document summarizer in 15 lines
Say you’re a consultant who needs summaries of client interview notes without uploading them anywhere. After Step 6 created the summarizer model, this script summarizes every .txt file in a folder:
from pathlib import Path
from ollama import chat
for f in Path("interviews").glob("*.txt"):
text = f.read_text()
r = chat(
model="summarizer",
messages=[{"role": "user", "content": text}],
options={"num_ctx": 8192},
)
Path(f.with_suffix(".summary.md")).write_text(r.message.content)
print("done:", f.name)
The num_ctx option is the line people forget. Interview notes often run past the 4,096-token default, and when they do the model quietly works from only part of the file. Check one summary against the end of its source file before you trust the rest. Speed depends on your machine and model size, so time the first few files before you plan around it. Nothing leaves the machine.
Got PDFs or scans instead of text files? Convert them on the same machine first, or the summary is only as good as whatever copy-paste produced. LlamaIndex’s free LiteParse parser does this locally, and its OCRia-nano-1 models (released Nov 23, 2026) add OCR, tables and layout on an ordinary CPU. After pip install liteparse, these lines from LlamaIndex’s model card write a Markdown copy you can feed to the loop above:
from pathlib import Path
from liteparse import LiteParse, models
models.pull("ocria-nano") # once
parser = LiteParse(ocr_backend="ocria-nano", output_format="markdown")
for f in Path("interviews").glob("*.pdf"):
f.with_suffix(".txt").write_text(parser.parse(str(f)).text)
Read the OCRia-nano-1 license before you use it for work: commercial use is free only if your company is under $2M in revenue and under $2M in funding. Check tables in the output against the original, since totals and dates are where parsers slip.
Choosing between local, hosted open models and frontier APIs
| Need | Good fit |
|---|---|
| Data must not leave the device | Ollama locally |
| Open models, but fast and without your own hardware | Hosted inference such as Groq, Together AI or OpenRouter |
| Best possible reasoning or coding | Frontier APIs (OpenAI, Anthropic, Google) |
Many teams use all three: local for development and sensitive preprocessing, hosted open models for cheap high-volume jobs, frontier models for the hardest steps.
Rather have it set up with you?
If this workflow fits your business but you'd rather not build it alone, we can scope it with you. Pricing is quoted to a defined scope before any work starts.
Request a scoped quote →FAQ
Is Ollama free?
Yes. Ollama is open source and running models locally is free apart from your hardware and electricity. Ollama also offers optional cloud models that require signing in.
What port does Ollama use?
By default Ollama listens on 127.0.0.1 port 11434. Change the bind address with the OLLAMA_HOST environment variable.
Can I use the OpenAI Python library with Ollama?
Yes. Point the client at base_url http://localhost:11434/v1/ and pass any api_key value such as 'ollama'; the key is required by the client but ignored locally. Ollama supports a subset of the OpenAI API.
What is the default context window?
Ollama's FAQ states a default of 4096 tokens. Raise it with the OLLAMA_CONTEXT_LENGTH environment variable or the num_ctx parameter, at the cost of more memory.
Is it safe to download AI models from Hugging Face?
From the model maker's own page, generally yes. Anyone can upload to Hugging Face, and in November 2026 researchers found fake 'uncensored' copies of popular models with hidden Windows malware, downloaded about 2,400 times. Follow the maker's own link, prefer Ollama library builds, scan other files with GGUF Radar, and never run a command a model tells you to run.
Are local models as good as GPT or Claude?
Usually not for the hardest reasoning or coding tasks, but small open models are very capable for summarizing, classifying, extraction and drafting, and they keep data on your machine.
Last checked against official docs: October 2026 (Ollama README, docs.ollama.com OpenAI compatibility, FAQ and Modelfile reference); Inkling 2 Mini notes checked Nov 21, 2026 against the Hugging Face model card and Ollama library page; LiteParse and OCRia-nano-1 notes checked Nov 27, 2026 against the Hugging Face model card and license; model download safety notes checked Nov 28, 2026 against Malspark's Nov 27 report, the GGUF Radar README and Hugging Face's Ollama docs. Prices and UI labels change; confirm on each vendor's site.
New AI tools, honest reviews and playbooks. One email a week.
Almost done. Confirm your email in the beehiiv tab we just opened.
That email does not look right. Try again.
Free. Unsubscribe anytime.