
Context Engineering for Coding Agents
Bad output from a coding agent often traces back to what it could not see. Here is the context inventory, why one example can beat a description, and the session-reset heuristic.
TL;DR: Bad output from a coding agent is often a context failure rather than a model failure — the agent reinvented a utility it never saw or violated a convention it was never shown. Before blaming the model, ask what the agent could actually see when it decided. Supply context deliberately: the target files, the neighboring interfaces, one canonical example of the pattern to follow, the failing output verbatim, and the relevant constraints. One existing implementation can convey a pattern better than a paragraph describing it. More is not better — irrelevant files dilute attention and stale examples can actively mislead — so curate to fewer, representative inputs. And when correcting the agent stops working, reset with a clean, better-curated context instead of arguing. This article covers the diagnostic, the inventory, the budget, and the reset.
Many agent failures are context failures
When a coding agent produces something wrong, the reflex is to blame the model — it is not smart enough, the task was too hard. Often, the agent did exactly what a competent worker would do with the information it had, and the information was the problem. An agent that has not seen your existing date-formatting utility can write a new one, because from where it sat there was no utility to reuse. An agent that has not seen how your codebase handles errors can invent its own convention, because it was never shown the one you already have. Neither needs to be a reasoning failure. Both can be the predictable result of deciding without the relevant input.
The diagnostic habit that follows is simple and worth making automatic: before concluding the model failed, reconstruct what the agent could actually see when it made the decision. Not what exists in the repository — what was in front of it. If the utility it reinvented was in a file the agent never opened, the fix is not a smarter model or a sterner prompt; it is putting that file in view. If the convention it violated lives in a directory it was never pointed at, the same. The question "what could it see" turns a vague complaint about quality into a specific, fixable gap.
This reframes the work. Context selection is engineering judgment, not a mechanical retrieval step. An agent can search and explore the repository to pull in context, but it may not find a convention it has no name for or a file it has no reason to open, and it cannot guess a convention that is not visible in what it read. One part that shapes output quality — deciding what the agent is pointed at for this specific task — is a human decision made before the agent runs; the agent can still retrieve more on its own as it works.
The context inventory
Deciding what to supply goes better as a checklist than as intuition, because the categories that matter are stable across tasks even when the specific files change. Five kinds of context are worth supplying deliberately rather than hoping the agent finds them.
| Context category | What it is | Why to supply it |
|---|---|---|
| Target files | The code that will actually change | Without them the agent edits code it has not read |
| Neighboring interfaces | The types, signatures, and modules the change touches | Reduces the risk of inventing shapes that already exist |
| One canonical example | A single existing implementation of the pattern to follow | Can show the convention faster than describing it |
| Verbatim error output | The exact failing message, stack, or diff | Guessing from a paraphrase can waste the attempt |
| Spec constraints | The relevant limits and requirements for this task | Helps keep the work inside the real boundaries |
Two of these deserve a note about scope. The canonical example is a pointer to a pattern in the codebase, not the durable repo-wide instruction files that make a codebase legible in general — those are a separate concern, covered in the article on the agent-readable codebase. And "spec constraints" here means handing the agent the requirements that bound this task; what the work statement itself should say is covered in the article on spec-driven development. This inventory is about the supporting material you gather around a task, selected fresh each time.
The distinction between what agents find on their own and what must be pushed is the practical core. Retrieval covers what an agent can name — it can find a function if it can guess the function's name, follow an import if it can see the import. It may not retrieve a convention it has no name for, or know that the right pattern lives in a file it had no reason to open. Names it can guess, the agent can find; conventions it cannot guess, you must push.
The single highest-leverage item in that inventory is the canonical example, because one example often conveys a pattern better than a description. Tell an agent "follow our usual approach to data access" and you have described a convention in words the agent must now interpret and reconstruct, probably imperfectly. Show it one existing data-access module that does the thing correctly, and the agent can see the pattern in full — the naming, the error handling, the structure, the small decisions no paragraph would think to mention. A description is a lossy compression of a pattern; the implementation is the pattern. When you can point at a real instance of "do it like this," that single file can outweigh a page of prose about it.
The corollary is that a good example is worth finding before you start. It is tempting to write instructions because writing is fast and searching for the right precedent takes a minute. But the minute spent locating the one module that exemplifies the pattern pays back in an agent that matches your codebase instead of approximating it. The vendor discussion of context engineering as a discipline makes a related point from the tool-builder's side — for the examples in an agent's prompt, it recommends curating a set of diverse, canonical examples that portray the agent's expected behavior, rather than stuffing the prompt with a laundry list of edge cases meant to spell out every rule the model should follow for a task (Anthropic, "Effective context engineering for AI agents", as of November 2026) — and the vendor-independent memo series on Martin Fowler's site treats the context supplied to a coding assistant as a major part of working with these tools (Exploring Generative AI, as of November 2026).
The context budget
If context were free, the answer would be to supply everything. It is not. Attention is finite, and every irrelevant file you add competes with the relevant ones for it. More context is not better context — past the point of relevance, additional material dilutes the agent's focus and, worse, stale material can actively mislead. A deprecated example left in view is not neutral; it can read as an instruction to do the wrong thing, and the agent may not be able to tell it is deprecated unless the code marks it or you tell it.
The overload has recognizable symptoms. The agent can latch onto an incidental pattern from an unrelated file that happened to be in scope and apply it where it does not belong. It can follow a deprecated example that happened to be the one it saw most clearly. It can produce something shaped by whatever was loudest in the pile rather than by what was relevant. When output goes sideways in these specific ways — copying a pattern you did not intend, using an old approach you thought was gone — the cause may be not too little context but too much of the wrong kind.
The curation move is to prefer fewer, more representative inputs over bulk dumps of the repository. Handing an agent the whole codebase feels safe and is the opposite; it buries the five files that matter under five hundred that do not. Choose the target files, the one best example, the interfaces actually touched, and the verbatim error, and leave the rest out. A tight, representative context is not a compromise forced by limits — it is the better context, because it points the agent's attention where the task lives instead of scattering it.
Context across a session
A single well-curated context is one thing; a long session is another, and it degrades in a specific way. As you correct the agent turn after turn, the earlier wrong attempts stay in the context window — in full, unless the tool summarizes the history through compaction or clears earlier tool results — and can keep influencing what comes next. The session accumulates contradictions: the first flawed approach, your correction, a second attempt that half-followed the correction, another correction. By the tenth exchange the agent is reconciling a transcript of mistakes, and the noise starts to outweigh the signal.
The heuristic for this is the reset. When correcting the agent stops working — when each fix produces a new problem and you are arguing with something that keeps half-remembering its earlier errors — stop correcting and restart. Open a clean context, and this time curate it with everything you learned from the failed session: the right example you should have supplied first, the constraint that turned out to matter, the file the agent kept missing. A fresh start with better context beats another correction layered onto a contaminated one.
The discipline that makes the reset cheap is carrying forward only the distilled decision, not the full history. What you learned in the failed session is a few sentences — "use this module as the pattern, respect this constraint, the bug is in this file" — not the entire back-and-forth of how you learned it. Bring the conclusion into the new context and leave the transcript behind. Reconstructing the argument that got you there only reintroduces the contradictions you reset to escape; the value is in the decision, and the decision is small enough to carry clean.