Context engineering for coding agents: what goes in every turn
Context engineering is deciding what the model sees on each turn. For a coding agent that means three piles: what loads every turn (a short core of rules and facts), what loads on demand (notes about one project or one kind of task), and what never needs to load. Getting the piles right cuts tokens and keeps the model’s attention on the code in front of it.
Updated 2026-11-27
What context engineering means#
Prompt engineering is how you word a request. Context engineering is everything else the model reads with it: instructions, memory, tool descriptions, files and the history of the session. In a coding agent that is most of the text, and it is sent again on every turn.
More context is not better. A model reading ten pages of notes about other projects answers worse and costs more than one reading the one page it needs.
A layout that works: always-on and on-demand#
| Pile | What goes in it | When it loads |
|---|---|---|
| AlwaysOnMemory | Who you are, the rules every task follows, where things live. | Every turn. Keep it short. |
| OnDemandMemory | One file per project, tool or kind of task, each with a one-line hint in the index. | Only when the task matches the hint. |
| Nothing | Old session logs, finished work, notes repeated from the code or git history. | Never. Delete it or archive it. |
The hint is the important part: it tells the agent when to open a file, not what is in it. A good hint reads like "before any deploy" or "when touching the billing code".
Rules of thumb#
- One fact, one place. A rule written in three files is read three times and drifts.
- Write the why once, short. One line of reason helps the model apply a rule; a story does not.
- Do not copy the code. Anything the agent can read from the repo or git does not belong in memory.
- Stable first. Keep text that never changes at the top so the provider can cache it.
- Check for drift. Memory grows by one line at a time; look at the always-on size every so often.
Doing it automatically#
MinniMemoryMCP applies this layout to Claude Code and Codex memory and keeps it in shape. On our test memory the always-loaded part went from 12,511 to 6,699 tokens. With MinniMemoryMCP 1.6.0 on our test memory, the agent went looking in its memory 17 times instead of 89 over the same 87 answers, and the run used 47.2% fewer tokens.
$ npm i -g minnimemory
$ minnimemory installRelated: How to fix a CLAUDE.md that is too long, What to do when the Claude Code context window is full and Token optimization for AI agents.