This is a maintained guide to the patterns that fit large codebases and long tasks into agent context windows: selective loading, packing, retrieval, compaction, and memory files; verified against harness documentation as of 2026-11-03.
Every working pattern does one thing, it keeps the window small and fresh, and in 2026 the harness-native features (compaction, subagents, memory files) deliver more of that value than any retrieval pipeline you can buy.
My disagreeable claim: below a few hundred thousand lines of code, a short memory file plus automatic compaction beats a bespoke RAG stack, and teams buying context engines before learning their harness’s /compact are paying money to avoid learning a skill.
The corpus has long argued that context quality dominates model choice; this page is the operational version of that argument.
The problem is attention, not tokens #
The window is finite, but the binding constraint is attention: recall drops as context grows, and it drops worst in the middle.
The lost in the middle result showed models recall information best at the beginning or end of long inputs and significantly worse in the middle, even for explicitly long-context models.
Anthropic’s own engineering post names the phenomenon context rot and treats every token as drawing down a finite attention budget.
Claude Code’s current documentation makes the operational version of the same point: with 1M-token windows and automatic compaction, running out of window is rare, but
/clear between tasks is still worth doing because a focused context gets you better answers and costs less.
The window also fills before you say anything: the model selection guide records harness baselines of about 33k input tokens (Claude Code) versus about 7k (OpenCode) on a minimal task, so a heavy harness taxes every pattern below.
A bigger window does not escape this, it spreads the same attention over more tokens, which is why the patterns exist at every window size.
Load identifiers, not contents #
The strongest pattern keeps lightweight pointers in context (paths, symbols, queries) and pulls contents just in time.
Anthropic calls this just-in-time context: agents hold references and load data at runtime through tools, the way Claude Code analyzes large data with head and tail instead of loading whole files.
Every harness ships the manual version: @ file references in Claude Code and OpenCode inject one named file on demand, and OpenCode’s ! prefix injects a shell command’s output into the prompt.
aider is the purest expression of the family: its tree-sitter repo map ranks symbols by graph references into a roughly 1k-token budget, so the model sees the codebase’s skeleton and fetches bodies only when needed (see the tree-sitter chunking note).
The field is consolidating here: the semantic code search note records pioneers replacing local embeddings indexes with agentic grep loops, because grep is always current and an index is not.
Firewall noisy work into subagents #
Exploration is the biggest context consumer, so run it in a window you throw away.
Claude Code’s docs recommend delegating investigation to subagents, which read files in their own context window and return only a summary; OpenCode does the same with custom commands that force subtask: true so output never pollutes the primary session.
Anthropic quantifies the pattern: a subagent may burn tens of thousands of tokens exploring but returns a 1,000 to 2,000 token distilled summary.
This is also how you keep retrieval cheap: an Explore-style subagent can grep aggressively because the misses cost its own window, not yours.
The failure mode to avoid is unscoped investigation in the main session, which every later turn re-reads; Claude Code’s context-window docs route research through a subagent precisely so the large file reads stay out of yours.
Packing and chunking: measure dumb before buying clever #
Whole-repo packing is legitimate but linear, so it is a budget decision, not a default.
Repomix packs a repo into one file with exact token counts and a --token-budget flag that fails CI, which makes “just give it everything” a measured choice rather than an accident.
Its limits are recorded in the note: spend grows linearly, attention is weakest exactly in the middle regions where the pack puts most content, and the tree-sitter --compress mode is experimental and lossy by design.
If you chunk for a vector store instead, the tree-sitter chunking note records practitioners ranking truncation and fixed-length splits above AST chunking, because 16k-token embedding models fit most whole files.
Framework machinery exists (LlamaIndex’s CodeSplitter, see the LlamaIndex note) but its own makers pivoted elsewhere, and the tools that shipped code retrieval built it by hand or stripped it back out.
Indexed retrieval pays only past a threshold #
Retrieval infrastructure is an enterprise-scale pattern, and the strongest evidence for that comes from the vendors selling it. The Sourcegraph note records their CodeScaleBench data: a +0.259 reward delta for code intelligence in the 400K to 2M LOC range, but a -0.080 delta below 400K, meaning their own tools slightly hurt agents on most repositories, behind a $16K floor. Augment Code makes the strongest counter-case, retrieval-first agents at 33% lower token spend at matched quality, and every number is vendor-run, which the note flags as the missing replication. Greptile applies the same graph-index idea to review, with the caveat that its index is post-commit and never sees your working tree. When you do cross the threshold, delivery is settled: an MCP server (Sourcegraph’s works with Claude Code, Codex, Cursor, and Amp) feeds the index to whichever harness you already run.
Compaction is a default you should steer #
Summarize-and-restart has shipped as a harness default, and its lossiness is steerable if you bother steering it.
Claude Code auto-compacts as you approach the limit, and Anthropic now documents what survives: the summary keeps your requests and key code snippets while detailed instructions from early in the conversation may be lost, CLAUDE.md and auto memory re-inject from disk, then Claude Code re-reads up to five of the files it read or edited in the session, choosing the ones modified most recently, and re-injects the bodies of any skills you invoked, capped at 5,000 tokens per skill and 25,000 in total with the oldest dropped first, and since v2.1.304 it also rebuilds the skill descriptions and re-injects those, closing the old gap where the listing did not reload.
The docs also name idle compaction now: a session you leave idle gets compacted on its own, and best-practices describes a past-200K session on the one-hour cache compacted shortly before the cache expires, so the summary you come back to after lunch is a steering target like any other.
All three major harnesses expose it manually: /compact <instructions> in Claude Code (for example, “focus on the API changes”), /compact in Codex (with /status showing context usage), and /compact, aliased /summarize, in OpenCode.
Claude Code goes further than most: the rewind menu summarizes from or up to a chosen checkpoint, and /btw answers side questions that never enter history at all, the same idea as Codex’s /side.
The steering mechanism most engineers miss: put compaction directives in the memory file (“when compacting, always preserve the modified-file list and test commands”), which turns a lossy default into a controlled one.
Claude Code also exposes the trigger itself now: run /autocompact with a token count, like /autocompact 500k, to set how full the window gets before the automatic pass runs.
Augment’s rebuild, recorded in its note, runs proactive compaction on a cheaper model and measured 53% lower cost per task, so compaction is also a cost lever, not only a capacity lever.
The caution is real: Anthropic warns overly aggressive compaction loses subtle context whose importance shows up later, which is the argument for files.
Files outlive sessions #
Anything you need after a compaction should already be on disk, because files are the only layer that survives restarts, compactions, and vendor switches.
Anthropic calls this structured note-taking: the agent writes NOTES.md-style files outside the window and reads them back later, which is how agents maintain coherence across hours.
The shipped version is the memory file: CLAUDE.md, AGENTS.md, and GEMINI.md load every session, and Claude Code’s /init and OpenCode’s /init scaffold them.
Their documentation also carries the warning worth repeating: bloated memory files cause the model to ignore your actual instructions, so prune ruthlessly and move rarely-needed knowledge into on-demand skills.
The automation of capture now has two designs: Gemini CLI’s Auto Memory (experimental) mines idle past sessions for durable facts and proposes memory patches and skill drafts into a review inbox, applying nothing without approval (see the Gemini CLI note for that harness’s status), while Claude Code now ships its own auto memory on by default, a per-project MEMORY.md index plus topic files of which the first 200 lines load every session, written by the model with no review gate and switchable off through /memory or the autoMemoryEnabled setting.
The design question is no longer hypothetical: I still treat review-gated capture as the correct design, because unreviewed auto-memory is how a wrong summary becomes a permanent fact, and the default-on Claude Code version is that failure mode with a toggle.
A workflow that composes the patterns #
Compose by phase: files for what persists, search for what changes, subagents for what is noisy, compaction for what is finished.
- Keep a short memory file (under a page) with commands, conventions, and landmines; everything else loads on demand.
- Start a fresh session per task (
/clearor OpenCode’s/new), because a clean window with a better prompt beats a long one full of corrections. - Let subagents do the exploration and
@-reference the few files you already know matter. - Compact at phase boundaries (after exploration, before implementation) with explicit instructions, instead of waiting for the auto-trigger mid-edit.
- Have the agent write durable facts (decisions, found constraints) to a notes file during long tasks, so the next compaction cannot erase them.
- If you run Claude Code, its auto memory is written by the model now: review
/memorymonthly and prune it the way you prune CLAUDE.md, because the model’s summary of you is context too. - Add an MCP context engine only when you pass the few-hundred-thousand-line threshold, and re-run its benchmark on your own repos first.
- Measure: watch context usage (
/status,/context), and track tokens per merged PR the way the model selection guide argues.
What changes fast and how to re-verify #
Compaction defaults and memory features are the fastest-moving surfaces in every harness, so every claim here carries a date. Gemini CLI’s Auto Memory is explicitly experimental and off by default, Claude Code’s auto memory and compaction-survival rules are newer and have both changed repeatedly across releases, and Augment’s efficiency numbers await independent replication. On each refresh I re-fetch the four harness pages cited below plus the context-engine notes, and I update this page and its date together. A pattern claim that no longer matches the docs gets deleted, not hedged.
What to Do Next #
- Learn your harness’s compact, clear, and subagent commands this week; they are free and you already pay for their absence.
- Add one compaction directive to your memory file naming what must survive summarization, then verify it survives on your next long session.
- Delete half of your memory file today; if behavior does not degrade, it was padding.
- Before buying any context engine, measure your current tokens per task and check your repo against the 400K LOC threshold with your own numbers.
- Try Repomix with
--token-budgetas a CI guard on the whole-repo habits you already have, so at least the dumb pattern is bounded.
Changes #
- 2026-08-24 - Published as queue item 3, an essay of pattern families grounded in harness documentation.
- 2026-09-24 - Replaced the verification preamble with a single as-of clause in the intro on owner request.
- 2026-09-29 - Link check: repointed the Codex slash-commands reference to learn.chatgpt.com/docs/reference/slash-commands after developers.openai.com began redirecting; every other cited page fetched 200.
- 2026-10-04 - Updated the compaction-survival claim to Anthropic’s current enumeration (most recently modified files plus re-injected invoked-skill bodies, replacing the older accessed-files list) and recorded Claude Code shipping auto memory on by default as the unreviewed counterpart to Gemini CLI’s review-gated inbox, with a monthly /memory pruning step added to the workflow.
- 2026-10-10 - Re-verified every pattern claim against the live harness docs with the four cited pages plus Anthropic’s engineering post all fetching 200, and folded in two current-doc details: the re-injected skill bodies are capped (5,000 tokens per skill, 25,000 in total) and Claude Code’s auto-compact threshold is now settable with /autocompact.
- 2026-10-23 - Rotation re-verification of all eight cited pages: corrected the compaction-survival claim, Claude Code rebuilds and re-injects skill descriptions since v2.1.304 where the essay said the listing did not reload, and named the autoMemoryEnabled setting beside /memory as the auto-memory off-switch; every other pattern claim re-verified against the live docs, and the intro as-of re-dated.
- 2026-10-28 - Rotation re-verification of all eight cited pages: every one fetched 200, the compaction, subagent, and auto-memory claims re-checked against the live docs with nothing to correct (the docs now also state that the summary inherits the session’s thinking setting and that an over-5,000-token file returns as a path reference, refinements the essay’s claims survive), and the intro as-of re-dated.
- 2026-10-31 - Rotation re-verification of all eight cited pages plus the how-claude-code-works page: every one fetched 200, and three stale Claude Code documentation attributions replaced with the rewritten docs’ current wording, the retired “the context window is the most important resource to manage” line and the removed “infinite exploration” anti-pattern no longer exist on the live pages (whose best-practices guide now argues focused context for answer quality rather than window scarcity), and the compaction-survival enumeration tightened to the summary-keeps-requests-and-key-code-snippets claim the current how-claude-code-works page states; every other pattern claim re-verified against the live docs, and the intro as-of re-dated.
- 2026-11-01 - Link check: all nine cited pages fetched 200, the pattern claims holding from the 2026-10-31 rotation pass, and the intro as-of left at 2026-10-31 since no fact moved.
- 2026-11-03 - Rotation re-verification of all nine cited pages: every one fetched 200, two compaction claims corrected against the rewritten context-window page (the five-file re-read now names the session’s read-or-edited files chosen by recency, and the skill caps gained the oldest-dropped-first detail), idle compaction folded in as a named feature (a session left idle compacts on its own, and best-practices describes the past-200K one-hour-cache session compacted shortly before the cache expires), and the intro as-of re-dated.
- 2026-11-04 - Link check: all nine cited pages fetched 200, the compaction-survival, skill-cap, idle-compaction, and v2.1.304 claims re-checked against the live context-window page with nothing to correct, and the intro as-of left at 2026-11-03 since no fact moved.
- 2026-11-05 - Link check: all nine cited pages fetched 200, the compaction-survival, skill-cap, idle-compaction, v2.1.304, and /autocompact claims re-checked against the live context-window page with nothing to correct, and the intro as-of left at 2026-11-03 since no fact moved.
- 2026-11-07 - Link check: all nine cited pages fetched 200, the compaction-survival, skill-cap, idle-compaction, v2.1.304, and /autocompact claims re-checked against the live context-window page with nothing to correct, and the intro as-of left at 2026-11-03 since no fact moved.
- 2026-11-08 - Link check: all nine cited pages fetched 200, the compaction-survival, skill-cap, idle-compaction, v2.1.304, and /autocompact claims re-checked against the live context-window page with nothing to correct, and the intro as-of left at 2026-11-03 since no fact moved.
See also #
- Model Selection for Coding Tasks - the other half of the economics: why harness overhead moves your bill as much as model choice
- Repomix - the packing pattern measured, budgeted, and bounded
- Augment Code - the strongest vendor case that retrieval beats exploration
- Sourcegraph code context platform - the size threshold below which indexed retrieval hurts
- Semantic code search in coding tools - why tools are retreating from indexes toward agentic search
References #
https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents - context rot, attention budgets, compaction mechanics, structured note-taking, sub-agent summaries, just-in-time retrieval
https://code.claude.com/docs/en/best-practices - Claude Code auto-compaction, /compact with instructions, /clear, /btw, memory-file pruning, compaction directives
https://code.claude.com/docs/en/memory - Claude Code auto memory (on by default, first 200 lines or 25KB of MEMORY.md per project, /memory toggle and autoMemoryEnabled setting) and the note that project CLAUDE.md is re-read after compaction
https://code.claude.com/docs/en/context-window - the compaction-survival documentation: summary contents, up to five of the session’s read-or-edited files re-read by recency, invoked-skill bodies re-injected with the 5,000 and 25,000 token caps and oldest-dropped-first, skill descriptions rebuilt and re-injected since v2.1.304, and the named idle-compaction behavior (fetched 2026-10-23, 2026-10-31, and again 2026-11-03, the page now organizing survival per mechanism and naming idle compaction)
https://code.claude.com/docs/en/how-claude-code-works - the when-context-fills-up section whose summary claim (requests and key code snippets kept, detailed early instructions may be lost) the compaction paragraph now cites (fetched 2026-10-31)
https://learn.chatgpt.com/docs/reference/slash-commands - Codex /compact, /status context usage, and /side (the developers.openai.com URL redirects here)
https://opencode.ai/docs/tui - OpenCode /compact (alias /summarize), /new (alias /clear), and @ file references
https://raw.githubusercontent.com/google-gemini/gemini-cli/main/docs/cli/auto-memory.md - Gemini CLI Auto Memory: review-gated extraction of memory patches and skills from past sessions
https://arxiv.org/abs/2307.03172 - Lost in the Middle: recall degrades for information in the middle of long contexts