Context engineering is deciding what goes into a model’s context window at each step, and what stays out: instruction files, retrieved code, tool output, history and memory. The goal is the smallest set of tokens that lets the agent take the right next step, because less context costs less every turn and keeps the model focused.
What context engineering means for coding agents
Each step the agent re-sends its whole context, and that is what the model reasons over and what you pay for. Anthropic’s post on effective context engineering frames it as finding the smallest set of high-signal tokens; too much and the agent gets slower, costlier and worse (context rot).
| Layer | Loaded | Your lever |
|---|---|---|
| System prompt and built-in tools | Every request | Choice of agent |
| MCP tool definitions | Every request | Which servers are connected |
| CLAUDE.md / AGENTS.md | Every request | Length and scope |
| File contents | When read | What and how much the agent reads |
| Tool results | When run | Output size, clearing old results |
| Conversation history | Grows every turn | Compaction, /clear, subagents |
| Memory files | On demand | What you write down |
1. Instruction files: small, specific, always true
CLAUDE.md and AGENTS.md load at the top of every request: the most reliable place for rules, the most expensive place for prose.
- Keep build and test commands, conventions the agent gets wrong, hard constraints.
- Cut anything the agent can learn by reading the code.
- Scope directory rules to a CLAUDE.md in that directory, loaded only when Claude works there. Full checklist in MCP and CLAUDE.md overhead.
2. Retrieval vs reading: load code just in time
Search with grep and glob, read the function that matters, move on. Whole directories up front are paid for on every later turn.
- Point the agent at the right directory or name the function.
- Keep a short repo map in CLAUDE.md so it knows where to look.
- Ask for line ranges. A file path is a few tokens; the file can be thousands.
3. Tool results: the biggest and noisiest layer
Failing tests only, tail on logs, quiet builds, scoped searches. In your own agent, clear old results once acted on; both Anthropic and OpenAI APIs now offer options for that. More in tool output is eating your context.
4. History: subagents, compaction and memory files
- 1Subagents isolate exploration
Anything that reads a lot to produce a little. The main session gets a paragraph, not thirty file reads that are re-sent for the rest of it.
- 2Compact at task boundaries, with instructions
Say what to keep: files changed, failing tests, rejected approaches. Switching tasks?
/clear. See /compact vs /clear. - 3Keep state in memory files
A plan with checkboxes, a notes file, a handoff document. They survive compaction and
/clear; update them after each step.
5. A compression layer for what is left
What remains in a long session is content the model already used, and it is re-read every turn. condense trims new content before it is cached and rewrites finished loops, keeping paths, identifiers, errors and code, without breaking the cache.
Measure what your context costs
Judge changes by cost per finished task: saving tokens but adding turns is not a saving.
| Command | What it shows |
|---|---|
/context | What fills the window |
/cost | Session spend on an API key |
dense info | Tokens sent against tokens forwarded through condense |
Run dense claude after installing the CLI to try it.
Context engineering checklist
| When | Do this |
|---|---|
| Once per repo | Write a short CLAUDE.md or AGENTS.md; scope MCP servers to the project |
| Start of a task | Fresh session, two-sentence brief, plan file for anything multi-step |
| During the task | Targeted reads, quiet tool output, subagents for broad searches |
| At a milestone | Update the plan file, commit, /compact with instructions |
| Switching tasks | Handoff note, /clear |
| Monthly | Re-check /context on a fresh session and trim what crept in |
Frequently asked questions
What is the difference between context engineering and prompt engineering?
Prompt engineering words one instruction well. Context engineering manages everything the model sees across a long task, and how it changes turn by turn.
How do I do context engineering in Claude Code?
Short CLAUDE.md, only the MCP servers you need, subagents for exploration, small tool output, /compact with instructions at task boundaries, and regular /context checks.
Should a coding agent read the whole codebase into context?
No. Search just in time with grep, glob and targeted reads, and keep pointers like file paths in context instead of content.
Where should agent memory live?
In files. A plan or handoff note survives /clear and compaction and a human can check it.
Does context engineering save money?
Yes. The agent re-sends its whole context every turn, so every token you keep out is one you do not pay to re-read.