Why does my agent forget things or fail tool calls?
Agents fail locally for three reasons: the context is too short, the model is too small, or the tool-call format is wrong. Check them in that order, because the first is the most common and the easiest to fix.
Context length, again
An agent sends a lot more than your message. The system promptSystem promptStanding instructions placed before the conversation.Read more, the tool definitions, every file it read and every tool result all go into the context on every turn. A coding agent can fill 20K tokensTokenA chunk of text, usually part of a word. Models read and write in tokens.Read more before it has done anything.
When the context fills, many runtimesRuntimeThe software that loads a model file and runs it.Read more drop the oldest tokens without telling you. The agent loses its instructions or the first half of the task, and you see it forget a file it just read, repeat a step, or stop calling tools at all.
Give an agent at least 32K tokens. It is comfortable at 64K tokens.
Set the context on the runtime, not only in the agent. The agent's setting does nothing if the server loaded the model with a smaller one.
OLLAMA_CONTEXT_LENGTH=32768 ollama serve$env:OLLAMA_CONTEXT_LENGTH = "32768"
ollama serveOLLAMA_CONTEXT_LENGTH=32768 ollama serveQuit the Ollama app first, so this server is the one that answers. In LM Studio, set Context Length in the model's load settings. With llama.cpp, pass -c 32768 to llama-server.
A longer context costs memory. 32K tokens of KV cacheKV cacheSaved work from earlier tokens, so the model does not reread the whole conversation for every new token.Read more on a mid-size model can take several GB. If the model no longer fits, use a smaller quantizationQuantizationStoring each weight with fewer bits so the model takes less memory.Read more or quantize the KV cache, see How do I make it faster? and Why won't it load?.
Model too small for tools
Tool callingTool callingA model asking the app to run a function, such as a search or a shell command.Read more needs the model to write strict JSON, pick the right tool, and keep a plan across many steps. Small models can do one call. They lose the plot over twenty.
For a stable agent loop, we recommend 27B and up. Below that, expect invented tool names, missing arguments and loops that never finish. Which size of model do I need? shows what each class can do.
| Model | Parameters | 4-bit | 8-bit | Footprint | Licence |
|---|---|---|---|---|---|
| gpt-oss-20b | 21B / 3.6B active | 13 GB* | – | Apache 2.0 | |
| Mistral Small 3.x | 24B | ~14 GB | 25 GB | Apache 2.0 | |
| Gemma 4 26B A4B | 26B / 4B active | ~16 GB | 29 GB | Apache 2.0 | |
| Qwen3.8-27B | 27B | 16 GB | 30 GB | Apache 2.0 | |
| Muse Glimmer 30B | 30B | 17 GB | 34 GB | Apache 2.0 | |
| Gemma 4 31B | 31B | 19 GB | 34 GB | Apache 2.0 |
~ means the figure is approximate. * means the model ships in its own low-precision format, so no Q8 build applies.
As of 27 Nov 2026
Also check the model was trained for tools at all. The model card says so, and Ollama tags such models with "tools" on its library page. A model without tool training will describe the call in prose instead of making it.
Template and tool-format mismatches
Each model family writes tool calls in its own format. The runtime has to recognise that format and turn it into the structured call your agent expects. If it does not, the agent sees plain text containing JSON and does nothing with it.
The fixes depend on the runtime:
- Ollama and LM Studio handle the format for supported models. Update both the runtime and the model if calls come back as text.
- With llama.cpp's server, start it with
--jinjaso it uses the model's own template, which includes the tool format. - With vLLM, tool calling is off by default. Enable it with
--enable-auto-tool-choiceand pick the parser for your model family with--tool-call-parser.
vllm serve <model> --enable-auto-tool-choice --tool-call-parser hermesThe hermes parser fits some families and not others. The vLLM docs list which parser goes with which model. When do I need vLLM or SGLang? covers the rest of the setup.
Ask your agent to do one thing that needs exactly one tool, such as "list the files in this folder". If that fails, the problem is format or model. If it works and longer tasks fail, the problem is context.
Go deeperThat's enough
Why speed matters for agents too
Agent loops run many model calls per task, and reasoning models add thousands of thinking tokens to each. A speed that feels fine in chat can make an agent take many minutes per task. Agent loops need well above chat speed, and How fast is fast? has the thresholds.
The API format
Most agents talk to the runtime through an OpenAI-style API. Some expect a different API shape, or a specific base URL ending in /v1. A wrong base URL gives connection errors, not bad answers. How do I call a local model from code? lists the endpoints for each runtime.
MCP servers add context
Every MCPMCPModel Context Protocol. A standard way to plug tools into a model.Read more server you connect adds its tool descriptions to the context on every turn. Ten servers with ten tools each can take thousands of tokens before your task starts. Connect only the servers the task needs. How do tools and MCP work with local models? explains how MCP fits in.