How can I reduce the context my coding agent consumes without hiding information it needs?

Noor consults a book beside an office bookshelf while Ivo studies a folder, selecting the context needed for their work.

TL;DR

Agent context fills with instruction files loaded every turn, broad file reads, long test output and rules for code you are not touching. Measure with the tool's own breakdown, then cut in order of risk: shorten always-loaded instructions, scope other rules to paths and check what that scoping selects, filter output to what matters, and send broad exploration to a subagent. Back must-hold rules with checks, and judge the change on completed work rather than token counts alone.

Fewer tokens without more mistakes

In May 2026 a Codex user on a paid plan asked on Hacker News for ways to extend their sessions by reducing the tokens consumed; they had already cleaned up their AGENTS.md, and it did not seem to help.

In August 2026 another Codex user asked on the OpenAI Developer Community how others manage the context window in long sessions that use many skills and agents. They added a point worth keeping: they were not trying to squeeze out every token, and more context was a reasonable trade if it meant fewer mistakes.

Find out what is actually filling the context

Guessing is how people end up trimming the file that was not the problem. Each tool shows a breakdown:

  • Claude Code: /context shows a live breakdown of context usage by category, including which CLAUDE.md and memory files loaded (context window).
  • Codex: /status shows the session's token usage and remaining context (slash commands).
  • Cursor: the context ring next to the prompt opens a breakdown by category, including rules, skills, MCP, conversation and summarized conversation (prompting documentation).

Rules loaded for code you are not working on are a common surprise. In July 2026 a developer with hundreds of nested AGENTS.md files reported on the Cursor forum that starting at the repository root pulled in roughly 600,000 tokens of rules against a 300,000-token window, triggering repeated summarization. A staff reply said AGENTS.md files are always-applied and that since version 3.6 Cursor discovers nested ones across the whole repository, and suggested opening the subfolder you work in, converting nested files into glob-scoped rules, or keeping one small root file. That description conflicts with Cursor's rules page, which says nested files apply when working in their directory, so check the breakdown in your own version rather than relying on either.

Illustration of Noor staring at her laptop in disbelief with a palm pressed to her cheek while Mira, holding a mug, leans over her shoulder to look at the screen.
Rules loaded for code you are not touching are a common surprise, so measure what fills the context before you cut anything.

Shrink what loads on every turn

Always-loaded instructions are paid for on every request, so they are the first place to look:

  • Keep them short. Claude Code's documentation targets under 200 lines per CLAUDE.md and notes that longer files reduce adherence as well as consuming context. Its best practices give a useful test for each line: would removing it cause the agent to make mistakes? If not, cut it. Imports help organize a long file but do not reduce its cost, because imported files load at launch too.
  • Move procedures out. Multi-step procedures and rules for one part of the codebase belong in skills or path-scoped rules, which load when needed rather than every turn (Claude Code costs). Codex's best practices likewise advise keeping AGENTS.md short and pointing to task-specific files.
  • Watch the cap. Codex stops adding instruction files once they reach project_doc_max_bytes, 32 KiB by default, and its troubleshooting advice is to raise the limit or split files so critical guidance stays intact (Codex AGENTS.md guide). Past the cap, trimming is not optional; something is already being dropped.
  • Know what is always on. Cursor applies CLAUDE.md to every conversation regardless of its settings (Cursor rules help), so a long CLAUDE.md costs in Cursor as well as in Claude Code.

Scope rules to paths, then check what the scoping selects

Moving rules from always-on to path-scoped gave the largest reduction in the test below, and it is also the easiest way to hide a rule. To see both effects, Paqad's pinned rule compiler and selector were run against the rule tree in Paqad's own repository at commit 29548baf, which compiles to 44 rules.

Two lessons follow for any tool. Write scoping explicitly, as globs your tool documents, rather than relying on inference. And check what was selected for real changes: Claude Code's /context and InstructionsLoaded hook show which files loaded, and Paqad's rules-loaded.json lists each applicable rule with the paths that matched it. Paqad's documentation also states that script-enforced rules run at its checkpoints whether or not their text was loaded, which is the safeguard for a rule the selection misses. If you want that record for changes against your own rule tree, Paqad's planning documentation describes how it selects and records rules per change.

Bar comparison of rule text loaded by path-scoped selection for four changes against loading every rule, with a note that the React change did not load the React conventions.
Path-scoped loading cut the rule text substantially, but one change missed the rules it most needed.

Cut output, not information

Command output is often the largest avoidable cost; change what reaches the model rather than what exists:

  • Filter test and log output to failures. Claude Code's costs page includes a pre-tool hook example that rewrites test commands to show only failing output, and notes that a hook grepping a long log for errors can shrink tens of thousands of tokens to hundreds. The full output still exists if the agent needs it.
  • Send verbose work to a subagent. Claude Code and Codex both document subagents for exploration, test runs and log triage, returning a summary to the main session. Codex's subagents page names the problems this avoids, context pollution and context rot, and also notes that subagent workflows use more tokens overall (Codex subagents).
  • Let search find files instead of pre-loading them. Cursor's January 2026 best-practices post advises @-mentioning a file only when you know it matters and otherwise letting the agent search.

Know when to fetch more, and when to start fresh

A smaller context is only safe if the agent can get back what it needs. Anthropic's context-engineering guidance puts the balance well: aim for the smallest set of high-signal tokens, but minimal does not have to mean short, and overly aggressive compaction can lose subtle context that turns out to matter later (Effective context engineering for AI agents).

In practice that means two habits. Have the agent re-read a file before editing it rather than rely on a summary of it; Claude Code's compaction table notes that path-scoped rules and nested instruction files come back only when a matching file is read again. And prefer a fresh session for a new unit of work over compacting the same one repeatedly, while staying in one session for one coherent task, which Codex's best practices say preserves the reasoning trail. What to write down so the fresh session continues accurately is set out in the article on what to save for a new AI coding session.

Judge the result on completed work

A smaller context that produces more rework is not a saving. When you change what loads, compare the same kind of task before and after: did the change pass the project's checks the first time, how many corrections did it need, and was any rule violated that used to hold? Token usage is one line in that comparison, not the conclusion. For the spending side, the article on controlling AI coding costs when agents keep retrying covers caps and model choice.

Frequently Asked Questions

Cut the largest category in the breakdown first, and pair every cut with the safeguard that would catch what it hid: a loading record for scoped rules, the full output kept on disk for filtered logs, and a check in CI for any rule that must hold everywhere.

Usually a long always-loaded instruction file, rules for parts of the repository you are not touching, verbose test and build output, many skill or tool descriptions, and a long conversation history. Past Codex's 32 KiB instruction cap, the question changes from waste to loss: something is already being dropped.

Keep repository-wide rules in the always-loaded file, scope the rest with explicit globs, and test the selection with a few real changes, including one in each area whose conventions matter most. The React miss above would have shown up on the first such test.

Before editing a file it has only seen through a summary, when a task reaches code it did not read earlier, and after compaction, when path-scoped rules may not return until a matching file is read again.

Keep the model and the kind of task the same on both sides, and compare several tasks rather than one, because single runs vary. If tokens fall while corrections or rule violations rise, the reduction hid something the agent needed.