---
title: Your coding agent runs for 300 turns and its context fills with tool output. Design a context-management strategy using…
url: https://gurkanozkan.ai/learn/questions/recent-b-long-context-03/
markdown: https://gurkanozkan.ai/learn/questions/recent-b-long-context-03.md
kind: question
role: Applied LLM
stage: System design
difficulty: Medium
json: https://gurkanozkan.ai/api/learn/questions/recent-b-long-context-03.json
robots: noindex
---

# 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.

Applied LLM · System design · Medium

## 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.

*Corrected by an independent AI reviewer.*

## 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?

## Topic guides

- [Agents and tool use](https://gurkanozkan.ai/learn/topics/agents.md)
- [LLM cost and unit economics](https://gurkanozkan.ai/learn/topics/cost.md)
- [Long context and context management](https://gurkanozkan.ai/learn/topics/long-context.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
