Context Engineering for Local Coding Agents: Budgeting a Window You Pay For

Written by Jakub Rusinowski · Last updated 2026-08-04 · Hardware figures computed by our VRAM engine

Context engineering means curating the smallest high-signal set of tokens the agent sees each turn. Locally it has a second dimension API users never face: context is VRAM. Every token of window costs KV cache that competes with the weights, so the job is to budget a window you are paying for in hardware — and to compact before quality degrades, which on a local model happens well before the window is full.

On a hosted model, context is an abstract limit with a price per token. On your own GPU it is physical: the KV cache for a long agent conversation is gigabytes of the same VRAM holding the weights. Doubling the context can mean dropping a quantisation level or a model tier — a trade no API user ever has to make.

That changes the discipline. Cloud advice says "you have 200K, use it." Local advice is the opposite: decide your window from your VRAM first, then fit the agent's working set into it. Get this backwards and you meet the two most common local-agent failures — silent prompt truncation, and out-of-memory errors halfway through a task that started fine.

What is actually in the window

Before optimising, know the bill. A typical VS Code agent turn, mid-task, on a repo of moderate size:

ComponentTypical tokensNotes
Harness system prompt2,000 – 10,000Varies enormously by tool. Aider is at the low end; full agents at the high
Tool / MCP schemas500 – 25,000+Every connected MCP server adds its whole tool list, every request. The usual silent killer
Rules file (AGENTS.md)300 – 1,500Cheap, high leverage — keep it that way
Repo map / file tree1,000 – 5,000Aider's repo map, or whatever index your agent injects
Files in the working set2,000 – 20,000The part that is actually about your task
Conversation + tool resultsgrows without boundTest logs, greps, build output. Where windows die
Room for the answer1,000 – 4,000Reserve it explicitly or the model gets truncated mid-diff

Two lines dominate: tool schemas and accumulated tool results. Neither is your prompt, and neither is visible in the chat UI — which is exactly why people conclude the model is dumb when their window is 70% overhead.

The VRAM cost of context

KV cache scales linearly with context length and with model size, and it is additive on top of the weights. The rough shape on a 24 GB card running a 27B at Q4: the weights take roughly three-quarters of the card, and each additional 8K of context takes a further slice — which is why 32K fits and 128K does not, on a model whose card advertises 256K.

Rather than trusting a table, model it: the VRAM calculator computes weights plus KV cache for a specific model, quantisation and context length, using the same engine as every figure on this site. Three practical consequences:

If the numbers say your target context does not fit, the honest options are a smaller quant, a smaller model, or renting a bigger GPU — not hoping the truncation goes unnoticed.

Context rot: why quality drops before the window is full

Filling a window does not degrade a model gracefully at 100% and perfectly below it. Attention quality falls as the window fills and as irrelevant content accumulates — the pattern usually called context rot. Local models, generally smaller and often trained with less long-context data than frontier models, hit it earlier: a practical rule is that a local coding agent starts getting noticeably worse somewhere around half to two-thirds of its served window.

Symptoms, in the order they usually appear:

1. It stops honouring the rules file (those tokens are furthest back and most diluted). 2. It re-reads files it already read, because the earlier read has been buried. 3. It references functions that do not exist — often ones from an earlier, discarded plan. 4. Edits start conflicting with edits it made twenty turns ago.

The fix is not a bigger window. It is removing tokens: compaction, subagents, or a restart. A fresh session with a one-paragraph handoff routinely outperforms a degraded session with full history — and locally it costs nothing but the prefill.

Five techniques, ordered by payoff

1 · Set the served window deliberately. Not a context technique so much as the precondition for all of them. Ollama truncates to its default silently; a long-context variant is two lines:

cat > Modelfile <<'EOF'
FROM qwen3.6:27b
PARAMETER num_ctx 32768
EOF
ollama create qwen3.6:27b-32k -f Modelfile

Then select that variant in the agent, and confirm the cache actually fits with the checker before trusting it.

2 · Truncate tool results at the source. A full test log or a recursive listing can be tens of thousands of tokens of noise. Keep the tail of failures (that is where the assertion is), cap greps to the matching lines with a little surrounding context, and never let a raw find or ls -R into the window. Most harnesses expose a limit; set it to something your window can survive.

3 · Compact deliberately, not at 95%. Production harnesses compact in stages — drop stale tool output, then summarise old turns, then rebuild the working set. Whatever your tool offers, trigger it earlier than the default: thresholds designed for a 200K frontier window are far too late for a local 32K one. What must survive a compaction: the goal, decisions already made, the current diff, and open problems. What should not: raw logs, superseded plans, file contents already edited.

4 · Curate the working set by hand. Adding files explicitly beats letting the agent discover them — it is faster, cheaper, and dramatically improves small-model accuracy. Aider's /add and /drop, Cline's explicit file mentions, and per-project ignore rules are the primitives. Three right files beat thirty plausible ones.

5 · Isolate with subagents or separate sessions. The pattern that scales: a sub-task runs in its own window and returns a short summary rather than its whole transcript. Even without built-in subagent support you can do this manually — run the noisy exploration ("which files touch auth?") in one session, then start the edit session with the three-line answer. This is the single most effective local technique for long tasks, because it keeps the main window small permanently.

Retrieval vs the whole repo

Long-context marketing suggests pasting the codebase in. For local agents this is the wrong instinct twice over: you cannot afford the cache, and accuracy usually *drops* when the relevant file is buried among fifty irrelevant ones.

The working alternative is search, not stuffing. Coding agents that lean on grep/ripgrep and the file tree — reading only what the search returns — tend to outperform ones that pre-load context, and they are far cheaper per turn. Practically:

Whole-file context earns its place for the file being edited, and rarely beyond it.

A budget that works on 16 and 24 GB

A concrete starting allocation, assuming a 16K served window on 16 GB and 32K on 24 GB:

Slice16K window32K window
Harness prompt + tool schemas≤ 4K (pick a light harness, few tools)≤ 8K
Rules file≤ 1K≤ 1.5K
Working-set files4–6K (2–3 files)10–14K (4–6 files)
Live conversation + tool results3–4K, compacted aggressively6–8K
Reserved for the reply2K3K

If the schemas alone break the first row — the common case with several MCP servers connected — that is your fix, and it is a bigger win than any prompt rewrite. Details in MCP with local coding agents.

Frequently asked questions

What is context engineering for coding agents?
Curating the smallest high-signal set of tokens the agent sees on each turn — system prompt, tool schemas, rules, the files that matter, and a compacted history — instead of assuming a big window fixes things. On local hardware it also means budgeting that window against VRAM, because every token of context is KV cache competing with the model weights.
How much VRAM does context use?
KV cache grows linearly with context length and model size, on top of the weights. On a 24 GB card a 27B at Q4 typically leaves room for roughly 32K of context, not the 256K the model card advertises. Model the exact figure for your model, quantisation and context length with the VRAM calculator rather than guessing — and consider 8-bit KV cache, which roughly halves the cost.
What is context rot?
Degradation in answer quality as the context window fills with accumulated and increasingly irrelevant content. It appears well before the window is technically full — on local models often around half to two-thirds — and shows up as ignored instructions, re-reading files, and referencing code that does not exist. The fix is removing tokens (compaction, subagents, a fresh session), not a longer window.
Should I give my local coding agent the whole repository?
No. You usually cannot afford the KV cache, and accuracy tends to fall when the relevant file is buried among many irrelevant ones. Let the agent search the repo with grep or a repo map and read only what it finds; reserve full-file context for the file being edited. For large codebases, index separately and feed back snippets.
When should a coding agent compact its context?
Earlier than the defaults suggest. Auto-compaction tuned for a 200K frontier window triggers far too late for a local 32K one; aim to compact when roughly half to two-thirds full. Preserve the goal, decisions already made, the current diff and open problems; discard raw logs, superseded plans and the contents of files already edited.

Keep going