Context window management strategies for coding agents

Know which strategy is at work, pick the right one per task, and keep decisions where no compaction can drop them.

By · Published

Context window management for coding agents is choosing what stays in the model's limited working memory and what lives outside it. There are four strategies: truncation, summarization, retrieval and structured external memory. Most agents combine them, and knowing which one is at work explains most surprises in a long session.

This guide compares the four, shows when each fits, and how to combine them on long projects.

Why do coding agents fill the window so fast?

A chat assistant spends tokens on your message and its reply. A coding agent spends them on everything it touches: file reads, search results, test output, diffs and the definitions of its tools. Reading one large file can cost more than a dozen chat turns, and the agent often rereads a file after editing it to check the result. A failing test suite can add thousands of lines of stack traces at once.

What are the four strategies?

Truncation

Drop the oldest messages when the window is full. It is cheap and explains why an agent suddenly forgets a decision from an hour ago.

Summarization and compaction

Replace older turns with a summary and continue from it. Claude Code does this automatically as a conversation approaches the limit, and /compact runs it on demand with your focus instructions (Claude Code commands). Codex offers /compact and an automatic compaction threshold, and opencode has automatic compaction in its configuration (opencode config). A summary keeps conclusions but tends to drop dead ends, which are exactly what stops an agent from trying them again.

Retrieval

Keep the window small and fetch what the task needs: search over the code, the documentation or a project record, often through an MCP tool (MCP specification). Retrieval trades memory for precision. Its risk is a stale or badly scoped index that returns the wrong files, which the agent then trusts.

Structured external memory

Keep durable context outside the model, in files such as AGENTS.md or in a shared workspace every agent reads. A dead end one agent found becomes the starting knowledge of the next. This is the strategy that survives months: the persistent project memory guide shows how retrieval and a written record work together.

When should you truncate rather than summarize?

Truncate when the old turns can be recovered or no longer matter: in a refactor, the working tree already shows the result; in debugging, the first ten failed attempts are noise.

Summarize, or write to external memory, when the turns hold decisions with no other home: why you chose one approach, which test setup does not work, what the agent already tried. The test is simple: if losing the turn would force a new conversation to explain it again, keep it outside the window.

Which strategy fits which task?

Strategy Long refactor Multi-file debugging Hand-off between agents
Truncation Poor: drops early decisions Acceptable: recent traces matter most Poor: the next agent loses history
Summarization Good: compresses finished phases Good: keeps the bug's story Good: one brief per hand-off
Retrieval Good: pulls files on demand Good: finds the touched code Acceptable: needs a shared index
External memory Best: decisions survive months Good: known dead ends persist Best: one record instead of retelling

Quick fixes rarely need more than a fresh conversation. Long refactors need decisions written down as they are made, because compaction will eventually summarize them away. Window sizes differ by model, from 200,000 to 1 million tokens on Claude models (context windows), but a larger window only postpones the moment strategy matters.

How do you combine external memory with the window?

Keep three layers apart:

  1. In the window: the current task, the current diff, and a short AGENTS.md or CLAUDE.md with conventions and active rules.
  2. In external memory: decisions, dead ends, current state and past summaries.
  3. Retrieval as the bridge that brings the right slice in at the right moment.

A workable routine: start the project with an AGENTS.md covering stack, style and test commands; after each session, append dated decisions and open questions to a decisions record; compact at natural checkpoints, such as after a passing test run.

Across agents, the record has to be reachable by all of them. Fileaify keeps it as a hosted project workspace: Claude Code, Codex and Cursor read the same decisions over the MCP connector or REST, find the relevant sections with combined keyword and semantic search, and propose updates that you review. Keys are a separate matter, held in a vault and used without being shown to the agent. The Claude Code context window guide covers the commands specific to Claude Code.

Sources

Try it

The demo runs on a prepared project, with no account. To keep decisions outside every agent's window, create a free workspace and give your agent https://fileaify.com/agent.

Common questions

Does compaction lose architecture decisions?

It can. A summary keeps conclusions but often drops dead ends and the reasons behind early choices. Write decisions to a file or a shared record when they are made, not after.

Is retrieval better than a bigger context window?

For most coding work, yes: only the relevant files enter the window. Its weakness is staleness, since an index that lags the code returns files the agent treats as current.

Can Claude Code and Codex share context on the same repository?

Not through their conversations. They share what lives in the project: AGENTS.md for conventions and a shared record for decisions, which both can read.

Which strategy fits a quick fix?

Usually none beyond a fresh conversation. Strategies matter for long refactors, multi-file debugging and hand-offs between agents.

Written with AI assistance, reviewed by Stéphane Mermet and checked against the product. Fileaify publishes this guide and sells the service it describes.