Context Engineering for AI Coding Agents: 7 Practices Backed by Research
The model you pick matters less than what you put in front of it. Research from 2023 to 2026 keeps finding the same thing: models get less reliable as their input grows, an auto generated AGENTS.md can make an agent slower and more expensive without making it better, and a well designed tool interface can lift an agent more than a model upgrade. Context engineering is the practice of deciding exactly what the agent sees on every step.
This review takes seven practices that coding agent teams recommend and checks each one against the study behind it: what was measured, how big the effect was, what to change in your setup, and what it does to your bill. Where the evidence is thin or comes from a vendor, we say so.
18 of 18
models tested by Chroma became less reliable as input length grew
+20%
inference cost from context files like AGENTS.md, with no gain in success (ETH Zurich, ICLR 2026)
+64%
relative gain from a better agent tool interface, same model (SWE-agent, NeurIPS 2024)
What is context engineering for AI coding agents?
A coding agent runs in a loop. On each step it reads files, runs a command, looks at the result and decides what to do next, and on each step it resends a growing pile of instructions, code, tool output and history to the model. Context engineering is choosing which tokens go into that pile, in what order, and which ones get dropped.
Anthropic framed it in September 2025 as the successor to prompt engineering. A prompt is written once. Context is assembled again on every step, so it has to be managed like a budget: every token you add competes for the model's attention with every token already there.
Our AI coding agents 2026 research overview covers the six layers modern agents give you to control context, from rules files to hooks. This post goes one level deeper and asks which practices actually hold up when someone measures them.
The 7 practices at a glance
Summary
Seven context engineering practices and the evidence behind them
Effect sizes are as reported by each study. Study settings differ, so do not add them up.
| Practice | Key study | What it found |
|---|---|---|
| 1. Keep the working context small | Chroma Context Rot (2025), Lost in the Middle (TACL 2024) | All 18 models degraded with longer input. Facts in the middle of a long prompt are used worst. |
| 2. Keep AGENTS.md minimal and human written | Evaluating AGENTS.md, ETH Zurich (ICLR 2026) | Context files did not raise success and raised cost by about 20%. |
| 3. Retrieve just in time | Anthropic engineering guidance (2025) | Load file paths and search results on demand instead of preloading. |
| 4. Design what tools return | SWE-agent (NeurIPS 2024) | 18.0% vs 11.0% on SWE-bench Lite from interface design alone. |
| 5. Feed back test and execution results | Self-Debugging (ICLR 2024) | Up to 12% higher accuracy when unit test feedback is in context. |
| 6. Isolate exploration in subagents | Anthropic multi-agent research system (2025) | 90.2% better on breadth-first research, at about 15 times the tokens. |
| 7. Persist lessons, compact with care | ACE, Agentic Context Engineering (ICLR 2026) | +10.6% on agent tasks by growing a playbook instead of rewriting it. |
1. Keep the working context small
The evidence
Chroma, Context Rot (July 2025). Chroma tested 18 models, including GPT, Claude, Gemini and Qwen, on tasks where only the input length changed. Every model got less reliable as input grew, well before its context window was full. Distractors, meaning passages that look relevant but are wrong, made it worse, and even one similar distractor reduced accuracy.
Liu et al., Lost in the Middle (TACL 2024). Accuracy was highest when the needed information sat at the start or end of the prompt and dropped sharply when it sat in the middle. Performance fell as context grew, even for models built for long context.
For a coding agent, distractors are everywhere: an old version of a function, a similar helper in another folder, a stale stack trace from three steps ago, a test log for a different failure. They look relevant, so the model attends to them. A 1M token window is a ceiling, not a target.
What to do:
- Start a fresh session for each task instead of letting one conversation run all day.
- Exclude build output, lock files, vendored code and generated files from what the agent can read. Cline supports a
.clineignorefile, and most agents respect.gitignore. - Put the task statement and the most important constraint at the start or end of your message, not buried in the middle of pasted logs.
- Paste the one failing test and its error, not the whole test run.
2. Keep AGENTS.md minimal, and write it yourself
The evidence
Gloaguen, Mündler, Mueller, Raychev and Vechev, Evaluating AGENTS.md (ETH Zurich and LogicStar.ai, ICLR 2026). The team ran several coding agents and models on SWE-bench tasks with LLM generated context files, and on a new set of 138 real Python issues from repositories that already ship developer written context files.
Context files did not improve task success overall and raised inference cost by more than 20%. LLM generated files reduced success in 5 of 8 settings. Developer written files did better, with an average gain of about 4%, but still added about 19% to cost.
The mechanism: agents follow these files faithfully. They ran more tests, searched more files and took about 2.5 to 3.9 extra steps per task, but did not find the relevant files any faster.
Cost of context files
Extra inference cost when an AGENTS.md style file is added
Evaluating AGENTS.md, ICLR 2026. Success did not improve overall in either setting.
LLM generated file, new issue benchmark · +3.92 steps per task
LLM generated file, SWE-bench · +2.45 steps per task
Developer written file · about +4% success on average
Reference: a developer running a coding agent all day on Claude Sonnet 5.5, at 150 steps a day for 22 workdays with 30,000 input and 1,000 output tokens per step, spends about ₹23,624 a month at Velona prices as of 16 October 2026. A 20% overhead on that is roughly ₹4,700 a month.
The authors' conclusion is direct: unnecessary requirements make tasks harder, so a human written file should state only minimal requirements. That matches the advice of practitioners who add a line to AGENTS.md only after the agent has failed at something.
What to do:
- Delete any AGENTS.md or CLAUDE.md that an agent generated for you, unless you have read and trimmed every line.
- Keep what the agent cannot discover by reading the code: the exact test command, the package manager, the one directory it must never touch.
- Remove anything that restates what is already in the README or obvious from the code.
- Treat each line as a cost. If a rule does not fix a failure you have actually seen, it probably does not belong.
3. Retrieve context just in time instead of preloading it
The evidence
Anthropic, Effective context engineering for AI agents (September 2025). Anthropic recommends that agents keep lightweight references such as file paths and search queries, and load the content only when a step needs it. Claude Code is its own example: a small rules file is loaded up front, and files are pulled in with glob and grep as the work goes.
This is engineering guidance from a model vendor, not a controlled study. It is consistent with practices 1 and 2: preloading adds tokens, and the extra tokens are often the distractors that Chroma found harmful.
The opposite pattern is common in home made agents: embed the whole repository, retrieve the top 20 chunks for every request, and paste them all in. That fills the context with near misses before the agent has even looked at the problem.
What to do: give the agent fast search tools (ripgrep, a symbol index, the language server) and let it pull files when it needs them. Use pre-retrieval only for small, stable material such as an API schema or a style guide. If you do use retrieval, keep the result count small and rerank before inserting.
4. Design what your tools return, not just what they do
The evidence
Yang et al., SWE-agent (Princeton, NeurIPS 2024). With the same model, GPT-4 Turbo, an agent using a purpose built agent-computer interface solved 18.0% of SWE-bench Lite against 11.0% for an agent with only a plain Linux shell. That is a 64% relative gain from interface design alone.
The interface choices were mostly about context: search commands that summarise and cap results at 50 matches, a file viewer that shows a window of lines instead of a whole file, and a linter that runs on every edit and rejects broken code with a short, clear error.
Same model, different interface
Share of SWE-bench Lite issues solved by GPT-4 Turbo
SWE-agent paper, NeurIPS 2024
Plain Linux shell
Agent-computer interface
The benchmark and model are from 2024. The lesson about tool output design still applies to current agents.
Every tool result lands in the context and stays there for the rest of the session. A grep that returns 800 lines, a test runner that prints every passing test, or a build log full of warnings can each cost thousands of tokens and push the useful facts into the middle.
What to do:
- Wrap noisy commands. Run tests in a quiet mode that prints only failures, for example
pytest -qorvitest --reporter=dot. - Cap search output and tell the agent how many matches were cut.
- Return errors in one short, specific line rather than a full stack trace when the top frame is enough.
- Load only the MCP servers a task needs. Each one adds its tool descriptions to every request.
5. Put test and execution results back into the context
The evidence
Chen, Lin, Schärli and Zhou, Teaching Large Language Models to Self-Debug (Google Research and UC Berkeley, ICLR 2024). When the model saw the results of running its code against unit tests and explained the failure, accuracy rose by up to 12% on code benchmarks where tests were available. It also matched or beat baselines that generated more than 10 times as many candidate programs.
Without external feedback such as tests or error messages, the authors found that models struggle to correct their own code. The feedback is what makes the loop work.
This is the strongest argument for one specific kind of context: the output of a real check. A failing assertion with expected and actual values is a few dozen tokens that carry more signal than pages of instructions. It is also why the ACE paper in practice 7 could improve agents without labelled data, by using natural execution feedback.
What to do: give the agent one exact test command and tell it a task is not finished until that command passes. Feed back the failing case, not the full log. If a part of your codebase has no tests, ask the agent to write a failing test that reproduces the bug before it touches the code.
6. Isolate exploration in subagents, and budget for the tokens
The evidence
Anthropic, How we built our multi-agent research system (June 2025). A lead agent on Claude Opus 4 with Claude Sonnet 4 subagents outperformed a single Claude Opus 4 agent by 90.2% on Anthropic's internal research evaluation. Each subagent explored in its own fresh context and handed back a condensed result.
The cost was large: agents used about 4 times the tokens of a chat, and multi-agent systems about 15 times. Token usage alone explained about 80% of the performance difference. Anthropic also said tasks where agents must share the same context, or have many dependencies between them, are not a good fit today, and most coding falls in that group.
Searching a large codebase for every caller, reading docs for three libraries, reviewing a diff with a fresh pair of eyes. The subagent's long exploration stays out of the main context.
Changing one function and its callers across files, or any task where each step depends on the last. Splitting it means each agent works with half the picture.
What to do: use a subagent when you want to keep a large, messy exploration out of the main thread, such as finding where a value is set or reviewing a finished diff. Keep the actual editing in one agent. Ask subagents for short, structured answers, and run them on a cheaper model when the task is search rather than judgement.
7. Persist what the agent learns, and compact with care
The evidence
Zhang et al., Agentic Context Engineering, ACE (ICLR 2026). ACE treats an agent's context as a playbook that grows through small, structured updates after each task, using generation, reflection and curation steps. It beat strong baselines by 10.6% on agent benchmarks and 8.6% on finance tasks, and on the AppWorld leaderboard it matched the top production agent while using a smaller open source model.
The paper names two failure modes that matter for anyone who compacts context. Brevity bias: summaries drop the specific details that made a lesson useful. Context collapse: rewriting the whole context again and again slowly erodes it.
Compaction, summarising a long session and starting again from the summary, is now built into most agents. ACE is a warning about how to do it: append and edit small entries rather than rewriting everything into a short paragraph. The same idea works by hand with a notes file that the agent updates as it goes.
A small progress file the agent keeps on disk survives context resets and costs only a few hundred tokens to reload:
# NOTES.md (task: fix duplicate invoice emails)
## Decisions
- Root cause: retry in send_invoice() has no idempotency key
- Fix in billing/mailer.py only. Do not touch the queue worker
## Done
- Failing test added: test_invoice_sent_once
## Next
- Add idempotency key, run: pytest tests/billing -q
## Gotchas
- Local SMTP mock must be started first: make mail-mock
What to do: before you compact or start a new session, have the agent write decisions, finished work, next steps and gotchas to a file. Keep the exact commands and file paths. When a lesson repeats across tasks, promote that one line into AGENTS.md, and delete it later if it stops earning its place.
How context engineering lowers your coding agent bill
Coding agents are input heavy. In a typical step the agent sends about 30,000 tokens and gets back about 1,000, so roughly 86% of the bill on a model like Claude Sonnet 5.5 is input. Every practice above shrinks that input. One more lever works alongside them: prompt caching. On Anthropic models, reading a cached prefix costs 10% of the normal input price, and writing it costs 1.25 times the input price for a 5 minute cache. Caching only works if the start of the context stays the same from step to step, which is one more reason to keep rules files short and stable.
Cost per agent step
One coding agent step on Claude Sonnet 5.5, in rupees
30,000 input and 1,000 output tokens, Velona prices on 16 October 2026
No caching
25,000 of the 30,000 input tokens read from cache
About 68% cheaper per step once the cache is warm. The first step pays a one time write premium of about ₹1.28 on the 25,000 cached tokens, and the cache expires after 5 minutes of no use. Cache reads are priced at 10% of input, about ₹10.23 per 1M tokens on Sonnet 5.5, as of 16 October 2026.
For full rupee numbers by model, see our guides to Claude Sonnet 5 pricing and the cheapest AI model for coding agents.
A context engineering checklist for your next agent session
Start cleanNew session per task. One task statement, at the top, with the acceptance test.
Trim the rules fileOnly the test command, package manager and hard limits. Delete generated files you have not edited.
Hide the noiseIgnore build output, lock files and generated code. Load only the MCP servers this task needs.
Make tools quietTests print failures only. Searches are capped. Errors are one line.
Close the loopThe agent runs the test after each change and sees only the failing case.
Delegate search, not editsUse a subagent for wide exploration or review, keep editing in one thread.
Write it downBefore compacting, save decisions, next steps and gotchas to NOTES.md.
Keep the prefix stableSo prompt caching can do its job on every step.
Do Habits backed by the studies
- Short, hand written AGENTS.md with only what the code cannot tell the agent
- Fresh session per task
- Quiet test and build output
- A single exact test command as the finish line
- Subagents for search and review
- A notes file that survives resets
- A stable prompt prefix for caching
Avoid Habits the studies warn against
- Agent generated context files committed unread
- One session that runs until the window is full
- Pasting whole logs and full test runs
- Preloading the top 20 retrieved chunks on every step
- Splitting tightly coupled edits across agents
- Rewriting the whole context into a short summary
- Loading every MCP server into every session
Limits of this evidence
These studies are useful, but read them for direction, not exact numbers. Most were run on benchmarks built from public Python repositories, which may differ from your codebase. Some used models that are now one or two generations old. The Anthropic results come from internal evaluations that outsiders cannot reproduce, and SWE-bench Verified itself has known contamination problems, which is why OpenAI stopped reporting it in February 2026. The safest reading is the pattern they share: less but better context, real feedback from tests, and tools that return short, useful output. If you want to check a practice on your own code, our guide to evaluating AI coding models on your own tasks shows how to measure it.
Frequently asked questions
What is context engineering for AI coding agents?
Context engineering is deciding what an AI coding agent sees on each step: which instructions, files, tool results and history go into the model's input, in what order, and what gets dropped. It replaced prompt engineering as the main lever because agents rebuild their context on every step of a task.
Does AGENTS.md help coding agents?
Not by default. An ETH Zurich study at ICLR 2026 found that context files such as AGENTS.md did not improve task success overall and raised inference cost by more than 20%. LLM generated files reduced success in 5 of 8 settings. Short, human written files that state only minimal requirements did better.
What is context rot?
Context rot is the drop in model reliability as the input gets longer, even when the context window is far from full. Chroma tested 18 models in 2025 and found that every one degraded with longer input, and that distractors which look relevant but are wrong made it worse.
How does context engineering reduce AI coding costs?
Coding agents spend most of their budget on input tokens, so sending fewer tokens per step cuts the bill directly. Keeping the prompt prefix stable also lets prompt caching work. On Claude Sonnet 5.5, a step of 30,000 input and 1,000 output tokens costs about ₹7.16 without caching and about ₹2.30 when 25,000 of the input tokens are read from cache, at Velona prices as of 16 October 2026.
Should I use subagents for coding?
Use them for breadth first work such as searching a large codebase or reviewing a finished diff, where their long exploration stays out of the main context. Anthropic found multi-agent setups use about 15 times the tokens of a chat and are a poor fit for tightly coupled tasks, which includes most code editing.
Do tests help AI coding agents?
Yes. Putting real test results back into the context is one of the best supported practices. The Self-Debugging study found up to 12% higher accuracy when unit test feedback was available, and models struggled to fix their own code without that external feedback.
Run cheap and strong models from one key.
Claude, DeepSeek, Qwen, GPT and 300+ more, priced in rupees and paid with UPI.
Sources and method
Figures are quoted from the papers and posts below as reported by their authors. We did not rerun any of these experiments. Rupee examples use Velona list prices on 16 October 2026 with the request sizes stated, and Anthropic's published cache multipliers of 1.25 times input for a 5 minute cache write and 0.1 times input for a cache read.
- Gloaguen et al., Evaluating AGENTS.md: Are Repository-Level Context Files Helpful for Coding Agents? (ICLR 2026)
- Engineer's Codex summary of the AGENTS.md study
- Chroma Research, Context Rot
- Liu et al., Lost in the Middle: How Language Models Use Long Contexts (TACL 2024)
- Anthropic, Effective context engineering for AI agents
- Yang et al., SWE-agent: Agent-Computer Interfaces Enable Automated Software Engineering
- Chen et al., Teaching Large Language Models to Self-Debug
- Anthropic, How we built our multi-agent research system
- Zhang et al., Agentic Context Engineering (ACE)
- Anthropic prompt caching documentation
- OpenAI, Why SWE-bench Verified no longer measures frontier coding capabilities