Context in practice For engineers
Context engineering for AI coding: what to load, keep and drop
Context engineering is choosing which tokens a coding agent sees at each step: instruction files, a task brief, run-time retrieval, tools, handoffs and a verification check. It goes beyond writing a prompt. For one engineer on one repository, built-in features are often enough.
What is context engineering for AI coding agents?
Context engineering is the practice of choosing which tokens a coding agent sees at each step, so the model has what it needs to do the task and little else. Anthropic's engineering team defines context as the set of tokens included when sampling from a model, and context engineering as optimizing the utility of those tokens against the inherent constraints of LLMs.
The guiding question in that article is what configuration of context is most likely to produce the behavior you want. For a coding agent, the configuration is concrete: the instruction files it loads, the task brief you write, the files and search results it pulls in, the tools it can call, and the checks it can run.
Prompt writing is one part of that work. A prompt is a single input you compose once. Context engineering covers everything in the window during a long session, including tool output and earlier turns, and it is iterative because you decide again what to pass on at every step. Source: Anthropic, published 29 September 2025.
Treat the window as a budget. Every token competes for the model's attention, so a file, a search result or a tool description earns its place only if it changes what the agent does next. The sections below take the layers in the order a change usually needs them, and each one names what to load, what to keep between steps, and what to drop.
Three failure modes show up repeatedly in coding sessions. The agent lacks a constraint that lives outside the code. It carries too much, so the relevant line is buried. Or it loses a decision when the session is cleared or compacted. Each layer below addresses one or more of these, and none of them depends on a particular vendor.
| Aspect | Prompt writing | Context engineering |
|---|---|---|
| Unit of work | One instruction or system prompt | The whole set of tokens in the window across a session |
| When you do it | Before the agent starts | Before, during and between sessions |
| Typical question | How should this request be worded? | What should the agent load now, keep, or drop? |
| Example artifact | A task prompt | A CLAUDE.md, a task brief, a search tool, a handoff note, a test command |
| Failure you see | The agent misreads the request | The agent misses a constraint, repeats a rejected approach, or drowns in noise |
What belongs in persistent instructions such as CLAUDE.md and AGENTS.md?
Persistent instructions hold the facts an agent should know in every session and cannot learn from reading the code: commands, conventions, layout and standing rules. Claude Code reads CLAUDE.md files at the start of every session, and Anthropic's memory documentation says to keep them to those facts and to move multi-step procedures or single-area guidance into skills or path-scoped rules.
Two limits matter. Claude Code treats these files as context, not enforced configuration, so a rule that must never be broken belongs in a hook. And Anthropic's guidance on prompts applies to the files too: aim for the right altitude, specific enough to guide behavior and flexible enough not to hardcode brittle logic, and start minimal before adding lines for failures you observed.
If your repository uses AGENTS.md, Claude Code v2.1.277 or later reads it by default only when no CLAUDE.md or CLAUDE.local.md exists in your working directory or above it. A CLAUDE.md can import it with @AGENTS.md. Verified against the memory documentation on 18 November 2026.
Treat these files as the cheapest layer to improve and the easiest to let rot. Anthropic's documentation suggests adding a line when the agent makes the same mistake a second time, when a code review catches something it should have known, or when you type the same correction you typed last session. Delete a line when the code or process it describes changes. A stale instruction costs tokens in every session and can contradict a newer one.
What should a task brief contain?
A task brief is the short, per-change statement of what to do, why, and what must stay true. It is the layer instruction files cannot cover, because the facts change with every task. Claude Code's best-practices documentation lists missing information the agent cannot get from the repository as the first of three common reasons a session goes wrong.
Write the brief so someone who has never seen the ticket could act on it. Name the goal, the files or modules involved, the constraints that come from outside the code, and what done means in a form the agent can run.
Keep the brief short enough to read in a minute. Constraints that come from decisions are the most valuable lines, because the agent cannot derive them. Add the source of each one, such as a ticket, an incident note or a review thread, so the agent and a reviewer can check that the constraint still applies. A constraint with no source is a rumor the agent may follow too literally.
Do not paste the whole ticket history. Summarize what changed the plan and link the rest. The agent can open a linked file or search for it when needed, which keeps the starting window small.
How much context to give an LLM
- Goal: the behavior change, in one or two sentences.
- Scope: the files, modules or endpoints that are in and out.
- Constraints: decisions and limits that the code does not show, with where each came from.
- Rejected approaches: what was tried or ruled out, and why.
- Done means: the command or check that passes when the change is correct.
How should the agent find context at run time?
Let the agent keep lightweight pointers and load detail when it needs it, instead of pasting everything up front. Anthropic describes this as just-in-time retrieval: the agent holds file paths, queries or links and loads data at run time with tools. It also describes a hybrid strategy, with some context loaded up front for speed and the agent exploring further as needed, and cites Claude Code as an example.
Tools are part of the same budget. Anthropic's guidance is to keep tools self-contained, clear and token-efficient, with minimal overlap, because a bloated tool set creates ambiguous choices. In Claude Code, the Model Context Protocol (MCP) connects external tools and data sources such as issue trackers, databases and monitoring dashboards. Each server you add widens what the agent can pull in and what it can mix up, so connect the sources a task needs, not every source you have.
Retrieval has its own failure mode. A search result is evidence the agent has to judge, not a fact it can trust by default, and the amount you retrieve is a trade-off, not a maximum.
A practical split follows from this. Load up front only what every step needs: the task brief and the standing instructions. Leave the rest as pointers: a path to the module, a name to search for, a link to the design note. When the agent opens a file, ask whether you would have wanted that file in the window from the first message. If not, it is retrieved at the right time.
Claude Code's MCP documentation lists tool search as on by default. Check your own session rather than assuming how much each connected server adds: run /context all and look at what loaded. Verified against the MCP documentation on 18 November 2026.
What survives compaction and handoffs?
Only what is written to disk or restated in the next context survives. Claude Code compacts a conversation automatically before the context window fills. Per its memory documentation, a project-root CLAUDE.md is re-read from disk after /compact, nested CLAUDE.md files and path-scoped rules load again on demand, and an instruction given only in conversation can disappear.
Anthropic names three techniques for long tasks: compaction, which summarizes a conversation near its limit and continues in a new window; structured note-taking, where the agent writes notes outside the window and reloads them later; and sub-agents that work in clean windows and return condensed summaries. For a human or agent handoff, the practical form is a short note that lists decisions made, constraints found, and the next step, written into a file rather than left in chat.
Between unrelated tasks, Claude Code's documentation recommends /clear, which costs nothing and stops the next task from carrying the previous one's history.
The decisions most likely to be lost are the ones made in conversation: why an approach was rejected, which constraint ruled out a library, what the next engineer should not retry. Write them to a file when they are made, in a form that survives a clear. A handoff note of five lines that names the decision, the reason and the source is more useful to the next session than a long transcript.
How does verification fit into context?
Verification is the part of the context that tells the agent whether it is finished. Claude Code's best-practices documentation says an agent stops when work looks done, and that giving it something that returns pass or fail lets it make a change, run the check and fix what broke without you watching.
The check can be a test file, a build exit code, a linter, a script that diffs output against a fixture, or a browser screenshot beside a mockup. The documentation notes that if a repository already has one, the single most useful line in CLAUDE.md is the command that runs it. For review, it describes /code-review, which reviews a diff in a fresh subagent that never saw your conversation.
Verification also protects the rest of the context. An agent that can run the check discards a wrong guess in the same session, so the wrong guess does not leak into your next prompt. Choose the strictest check that fits the task. A prompt that says to run the auth tests until they pass works today with no setup, and a Stop hook enforces the check at the harness level, as the best-practices page describes.
Example (fictional): preparing context for one change
Example (fictional): a four-person team keeps a billing service. The task is to add a retry to the invoice export job. The engineer prepares the layers before starting the session. Names and numbers below are invented for illustration, and no results are claimed.
The pattern is the point: each fact goes in the layer that loads it at the right time, and the brief says where each constraint came from so the agent can check it.
The brief above is the artifact to copy. It is short, it cites where each constraint came from, and it ends with a check and a handoff line. The engineer starts a fresh session with it, lets the agent search for the scheduler config, and reviews the notes file at the end.
# Task brief: retry for the invoice export job (fictional)
Goal: retry transient failures in the nightly export, at most 3 attempts.
Scope: src/export/ and its tests. Do not change the scheduler config.
Constraints:
- Retries must finish inside the nightly window. Source: incident note, role: on-call writeup.
- Do not queue exports. Rejected earlier for cost reasons. Source: design note.
Done means: `npm test -- src/export` passes and a retry test exists.
Before stopping: write decisions and open questions to notes/export-retry.md.| Fact | Layer | Why it goes there |
|---|---|---|
| Tests run with npm test -- path/to/file | CLAUDE.md | Needed in every session and cannot be inferred |
| Export code lives in src/export/ | CLAUDE.md | Stable layout, one line |
| Never edit applied migrations | Hook or permission rule | Must not have exceptions, so it is enforced, not requested |
| The export must not retry past the nightly window (constraint from a past incident note) | Task brief, with its source | Applies to this change, and the agent should be able to check it |
| An earlier idea to queue exports was rejected for cost reasons (from a design note) | Task brief, under rejected approaches | Stops the agent from reproposing it |
| Which job scheduler config owns the window | Retrieved at run time by search | Detail the agent loads only if it needs it |
| Done: export tests pass and a retry test exists | Task brief and test command | Gives the agent a pass or fail check |
| Decisions made mid-session | Handoff note written to a file | Survives /clear, compaction and a teammate picking up the work |
Where built-in tools are enough
For one engineer or a small team on one repository, built-in features cover most of this. A committed CLAUDE.md or AGENTS.md carries standing rules. A task brief typed into the first prompt carries the constraints. Claude Code's file search, /clear, automatic compaction, subagents and a test command cover run-time retrieval, session hygiene and verification.
You need nothing more while the facts that matter fit in a short file and in your head. Run /context all in Claude Code to see which memory files loaded, and keep each CLAUDE.md under about 200 lines, per Anthropic's memory documentation. Add structure only when you notice the same decision being re-explained.
The limits show up with a team. A committed CLAUDE.md is shared, but the reasons behind its rules usually are not written down, and auto memory stays on one machine, so engineer A's notes never reach engineer B's sessions. At 3 to 30 engineers, decisions also spread across pull requests, tickets and chat that no instruction file indexes. Hand-maintained files then grow past the size where adherence holds, and nobody owns pruning them.
Where Shifu fits
Built-in files record rules and the current session records progress. Neither reliably records which decision, incident or rejected proposal produced a constraint, and that reasoning lives in tickets, pull requests, chat and design notes across a team. Shifu is a context engine for AI coding teams. It is designed to retrieve the decisions, constraints and reasoning relevant to the task at hand, with sources. We have not published customer results yet. Retrieval still needs an applicability check against the current code.
Context engineering checklist
Run this before a change that depends on earlier decisions.
- Confirm CLAUDE.md or AGENTS.md holds only facts needed in every session.
- Write a task brief with goal, scope, constraints with sources, rejected approaches and a done check.
- Give the agent a command that returns pass or fail.
- Connect only the tools and sources the task needs.
- Move any must-never rule into a hook or permission rule.
- Write decisions and next steps to a file before you clear, compact or hand off.
- Check each retrieved constraint against the current code before relying on it.
Frequently asked questions
What is context engineering?
Context engineering is choosing and maintaining the set of tokens a model sees at each step, so it has what the task needs and little else. Anthropic defines it as optimizing the utility of those tokens against the inherent constraints of LLMs. For coding agents it covers instruction files, task briefs, retrieval, tools, compaction and verification, not only the prompt.
What is the difference between context engineering and prompt engineering?
Prompt engineering concerns writing and organizing instructions, especially a system prompt. Context engineering covers curating the whole set of tokens during a session, including tool output, retrieved files and earlier turns. Anthropic describes prompt work as a discrete task and context engineering as iterative, because you decide again what to pass to the model at each step.
Does Claude Code keep CLAUDE.md after compaction?
Yes for a project-root CLAUDE.md. Per Claude Code's memory documentation, it is re-read from disk and re-injected after /compact. Nested CLAUDE.md files and path-scoped rules load again on demand. An instruction given only in conversation can be lost, so add anything that must persist to CLAUDE.md. Checked 18 November 2026.
Should I use CLAUDE.md or AGENTS.md for context?
Use whichever your repository already has, and avoid maintaining two copies. Claude Code v2.1.277 or later reads AGENTS.md by default only when no CLAUDE.md or CLAUDE.local.md exists in your working directory or above it. If you keep both, import AGENTS.md from CLAUDE.md with @AGENTS.md so the content is read once.
How much context should I give a coding agent?
Give the smallest set that lets it act correctly: standing facts in instruction files, a short task brief, and pointers it can follow at run time. Anthropic recommends starting with a minimal prompt and adding instructions for failures you observe. There is no fixed number, so test a change by running the same task with and without the added context.
Do I need a tool beyond CLAUDE.md for context engineering?
Not for one engineer on one repository. Built-in instruction files, a task brief, search, /clear, compaction and a test command cover the layers in this guide. A further tool becomes worth testing when decisions and their reasoning sit across tickets, pull requests and chat, and teammates keep re-explaining them to agents.
Sources and further reading
- Anthropic Engineering: Effective context engineering for AI agents (fetched 18 November 2026)
- Claude Code docs: How Claude remembers your project (fetched 18 November 2026)
- Claude Code docs: Best practices (fetched 18 November 2026)
- Claude Code docs: Connect Claude Code to tools via MCP (fetched 18 November 2026)
Prepared with AI assistance and reviewed for source fidelity and practical usefulness. Examples marked fictional are illustrations, not customer results.