Skip to content

Context as a Finite Resource

Every technique in this guide stems from one reality: the context window is finite, and quality degrades as it fills. Understanding the mechanics of this constraint is essential for engineering effective agentic workflows.

The context window holds everything the model can “see” during a single interaction:

  • System instructions — agent configuration files, permissions, tool definitions
  • Conversation history — all messages between you and the agent
  • Tool outputs — file reads, command results, search results
  • Agent state — current task context, accumulated decisions

For many modern AI coding agents, the context window is approximately 200,000 tokens. A single file read can consume 1,000-10,000 tokens. A debugging session exploring 20 files might consume 100,000+ tokens — half the window.

ActivityTypical Token Cost
System prompt + agent configuration file2,000-5,000
Single file read (500 lines)3,000-6,000
git diff output500-5,000
Test suite output1,000-10,000
Build error log500-3,000
Codebase exploration (10 files)30,000-60,000
Long debugging session50,000-150,000

The biggest consumers are file reads during exploration and verbose tool outputs (build logs, test results, large diffs).

Quality doesn’t degrade linearly — it follows a curve:

Quality
│ ████████████████
│ ████████
│ ████
│ ████
│ ██
│ ██
│ █
└──────────────────────────────────────▶ Context Fill %
0% 25% 50% 75% 90% 100%

Key thresholds:

  • 50%: First signs of degradation in instruction following
  • 75%: Noticeable recall issues for earlier context
  • 90%: Significant quality loss, missed instructions
  • 95%: Auto-compaction triggers (varies by AI coding agent)

Configure a status indicator to show context utilization — most AI coding agents expose this in their status bar or via a configuration option. This makes context usage always visible, like a fuel gauge.

Clear/reset your context between unrelated tasks. A fresh context for a new task always outperforms a polluted context from an old one.

Never ask your AI agent to “investigate” without scope boundaries:

# Bad — unbounded exploration
Look into why the tests are failing.
# Good — scoped investigation
Check the auth middleware tests in src/__tests__/auth/.
The test 'should refresh expired tokens' is failing.
Look at the token refresh logic in src/middleware/auth.ts lines 45-80.

Strategy 4: Use Sub-Agents for Heavy Reads

Section titled “Strategy 4: Use Sub-Agents for Heavy Reads”

When you need to explore many files, delegate to sub-agents:

Use sub-agents to:
1. Find all files that import from the auth module
2. Check which ones handle session tokens
3. Report back a summary of the token flow

The sub-agents consume their own context windows. Your main context receives only the summary.

When you compact your context, tell your agent what to preserve:

Compact context. Preserve: modified file list, the API design we agreed on,
test commands, and the remaining implementation steps.
Discard: file exploration, build logs, superseded approaches.

For tool-specific compaction commands and configuration, see the Tool Configuration Reference.

Think of context as a budget:

SpendCost (Tokens)Value
Essential context (agent config file, task description)3,000High — always needed
Targeted file reads5,000-15,000High — directly relevant
Exploratory file reads20,000-60,000Medium — use sub-agents instead
Build/test output5,000-20,000Low-Medium — extract key info
Failed approaches10,000-30,000Negative — actively harmful

The golden rule: Every token should earn its place. If a token doesn’t directly contribute to the current task, it’s reducing the quality of the tokens that do.

  • Context utilization directly impacts output quality — track it continuously
  • The biggest context consumers are file reads and verbose tool outputs
  • Clear between tasks, scope investigations, and delegate exploration to sub-agents
  • Target 40-60% utilization for complex reasoning tasks
  • Compact proactively with explicit preservation instructions
  • Failed approaches are actively harmful — clear them out