Skip to content

Context engineering for code

Coding with agents · topic coding-with-agents/context

An agent's output quality follows the quality of its context. This topic covers what belongs in a project's standing instructions, how to scope a task so the agent looks in the right places, how to point it at specific files, and how to keep a session from degrading as its context fills.

Concepts

Project instructions
A file in the repository the agent reads at the start of every session, holding what it needs to know each time: how to build and test, the conventions to follow, the places not to touch. It is where a recurring agent mistake gets fixed once. Keep it short and factual; long instruction files are followed less reliably than short ones. glossary
Scoping a task
Telling the agent where a task lives and where it stops: which module, which layer, which files are in play and which are off limits. Scoping saves context by avoiding exploration of irrelevant code and prevents the well-meaning refactor that spreads a small change across the repository. A scoped brief also makes the resulting diff easier to review. glossary
Referencing files
Naming the exact files, functions or documents the agent should read instead of describing them, so it loads the right context on the first try. A path and a line number beat "the config code". Reference the example to imitate, the test to extend and the spec to follow; the agent will otherwise pick whichever similar file it finds first. glossary
Avoiding context rot
Keeping the session's context lean so the agent keeps following instructions and remembering the goal: one task per session, summaries instead of raw logs, fresh sessions for new work, and no pasting of whole files it can read itself. When answers drift or earlier rules get ignored, the context is full; compact or restart rather than push on. glossary
Starting context
Everything in the context window before the first message: the harness's system prompt and tool descriptions, connected servers, skill descriptions, instruction files and saved memory. It is sent again with every request, so it costs tokens each time and leaves less room for the task. A context command shows its size by category. Moving rarely needed guidance into skills or rules that load on demand, and disconnecting unused servers, makes it smaller. glossary
Deciding at a phase boundary
Choosing what the next phase of work starts with when one phase ends, such as after planning. The options are to carry on in the same session, clear the context, compact it into a summary, write a handoff for a fresh session, or delegate the next part to a subagent. The choice follows what the next phase needs: the full history when it continues the same problem, and a written spec in a clean window when it starts a new one. glossary

Lessons

Your reference

Each lesson above adds its takeaways and its example here once you finish it. Your reference lists every lesson you have finished.

Writing project instructions the agent reads every session

Unlocks when you finish Writing project instructions the agent reads every session.

Telling the agent where the task is and where it stops

Unlocks when you finish Telling the agent where the task is and where it stops.

What each phase starts with

Unlocks when you finish What each phase starts with.

Sources