# Context engineering for code

> 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.

Page: https://schubergphilis.github.io/ai-training/topics/coding-with-agents/context/
License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/

*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](https://schubergphilis.github.io/ai-training/glossary/#project-instructions)
- **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](https://schubergphilis.github.io/ai-training/glossary/#scoping-a-task)
- **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](https://schubergphilis.github.io/ai-training/glossary/#referencing-files)
- **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](https://schubergphilis.github.io/ai-training/glossary/#avoiding-context-rot)
- **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](https://schubergphilis.github.io/ai-training/glossary/#starting-context)
- **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](https://schubergphilis.github.io/ai-training/glossary/#deciding-at-a-phase-boundary)

## Links

- **Builds on:** [Plan, implement, verify](https://schubergphilis.github.io/ai-training/topics/coding-with-agents/workflow/), [Grounding and memory](https://schubergphilis.github.io/ai-training/topics/concepts/grounding/)
- **Leads to:** nothing yet
- **Related:** [Project instructions](https://schubergphilis.github.io/ai-training/topics/customizing-agents/instructions/)
- **Competencies drawing on it:** [Ships a change with a coding agent through plan, implement and verify](https://schubergphilis.github.io/ai-training/competencies/coding-with-agents/ships-with-agent/)

## Lessons

- [Writing project instructions the agent reads every session](https://schubergphilis.github.io/ai-training/coding-with-agents/project-instructions/) (tutorial)
- [Telling the agent where the task is and where it stops](https://schubergphilis.github.io/ai-training/coding-with-agents/scoping-and-referencing/) (tutorial)
- [What each phase starts with](https://schubergphilis.github.io/ai-training/coding-with-agents/what-a-phase-starts-with/) (explanation)

## Sources

- `AEC-15` [AGENTS.md: contents, monorepo hierarchies, with a builder widget](https://lsimons.github.io/agent-engineer-course/15-agents-md/), Agent Engineer Course (course)
- `Academy claude-code-101` [Claude Code 101](https://academy.claude.com/courses/claude-code-101), Claude Academy (course)
- `Academy claude-code-in-action` [Claude Code in action](https://academy.claude.com/courses/claude-code-in-action), Claude Academy (course)
- `Claude Code context window` [Explore the context window](https://code.claude.com/docs/en/context-window), Claude Code documentation (reference)
- `Claude Code best practices` [Best practices for Claude Code](https://code.claude.com/docs/en/best-practices), Claude Code documentation (reference)
- `Anthropic context engineering` [Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents), Anthropic Engineering (reference)
- `OpenAI Academy get-started-with-codex-jkhsq` [Get Started with Codex](https://academy.openai.com/public/courses/get-started-with-codex-jkhsq), OpenAI Academy (course)
