How to run a local LLM for coding (Ollama and LM Studio)
Run a coding model on your own computer with Ollama or LM Studio: hardware, the context window setting most people miss, and honest limits.

Key takeaways
- A local model runs well only when the model and its context window fit in GPU or unified memory.
- Raise the context window to at least 64K tokens for coding agents; Ollama's default can be as low as 4K.
- Ollama and LM Studio both speak OpenAI- and Anthropic-style APIs, so most coding tools can connect without plugins.
- Local models handle explanations and small edits well; long multi-step agent runs are where hosted models still do better.
To run a local LLM for coding, install a model server such as Ollama or LM Studio, download a model that fits in your computer's memory, raise the context window to at least 64K tokens, and point your coding tool at the server's local address. Your prompts and code never leave your machine, and there is no per-message bill.
The catch is hardware. A local model is only as good as the memory you can give it, and the models that fit on a laptop are smaller than the ones behind Claude or ChatGPT. This guide covers the setup, the settings that trip most people up, and an honest view of what to expect.
Why run a model locally at all
People usually have one of four reasons:
- Privacy. Client code, contracts or medical data that may not be sent to a cloud provider can stay on your own disk.
- Cost. After the hardware, every prompt is free. That suits long refactors, bulk renames and experiments you would not want to pay for token by token.
- Working offline. On a plane or a bad hotel connection, the model still answers.
- A fallback. When your cloud plan hits its usage limit, a local model can keep small jobs moving. See what to do when you hit your Claude usage limit.
If none of these apply to you, a hosted model will usually write better code faster. That is not a reason to skip local models, just a reason to use them for the right jobs.
Check your hardware first
A model has to fit in memory to run at a usable speed. On a PC that means the graphics card's VRAM; on an Apple Silicon Mac it means unified memory, which the GPU shares with the rest of the system. When a model does not fit, the server spills part of it onto the CPU, and replies slow down sharply.
Two things take memory: the model weights and the context window. The weights are roughly the size of the download. The context window grows with the number of tokens you allow, and coding agents need a lot of them, because every file they read goes into the prompt. A practical rule: pick a model whose download is clearly smaller than your free GPU or unified memory, and leave room for the context on top.
Set up Ollama step by step
Ollama is a free model server for macOS, Windows and Linux. It runs in the background and serves models over a local HTTP API.
- Download Ollama from ollama.com/download and open it. On a Linux server, start it with
ollama serveif it is not already running. - Pull a model. Browse the Ollama library and prefer models listed with tool support, since coding agents work by calling tools such as "read file" and "run command".
- Try it in the terminal before wiring up anything else.
ollama pull <model-name>
ollama run <model-name>
# type /bye to leave the chat
The server listens on http://localhost:11434. Besides its own API, Ollama answers on OpenAI-compatible routes (/v1/chat/completions, /v1/responses) and an Anthropic-compatible one (/v1/messages), so most coding tools can talk to it without a plugin. Ollama can also start some coding agents for you: ollama launch claude opens Claude Code on a local model, and ollama launch codex does the same for Codex once Codex is installed. The details are in the Ollama quickstart.
Fix the context window before anything else
This is the setting that breaks most first attempts. According to Ollama's context length docs, the default depends on your VRAM: 4K tokens below 24 GiB, 32K between 24 and 48 GiB, and 256K at 48 GiB or more. Ollama's own advice is that agents and coding tools should get at least 64,000 tokens.
With a 4K window, an agent's prompt is cut before the model sees it. The model then forgets your instructions halfway through and edits the wrong file, and it looks like the model is bad when the real problem is the setting. To raise it, move the context slider in the Ollama app's settings, or start the server like this:
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
ollama ps # the CONTEXT column shows what each loaded model really got
Check the PROCESSOR column in ollama ps too. If it shows a CPU share, the model plus its context did not fit in GPU memory. Use a smaller model or a smaller window.
Or use LM Studio
LM Studio is a desktop app with a model browser, which makes it easier to compare models before you commit disk space. To serve a model to other tools, open the Developer tab and switch on Start server, or run lms server start in a terminal. Its server docs list OpenAI-compatible and Anthropic-compatible endpoints; the usual local address is http://localhost:1234. LM Studio often loads models with a small context length, so raise it in the model's load settings, for the same reason as above.

What to expect, honestly
| Task | How local models usually do |
|---|---|
| Explaining code, writing docs and commit messages | Good. Small context, single answer. |
| Small edits in one or two files | Workable with a tool-capable model and a large enough context window. |
| Long multi-step agent runs across a codebase | The weak spot. Smaller models lose track, repeat steps or call tools wrongly more often than large hosted models. |
| Speed | Fine when the model fits in GPU memory; slow when it spills onto the CPU. |
A sensible pattern is to keep a hosted model for planning and hard problems and send routine work to the local one. Give the local model narrow tasks with clear file names, and check its edits with git diff before you keep them. The prompts in how to prompt AI coding agents help even more with small models than with big ones.
Use a model running on another computer
A desktop with a big graphics card can serve your laptop. By default Ollama only listens on its own machine. Start it on the desktop with OLLAMA_HOST=0.0.0.0, then use the desktop's address (for example http://192.168.1.20:11434) from the laptop. Keep that port inside your home or office network or a private VPN such as Tailscale; do not expose it to the internet.
Local models in Ninja Builder
If you would rather not wire tools to a server by hand, Ninja Builder connects to one in a dialog. Go to Settings › Accounts › Add account › Local model, pick Ollama, LM Studio or Custom (the address fills in), click Test, and tick the models you want. Test also warns in one line when the server runs a model with too small a window for agent prompts, with the fix. Chats on a local model cost nothing per message and stay on your network.

Local chats start in lean mode by default: a short guide and the basic file and command tools, so even a modest computer answers quickly. You can switch lean mode off on the server's row for the full set of skills and helper agents. You can also move a chat that started on Claude or ChatGPT to the local model and carry on in the same thread. The setup is described in the accounts and usage docs. Ninja Builder is free for 14 days, then $4.99 a month (pricing); download it here.
Frequently asked questions
Can I run Claude Code with a local model?
Yes. Ollama answers on an Anthropic-compatible route, and the command ollama launch claude starts Claude Code on a local model. Expect weaker results than with Claude models on long tasks.
How much memory do I need for a local coding model?
Enough to hold the model weights plus the context window in GPU memory, or unified memory on an Apple Silicon Mac. The download size of the model is a first estimate for the weights; the context needs extra room on top.
Why does my local model ignore my instructions?
Most often the context window is too small and the server cuts the prompt. Set it to at least 64,000 tokens and check the CONTEXT column in ollama ps.
Is a local LLM free?
There is no per-message charge. You pay for the hardware and the electricity, and Ollama and LM Studio themselves are free to download.
Keep reading

What is MCP? The Model Context Protocol explained for developers
How the Model Context Protocol connects AI agents to your tools, how to add a server, and how to keep it safe.

How to prompt AI coding agents: examples that work
The four parts of a prompt a coding agent can act on, with before-and-after examples for a bug fix and a feature.

What AI coding really costs: subscriptions vs API keys
Plans with limits or pay-per-token API keys: how coding usage is counted, current prices, and how to estimate your own bill.