Your coding agent runs for 300 turns and its context fills with tool output. Design a context-management strategy using what model APIs now offer (context editing, compaction, memory files), and explain how you keep prompt-cache hits high and check that nothing important is lost.
Answer
Model answer, written in a candidate's voice
In short
- Clear stale tool results by rule before summarising: keep the last few, keep identifiers and file paths, drop verbose logs
- Use compaction to replace older turns with a summary that keeps goals, decisions, open tasks and identifiers, while recent turns stay verbatim
- Store durable facts in memory files outside the window and reload just in time; run noisy searches in sub-agents with clean windows
- Keep the prefix stable and history append-only so caching keeps hitting, accepting one cache miss after each compaction or batched clearing pass
- Measure task success, tokens per task (Anthropic reported 84% fewer tokens from context editing in a 100-turn eval) and targeted recall probes for facts that must survive compaction
In full
I treat the window as a working set, not a transcript: before anything is summarised I clear stale tool outputs by rule, keeping the last few verbatim and preserving identifiers, file paths, error codes and decisions while dropping or truncating verbose logs. Then I use compaction to replace older turns with a summary that keeps goals, decisions, open tasks and identifiers, while the recent turns remain verbatim, and I push durable facts into memory files or notes outside the window that I reload just in time, with noisy searches delegated to sub-agents that have clean windows so their bulk never enters the main context. To keep prompt-cache hits high I keep the prefix byte-identical (stable system prompt, tool schemas and instructions) and keep history append-only, accepting one cache miss after each compaction or clearing pass (so I clear tool results in batches, not every turn) and re-warming rather than rewriting earlier turns. I check that nothing important is lost with task success, tokens per task (Anthropic reported 84% lower token consumption from context editing in a 100-turn web-search evaluation) and targeted recall probes for facts that must survive compaction, such as identifiers, file paths and open tasks, failing the run if a probe misses.
What a strong answer covers
- Clears stale tool results by rule (keep the last few, keep identifiers and file paths) before summarising anything.
- Uses compaction to replace older turns with a summary that keeps goals, decisions, open tasks and identifiers, and keeps recent turns verbatim.
- Stores durable facts in memory files or notes outside the window and reloads them just in time; uses sub-agents with clean windows for noisy searches.
- Keeps the prefix stable and history append-only so caching keeps hitting, accepting one cache miss after each compaction.
- Measures task success, tokens per task (Anthropic reported 84% fewer tokens from context editing in a 100-turn eval) and targeted recall probes for facts that must survive compaction.
Common mistakes
- Summarising every turn, which destroys the cache prefix and loses detail.
Follow-ups
- When would you prefer your own summariser over a server-side one?