Context engineering for coding agents: diagnose before you add more tokens
A practical diagnostic flow for missing facts, stale instructions, noisy tool output, and genuine context limits, with a worked debugging example.
Updated
Sources are linked in the guide. Read our editorial policy.
On this page 6 sections
Give a coding agent the information needed for its next decision, not every document that might matter eventually. When it repeats a failed fix or edits the wrong layer, first identify whether the problem is missing evidence, conflicting instructions, stale state, or excessive history. A larger context window only addresses one of those possibilities.
Anthropic’s context engineering article, published September 29, 2025, describes context as a finite resource and recommends high-signal information, just-in-time retrieval, and selective compaction. That is an engineering principle, not a promise that every shorter prompt performs better.
This guide turns that principle into a diagnostic flow. The examples are illustrative; they are not results from a benchmark or a production incident.
First separate context from capability
An agent cannot infer a private deployment rule from source code. It also cannot repair an inaccessible database merely because you paste more documentation. Before changing the prompt, check three prerequisites:
- Can the agent read the current implementation and the relevant error?
- Does it have the tools and permissions needed for the requested work?
- Is the desired behavior specific enough that someone else could judge the result?
If the answer to any is no, repair that prerequisite. Do not label every failure “context rot.” A weak model, a broken tool, an ambiguous request, and an actual software defect can produce similar symptoms.
Follow this diagnostic flow
Start with one incorrect action and work backward to the evidence that should have prevented it.
| Question | If yes | If no |
|---|---|---|
| Was the decisive fact available to the agent? | Check whether it was current | Supply the smallest relevant source excerpt or retrieval path |
| Was that fact current when the action happened? | Check for competing instructions | Re-read the source and replace the obsolete claim |
| Did two instructions describe different desired outcomes? | Resolve precedence explicitly | Inspect whether relevant evidence was buried in unrelated material |
| Is most of the history irrelevant to the current decision? | Compact or start a focused session | Investigate model capability, tool behavior, or the diagnosis itself |
| Can the next action be checked independently? | Run the agreed check when authorized | Define the missing acceptance condition before continuing |
Do not change every variable at once. If you replace the model, rewrite the task, clear the history, and change the implementation together, a later success does not tell you which intervention helped.
Worked example: an agent keeps changing the wrong pagination code
Suppose a results page duplicates a row after clicking Next. The agent keeps modifying the browser’s array slicing. Meanwhile, the server returns an inclusive cursor boundary, and a product decision says subsequent pages must exclude the cursor row.
The first useful context is not the entire frontend repository. It is:
- The observed first-page and second-page item IDs, with sensitive fields removed.
- The endpoint’s cursor comparison and ordering code.
- The caller that passes the next cursor.
- The intended boundary rule and a regression case.
Ask the agent to identify the first point at which actual IDs diverge from expected IDs before proposing an edit. This separates “the UI displays duplicates” from “the UI creates duplicates.”
An illustrative task note might read:
Goal: page two must not repeat the final item from page one.
Boundary: the cursor identifies the last item already returned.
Evidence: page one ends with ID 40; page two starts with ID 40.
Inspect: endpoint cursor predicate, stable sort, and next-cursor caller.
Unknown: whether equal timestamps have a deterministic tie-breaker.
Do not change rendering until the response boundary is understood.
Those IDs are explanatory examples, not observed application output. In a real investigation, attach the actual response and relevant source location. If the server response is correct, move to rendering; the note should guide investigation, not predetermine the culprit.
Keep durable rules separate from live facts
Repository instructions belong in the instruction file supported by your agent, such as AGENTS.md or CLAUDE.md. Keep them focused on facts that cannot be readily discovered: required checks, surprising legacy constraints, and important team decisions.
Live task facts belong with the task: what changed, which hypothesis failed, what remains unknown, and which revision a result exercised. Claude Code’s documentation makes the same distinction between project knowledge and information already discoverable from code.
A past test pass is not a property of the current branch. After a relevant edit, describe that result as historical until the affected check runs again. Similarly, a summary of a file is not a substitute for opening its current contents before changing it.
Compact deliberately, without erasing evidence
When the task is still coherent but its history is noisy, preserve the objective, active constraints, decisions with reasons, unresolved failures, changed files, and evidence references. Remove duplicate logs and abandoned speculation. Keep rejected approaches only when their rejection prevents likely repetition.
Start a new session when the objective genuinely changes. Before resuming, retrieve the current files rather than trusting the summary as authoritative. Compaction can omit crucial details; no universal token count tells you when it is safe.
External documents and tool results also remain untrusted data. A cleaner context does not make instructions embedded in an issue comment legitimate. Use the MCP security checklist for that boundary.
Choose the next improvement from the failure
Use the coding-agent acceptance workflow to make success observable. If local memory is the actual constraint, use the local-model sizing guide. Compare compatible tools in the coding directory, but judge them on your own task rather than context-window size alone.
For repository exploration, Graphify offers a queryable code graph; follow its source references rather than treating inferred edges as runtime proof. Cursor can use the resulting scoped context, but tool permissions and verification remain separate from how much context you supply.
Research note: X search results informed topic selection, but direct thread access was blocked during research. This guide relies on the linked documentation, not an asserted reading of the latest threads.