# Why does my agent forget things or fail tool calls?

> The context is too small, the model is too small for tools, or the tool format does not match. Check them in that order.

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 prompt, 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 tokens before it has done anything.

When the context fills, many runtimes 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.

**Rule of thumb.** 
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.

**macOS**

```bash
OLLAMA_CONTEXT_LENGTH=32768 ollama serve
```

**Windows**

```powershell
$env:OLLAMA_CONTEXT_LENGTH = "32768"
ollama serve
```

**Linux**

```bash
OLLAMA_CONTEXT_LENGTH=32768 ollama serve
```

Quit 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`.

**The catch.** 
A longer context costs memory. 32K tokens of KV cache on a mid-size model can take several GB. If the model no longer fits, use a smaller quantization or quantize the KV cache, see [How do I make it faster?](https://localagi.co/tuning-for-speed) and [Why won't it load?](https://localagi.co/out-of-memory).

## Model too small for tools

Tool calling 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?](https://localagi.co/model-classes) shows what each class can do.

| Model | 4-bit | 8-bit | Licence |
| --- | --- | --- | --- |
| gpt-oss-20b | 13 GB | – | Apache 2.0 |
| Mistral Small 3.x | 14 GB | 25 GB | Apache 2.0 |
| Gemma 4 26B A4B | 16 GB | 29 GB | Apache 2.0 |
| Qwen3.8-27B | 16 GB | 30 GB | Apache 2.0 |
| Muse Glimmer 30B | 17 GB | 34 GB | Apache 2.0 |
| Gemma 4 31B | 19 GB | 34 GB | Apache 2.0 |

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 `--jinja` so 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-choice` and pick the parser for your model family with `--tool-call-parser`.

```bash
vllm serve <model> --enable-auto-tool-choice --tool-call-parser hermes
```

The `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?](https://localagi.co/vllm-and-sglang) covers the rest of the setup.

**Try it.** 
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 deeper

### 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?](https://localagi.co/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?](https://localagi.co/local-api) lists the endpoints for each runtime.

### MCP servers add context

Every MCP 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?](https://localagi.co/tools-and-mcp) explains how MCP fits in.
