Chapter 11
Context Management
Facts blocks that survive compaction, trimming at the boundary, and delegation as a context strategy.
11.1
Lift the facts out of the history
History degrades when it is summarised. A facts block is resent verbatim, so it cannot.
=== CASE FACTS (updated when a new fact appears) === Customer ID: CUST-12345 Order ID: ORD-67890 Order Date: 2025-01-15 Order Amount: $89.99 Issue: Damaged item on delivery Customer Request: Full refund Status: Pending manager approval ===
Ids, dates and amounts — precisely the values summarisation turns into "roughly" and "a few".
11.2
Trim at the boundary
A tool that returns 40 fields when 5 matter spends the window on noise — on every later call this turn.
PostToolUse hook rewrites the result before it ever enters the transcript.Keep order_id, status, total,items, return_eligible — drop the rest. Less context spent, and less noise for the model to wade through.
11.3
Position-aware input
Same content, different order, different recall. Put load-bearing text where retrieval is strong.
11.4
Write findings to a file
A long investigation outlives its own context window. A file does not.
# investigation-scratchpad.md ## Key findings - PaymentProcessor in src/payments/processor.ts extends BaseProcessor - refund() is called from 3 places: OrderController, AdminPanel, CronJob - PaymentGateway API rate limit: 100 req/min - Migration #47 added refund_reason (NOT NULL) — 2024-12-01
Conclusions, not transcripts. Each line is something it would cost tool calls to rediscover.
11.5
Delegation is a context strategy
A subagent spends its own window and hands back a sentence. That asymmetry is the point.
11.6
State on disk survives the crash
Each agent exports its own state; the coordinator reads a manifest to know where to pick up.
// agent-state/web-search-agent.json
{ "status": "completed",
"queries_executed": ["AI music 2024", "AI music composition"],
"results_count": 12,
"coverage": ["music composition", "music production"],
"gaps": ["music distribution", "music licensing"] }
// agent-state/manifest.json
{ "web-search": "completed",
"doc-analysis": "in_progress",
"synthesis": "not_started" }gaps is what tells the coordinator there is work left.completedRecall in 60 seconds
- Extract ids, dates and amounts into a facts block resent every prompt — summarisation destroys them.
- Trim tool results at the boundary (
PostToolUse) — 40 fields when 5 matter is pure waste. - Tool results accumulate for the whole turn, so noise compounds with every call.
- Put key findings first and action items last; bulk detail belongs in the middle.
- Scratchpad files hold conclusions that would otherwise cost tool calls to rediscover.
- Delegate wide reading to a subagent — it spends its window, you keep the summary.
- Constrain subagents: minimal context in, structured result out,
allowedToolstrimmed. - Per-agent state files plus a
manifest.jsonmake a crashed run resumable.