Sub topic 2
Where Context Grows in Coding Agents
Assume you know what tokens and a context window are, and can read basic Python and JSON. A coding agent builds each request from conversation history, instructions, tool calls, and tool results.
Where the Context Grows
Every tool result becomes possible input for the next model turn. File reads, compiler errors, test logs, and repeated history can therefore increase both context usage and cost.
flowchart TD
A[User request] --> B[Model context]
B --> C[Tool call]
C --> D[Tool output]
D --> B
B --> E[Next model response]caption: How an agent loop accumulates context alt: A user request enters the model context, which produces a tool call; the tool output returns to the context before the next response.
The agent may resend earlier messages on every turn. A small final context can still produce a large bill if that context was rebuilt and sent repeatedly.
def loop_input_tokens(system_tokens, step_tokens, turns):
"""Estimate input tokens sent across an uncached agent loop."""
total = 0
context = system_tokens
for _ in range(turns):
total += context
context += step_tokens
return total
print(loop_input_tokens(5_000, 2_000, 20)) # 480000Prompt Caching and Stable Prefixes
Prompt caching stores a repeated prefix, such as system instructions or stable project guidance. A cache read usually costs less than sending those tokens normally. Keep the prefix byte-for-byte stable when the provider requires exact matching. Put changing information, such as current tool output, after the stable prefix.
Keeping Context Small
Use context management to control what the model receives.
- Read only the files and line ranges needed for the current step.
- Truncate successful test output and retain the relevant failure.
- Compact old history into a short summary after a milestone.
- Avoid returning generated files or entire directories when a diff is enough.
Scoped reads reduce cost and often improve accuracy because the model has fewer unrelated details to consider.
Choosing Between Model Tiers
Model tiers trade capability, latency, and price. Use a stronger model for ambiguous planning or difficult debugging. Use a smaller model for predictable formatting, short summaries, or routine test execution.
| Step type | Suitable tier | Main reason |
|---|---|---|
| Plan an unfamiliar change | Strong | Better reasoning and context use |
| Summarize tool output | Small | Simple and repetitive |
| Apply a clear edit | Mid | Adequate capability at lower cost |
A routing policy can assign a tier by step type:
ROUTES = {
"plan": "large-model",
"edit": "mid-model",
"summarize_tool_output": "small-model",
}
def pick_model(step_kind, default="mid-model"):
return ROUTES.get(step_kind, default)
print(pick_model("plan"))
print(pick_model("other"))Exercise
Why can repeated tool output cost more than the final context size suggests?
Show answer
The agent may resend the growing context on every turn, so the billed input is the sum of many intermediate contexts.