Blog

Context engineering for coding agents: a practical playbook

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.

67.7%
of spend is re-reading cached context
72.3%
less on a 938-turn session through condense
+15.2%
cost per pass with rtk, one Terminal-Bench run

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).

LayerLoadedYour lever
System prompt and built-in toolsEvery requestChoice of agent
MCP tool definitionsEvery requestWhich servers are connected
CLAUDE.md / AGENTS.mdEvery requestLength and scope
File contentsWhen readWhat and how much the agent reads
Tool resultsWhen runOutput size, clearing old results
Conversation historyGrows every turnCompaction, /clear, subagents
Memory filesOn demandWhat 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.

Terminal-Bench cost, with and without rtk
Single run
no tool
$25.46
rtk
$29.33
rtk filters shell output, yet solved 75% vs 81.5% and cost more per pass. Measure cost per task, not output size. From the Terminal-Bench run.

4. History: subagents, compaction and memory files

  1. 1
    Subagents 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.

  2. 2
    Compact at task boundaries, with instructions

    Say what to keep: files changed, failing tests, rejected approaches. Switching tasks? /clear. See /compact vs /clear.

  3. 3
    Keep 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.

CommandWhat it shows
/contextWhat fills the window
/costSession spend on an API key
dense infoTokens sent against tokens forwarded through condense

Run dense claude after installing the CLI to try it.

Context engineering checklist

WhenDo this
Once per repoWrite a short CLAUDE.md or AGENTS.md; scope MCP servers to the project
Start of a taskFresh session, two-sentence brief, plan file for anything multi-step
During the taskTargeted reads, quiet tool output, subagents for broad searches
At a milestoneUpdate the plan file, commit, /compact with instructions
Switching tasksHandoff note, /clear
MonthlyRe-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.

All posts