Every model call carries the whole conversation. Keeping that small and stable is most of what makes an agent fast and cheap, especially on a local model with a modest context window.

A stable opening
The system prompt, the tool schemas and the environment block are byte-identical on every step of a session; the environment block is computed once and cached. The conversation is append-only. That lets Ollama's KV cache and cloud providers' prompt caches reuse everything up to the newest message, so each step after the first only pays for what is new.
This is also why skills and MCP tools are fixed when a run starts: adding one mid-run would change the opening and lose the cache.
The fixed overhead is about 2,600 tokens for the main agent and about 1,200 for an explore subagent.
Keeping tool output bounded
- Reads are ranged (1,000 lines by default), and searches are capped.
- Command output keeps the first 6,000 and last 14,000 characters, where errors usually are.
- Every result is trimmed to
gbsAgent.maxToolOutputChars(20,000 by default) before the model sees it. - Broad searches happen in subagents, whose output never enters the main conversation.
The compaction ladder
Trimming happens in a fixed order, cheapest first:
- Earlier requests' output. Each time you send a new request, tool results over 1,500 characters from earlier requests become a short note saying what the call was. The agent can read the file again if it needs to. Free.
- Old tool output in this request. The budget is the context window minus
the answer reserve (the smaller of
gbsAgent.maxOutputTokensand a quarter of the window), times 0.85. When the estimated prompt passes it, all but the six most recent tool results over 600 characters become notes. Free. - A summary. If that is still not enough, one model call condenses the
older part of the conversation into a summary, cutting only at a point that
does not separate a tool call from its result.
/compactdoes this on demand.
In normal numbers
The default window is 32,768 tokens, about 24,000 words or 50 pages. A bigger window is not automatically better: models get noticeably worse at finding things in the middle of a very full context, which is why the agent trims even when there is room.
| Setting | Default | Raise it when |
|---|---|---|
gbsAgent.contextWindow | 32,768 | Your model and hardware support more; set it to the real value |
gbsAgent.maxOutputTokens | 8,192 | Long files get cut off mid-write |
gbsAgent.maxSteps | 60 | Big tasks stop to check in too early |
gbsAgent.maxToolOutputChars | 20,000 | Test failures get trimmed right where it matters |
Counting requests, not tokens
One instruction becomes many model calls: three to eight for a small edit, twenty to sixty for a refactor with subagents. Request limits, not token allowances, are what run out first on a hosted model. See How free tiers run out.
