Use context engineering to keep your coding agent's context window small and on task
Context engineering is the work of choosing what goes into an agent's limited context window at each step: instructions, tools, files, history and notes. In a coding agent, measure what fills the window first, then cut the always-loaded instruction file to the rules every task needs. Move procedures into skills or rules that load on demand, turn off MCP servers you do not use, and send noisy searches to subagents. Keep progress in a notes file and clear the session between unrelated tasks.
By the Agents Handbook editors · CC BY 4.0
Your agent starts each session with a short instruction file, only the tools it uses, on-demand skills for long procedures, and a notes file that carries progress across sessions. You know what fills the window because you measured it.
- Difficulty
- Intermediate
- Time
- Varies
- Sources
- 4 · 4 independent
- Updated
- 2026-11-12
- Cost:
- Included in your Claude Code or Codex plan. Subagents and /compact send their own requests, which count toward the same usage limits.
The prompt
Fill the blanks here, or leave them for your agent to ask. Pick the tab for your agent. The hand-off button gives any agent this page as markdown.
Help me apply context engineering to this repository. The goal is a smaller, sharper context for every session. Do not change product code. 1. Measure. Report what fills your context window right now: instruction files, memory, MCP servers and their tools, skills, and conversation history. Give the size of each item if you can see it. List the five biggest items. 2. Instruction file. Read [CLAUDE.md / AGENTS.md]. Sort every line into one of four groups: - Keep: build, test and lint commands, and rules you would not guess from the code. - Move to a skill: multi-step procedures for one kind of task, such as releases, migrations or PR reviews. - Move to a scoped file: rules that apply to one directory or file type. - Delete: repository overviews and anything you can read from the code. Also flag lines that contradict each other. Show me the sorted list. Do not edit anything yet. 3. Tools. List each MCP server and tool, and which recent tasks used it. Suggest which to turn off, and which jobs a CLI I already have (such as gh) could do instead. 4. Notes. Propose a notes file at [docs/agent-notes.md] where you will record decisions, open bugs and next steps at the end of each milestone. 5. Wait for my approval. Then make only the approved edits, show the diff, and stop. Rules: keep the main instruction file under 200 lines. Never delete a rule I did not approve. Do not commit.
Human checkpoints: the agent stops and asks you
- Review the diff of every instruction file, skill and MCP config change before it is committed.
- Approve the list of MCP servers and tools to turn off before the agent changes the config.
The agent edits only instruction and config files. The main risk is cutting a rule the agent needs. Review every change to CLAUDE.md, AGENTS.md, skills and MCP config before you commit it.
What you need
- Claude Code or Codexapp
- A repository with an instruction file (CLAUDE.md or AGENTS.md)data
If you have none, run /init in either agent to draft one, then trim it with this playbook.
- Permission to edit instruction files and agent configpermission
Steps
Measure what fills the window
In Claude Code, run /context to see what uses space, and /context all to see which memory files loaded. In Codex, run /status to see the remaining context capacity. Write down the biggest items before you change anything, so you can compare later.
Check: You have a short list of the largest items in the window, with sizes where the agent shows them.
Cut the instruction file to what every task needs
Claude Code loads CLAUDE.md in every session, and its docs suggest under 200 lines per file. Keep build and test commands and rules the agent would not guess. Delete repository overviews: a 2026 study of AGENTS.md files found that context files did not generally raise task success, raised inference cost by over 20% on average, and that overviews in them did not help. Imports with @path do not save space, because imported files load with the file that imports them.
Check: The main instruction file holds commands and non-obvious rules, and nothing the agent can read from the code.
Move procedures to files that load on demand
In Claude Code, put a multi-step procedure in a skill at .claude/skills/<name>/SKILL.md. Its body loads only when you or Claude invoke it. Put rules for one part of the codebase in .claude/rules/ with a paths field, so they load only when Claude touches matching files. In Codex, put rules for one directory in a nested AGENTS.md there, since Codex joins files from the root down to your working directory.
Check: The release, migration or review procedure lives in its own skill or nested file, and the main file no longer contains it.
Turn off tools you do not use
In Claude Code, run /mcp and disable servers you are not using. Its docs say CLI tools such as gh, aws and gcloud use less context than MCP servers because they add no tool listing. In Codex, set enabled = false on a server in config.toml to turn it off without deleting it, or limit its tools with enabled_tools or disabled_tools.
Check: /context or /mcp shows only the servers your current work needs.
Send noisy work to subagents and hooks
In Claude Code, ask for a subagent when a search, test run or log read would flood the conversation. The subagent works in its own context window and returns a summary. For output you always trim, a PreToolUse hook can cut it before Claude reads it; the docs show a hook that keeps only the last 100 lines of test output.
Check: After a broad search, the main conversation holds only the subagent's summary.
Keep progress in a notes file
Ask the agent to write decisions, open bugs and next steps to a file such as docs/agent-notes.md at each milestone. Anthropic describes this structured note-taking pattern for long tasks: the agent reads its notes back after a reset. Claude Code also keeps its own auto memory and loads the first 200 lines or 25KB of it each session.
Check: The notes file exists, is under version control or a known path, and a fresh session can pick up the work from it.
Clear between tasks and compact with a focus
Start a fresh context when you switch to unrelated work: /clear in Claude Code, /new in Codex. When you need continuity, compact with instructions, for example /compact Focus on the failing tests and the API changes. Claude Code's docs note that /compact is itself a large request on a long conversation, while /clear costs nothing.
Check: Each new task starts with a clean window, and long tasks continue from a compacted summary plus the notes file.
Measure again and test one real task
Run /context or /status again and compare with your first numbers. Then run one normal task and check that the agent still follows your key rules. On a paid Claude plan, /usage also shows what share of recent usage went to skills, subagents and each MCP server.
Check: The window starts smaller than before, and the agent still runs the right build and test commands without being told.
What goes wrong
Claude Code ignores the rules in AGENTS.md.
Fix: By default Claude Code reads AGENTS.md only when there is no CLAUDE.md in your working directory or above it. If both exist, it reads only CLAUDE.md. Import AGENTS.md from CLAUDE.md with @AGENTS.md, or change the Project instructions setting.
A detail you needed disappears after compaction.
Fix: Write decisions and open bugs to the notes file before you compact, and pass focus instructions to /compact. Anthropic warns that compaction that is too aggressive can drop subtle context whose value shows up later.
The instruction file keeps growing, and the agent follows some rules and not others.
Fix: Claude Code's docs say longer files reduce adherence and that Claude may pick one of two contradictory rules at random. Run /doctor prompt-audit to find conflicts and stale references, then move procedures into skills.
One MCP server takes a large share of the window even when you do not use it.
Fix: Check /context, then disable the server or the heavy tool, or use the vendor's CLI instead. One Hacker News commenter found a Supabase server using 13.2k tokens until they disabled one tool.
Codex drops part of a long AGENTS.md without an error.
Fix: Codex stops adding instruction files once the combined size reaches project_doc_max_bytes (32 KiB by default). Split rules into nested AGENTS.md files near the code, or raise the limit in config.toml.
Usage goes up after you start using subagents.
Fix: Subagents send their own requests against the same limits. Use them for searches and logs that would flood the conversation, and choose a smaller model for simple subagent tasks, as Claude Code's docs suggest.
Evidence
Two or more public accounts from different people or sites describe the same job. We summarise each source in our own words. Open the original before you rely on it.
- Blog2025-09-29· Claude CodeEffective context engineering for AI agents
Anthropic says Claude Code loads CLAUDE.md up front and uses grep and glob to pull files in only when needed. When it compacts, it keeps a summary plus the five most recently accessed files. Subagents explore in their own windows and return condensed summaries. These are the vendor's own reports.
Anthropic Applied AI team · anthropic.com - Hacker News2026-03-16· Claude CodeComment on Apideck CLI, an agent interface with lower context use than MCP
The commenter tracked MCP context use with Claude Code's /context command. The Supabase server used 13.2k tokens all the time, 8k of it from one search_docs tool. After they disabled that tool, they report the server uses about 300 tokens when idle.
consumer451 · news.ycombinator.com - Hacker News2026-09-21Comment on MCP was always a bad idea?
The commenter says the Jira and GitLab MCP servers used too many tokens, so they installed the matching CLIs and let the coding agent use those instead.
ex1fm3ta · news.ycombinator.com - Blog2025-12-21· Claude CodeContext engineering with Claude Code
The author helps teams set up Claude Code. He says engineers copy prompts between conversations and lose context when they switch tasks. He sorts the setup into agents, skills, slash commands, hooks and MCP servers, and explains when to use each.
Thomas Bentkowski · tbtki.com
- How Claude remembers your project (CLAUDE.md, rules, AGENTS.md, auto memory)checked 2026-11-12
- Claude Code costs and context management (/context, /compact, /clear, MCP, skills, subagents, hooks)checked 2026-11-12
- Claude Code skillschecked 2026-11-12
- Claude Code subagentschecked 2026-11-12
- Custom instructions with AGENTS.md (Codex)checked 2026-11-12
- Codex CLI slash commands (/status, /compact, /new)checked 2026-11-12
- Codex Model Context Protocol (enabled, enabled_tools, disabled_tools)checked 2026-11-12
- Evaluating AGENTS.md, are repository-level context files helpful for coding agents?checked 2026-11-12
Report how it went
Every report makes this page better for the next person and the next agent. Reports are reviewed before they count. Agents can report with the report_run tool.