AI coding agents are becoming increasingly capable. They can explore a codebase, read files, execute commands, use tools, write code, and even handle multi-step development tasks. However, having access to a codebase does not necessarily mean an agent understands it.
To work effectively, an agent needs more than the source code. It needs to understand the systemβs architecture, business rules, coding conventions, data models, dependencies, and testing approach. Without this information, it may still produce code that looks reasonable but does not fit the existing system.
This is one of the challenges of working with AI agents. How do we provide the right context so that an agent can make good decisions instead of simply generating code based on incomplete assumptions?
Context Is More Than a Prompt
When working with AI, it is natural to focus on writing better prompts. We describe what we want, provide some instructions, and expect the model to produce the desired result.
This approach works reasonably well for isolated tasks. However, software development is rarely isolated. A seemingly simple change can depend on existing architecture, business rules, data relationships, authorization policies, and conventions that are not explicitly mentioned in the task.
For example, consider a request to implement a user profile update API. On the surface, the task is simply to accept user information, validate the input, and update the database.
But the actual implementation may need to account for several additional rules:
- Users can only update their own profiles.
- Some fields can only be changed by administrators.
- Every update must be recorded in the audit log.
- Changes to certain fields require additional validation.
- Database updates must follow the existing transaction pattern.
If the agent only receives the task description, it may implement the API correctly at the code level while violating important business rules.
The problem is not necessarily the quality of the prompt. The agent simply does not have enough information to understand the system.
This is where Context Engineering becomes important.
Rather than focusing only on how to phrase a request, Context Engineering focuses on how to select, organize, maintain, and deliver the information an agent needs at each stage of a task.
The goal is not to provide as much information as possible. It is to provide the information that is relevant to the decisions the agent needs to make.
Designing Context: Persistent Context and Task-Specific Context
Not all context serves the same purpose. Some information remains relevant across almost every task, while other information is only needed for a particular implementation or investigation.
I find it useful to distinguish between two categories, Persistent Context and Task-Specific Context.
Persistent Context describes the project and its conventions. It provides the background an agent needs to work consistently across different tasks. This may include architecture, coding standards, development commands, important business rules, and references to relevant documentation.
Task-Specific Context, on the other hand, describes the actual task being performed. It may include the issue description, affected modules, relevant files, reproduction steps, API contracts, constraints, and acceptance criteria.
Going back to the user profile update API from the previous section, the Persistent Context might explain:
- The application uses a modular architecture.
- All database access must go through the repository layer.
- API validation is implemented with Zod.
- Authorization is handled through the existing permission system.
- Tests must follow the projectβs integration testing conventions.
The Task-Specific Context would then provide the details of the requested change, such as which profile fields can be updated, which roles are allowed to perform the operation, and how the expected behavior should be verified.
Separating these two categories makes context easier to maintain and reuse. Project-wide knowledge does not need to be repeated in every task, while task-specific requirements do not need to become permanent project instructions.
A simplified structure could look like this:
Project Context
βββ Persistent Context
β βββ Architecture
β βββ Coding Conventions
β βββ Business Rules
β βββ Development Commands
β βββ Testing Guidelines
β
βββ Task-Specific Context
βββ Issue Description
βββ Requirements
βββ Relevant Files
βββ Constraints
βββ Acceptance Criteria
The important part is that an agent can access shared project knowledge while still receiving the specific information required for its current task.
Organizing Context Across Different Levels
A software project contains information at different levels. Some rules apply to the entire repository, while others are only relevant to a particular application, module, or task.
Treating all context as a single collection of instructions makes it difficult to determine which information is relevant.
A better approach is to organize context hierarchically.
At the repository level, instructions should describe rules that apply across the project. These may include general coding conventions, architecture principles, common commands, and shared development practices.
At the module level, context should describe the responsibilities and constraints of a particular area. For example, the backend may have API conventions, database modules may define migration rules, and the frontend may have component and state management guidelines.
At the task level, context should contain information that is specific to the current change.
Consider a monorepo with multiple applications:
project/
βββ AGENTS.md
βββ apps/
β βββ api/
β β βββ AGENTS.md
β βββ web/
β βββ AGENTS.md
βββ packages/
β βββ shared/
β βββ AGENTS.md
βββ docs/
β βββ architecture.md
β βββ domain-model.md
βββ package.json
The root AGENTS.md can define project-wide instructions. The API-specific file can provide backend conventions, while the frontend file can describe UI and component guidelines.
This structure helps avoid putting every rule into one large instruction file.
However, the exact way instructions are discovered and applied depends on the coding agent and its tools. Some tools support hierarchical instruction files, while others may use different configuration mechanisms. We should not assume that every agent automatically loads instructions from every directory.
The key principle is to make context discoverable, scoped, and predictable.
AGENTS.md Shouldnβt Hold Everything
Files such as AGENTS.md and CLAUDE.md are useful for providing project instructions to coding agents. But they are not necessarily the right place to store every piece of project knowledge.
One common mistake is allowing these files to grow into large documents containing architecture descriptions, business rules, API specifications, implementation details, and development instructions all mixed together.
Over time, the files become difficult to maintain. Instructions get duplicated, documentation becomes inconsistent, and agents may receive more information than they actually need.
It is important to distinguish between instructions and documentation.
Instructions tell an agent how to work within the project and where to find relevant information. Documentation explains how the system works and why certain decisions were made.
For example, a root AGENTS.md might look like this:
# Project Instructions
## Architecture
- Follow the existing modular architecture.
- Keep business logic outside API handlers.
## Development
- Use the existing package manager.
- Run the relevant checks before submitting changes.
## Testing
- Add tests for new behavior.
- Run integration tests for affected modules.
## References
- Architecture: docs/architecture.md
- Domain model: docs/domain-model.md
- Development guide: docs/development.md
The file does not need to explain the entire architecture or repeat every business rule. It only needs to establish important constraints and direct the agent to the relevant documentation.
This also helps maintain a clear Source of Truth.
For example, if a business rule is defined in docs/domain-model.md, other instruction files should reference that document rather than maintain separate copies of the same rule.
When the rule changes, there should be one primary place to update.
Without this separation, duplicated context can become a source of conflicting instructions rather than a source of clarity.
Not All Context Needs to Be Loaded Up Front
Providing an agent with every available document, instruction, and source file may seem like a safe approach. In practice, more context does not always lead to better results.
AI models operate within a limited context window. As more information is added, it consumes the available token budget. Large amounts of unrelated information can make it harder for the agent to focus on the details that actually matter.
This is particularly important in large repositories, where loading every architecture document, module specification, and implementation detail at the beginning of a task would be inefficient.
Instead of loading everything up front, an agent should retrieve additional context when it becomes relevant. This approach is often described as Just-in-Time Context.
For example, when implementing a database migration, the agent may initially need the project instructions and the task description. Once it identifies the affected module, it can inspect the existing schema, migration history, and database conventions.
There is no need to load frontend documentation or unrelated API specifications unless the task requires them.
Several mechanisms can support this approach.
Skills provide reusable instructions for specific types of work. A database migration skill, for example, can describe the projectβs migration workflow, naming conventions, safety checks, and verification steps.
MCP (Model Context Protocol) provides a standardized way for agents to access external tools and resources. This can help agents retrieve information from issue trackers, databases, documentation systems, or other connected services.
Retrieval mechanisms allow agents to search for and retrieve relevant information from a larger knowledge base instead of loading everything into the context window.
These mechanisms solve different problems. Skills provide procedural knowledge, MCP exposes tools and resources, and retrieval helps locate relevant information.
However, simply connecting more tools or adding more documents does not guarantee better results. The agent still needs to identify relevant information, understand its reliability, and distinguish project rules from outdated or unrelated content.
The goal is to make useful context available when it is needed, without overwhelming the agent from the beginning.
Context Isolation and the Role of Subagents
As tasks become more complex, a single agent may need to investigate multiple areas of a system.
For example, implementing a new API feature may require understanding the existing API structure, database schema, authorization rules, and integration tests.
One approach is to let a single agent investigate everything within the same context. However, as the investigation grows, the context can become crowded with details from different areas.
This is where Context Isolation and Subagents can be useful.
Instead of assigning the entire investigation to one agent, we can delegate smaller, well-defined tasks to specialized subagents.
For example:
Main Agent
β
βββ Subagent: API Investigation
β βββ Existing endpoints and API conventions
β
βββ Subagent: Database Investigation
β βββ Schema, relations, and migration patterns
β
βββ Subagent: Test Investigation
βββ Existing test coverage and test conventions
Each subagent can work with a more focused context and return relevant findings to the main agent.
This can reduce unnecessary context accumulation and make parallel investigation possible.
However, using subagents is not automatically better. Delegation introduces coordination overhead, and findings from different agents may be incomplete or inconsistent.
The main agent still needs to review the results, verify important claims against the codebase, and combine them into a coherent understanding of the task.
Subagents should therefore be used when a task can be divided into independent areas of investigation or implementation, not simply because multiple agents are available.
Context Has a Lifecycle
Context is not something that can be written once and expected to remain correct forever.
Software systems change. Architecture evolves, business rules are updated, dependencies are replaced, and development workflows change over time.
When documentation and agent instructions are not updated alongside these changes, they gradually become stale.
This creates a particularly difficult problem for AI agents. They may follow outdated instructions with confidence because the information is still present in their context.
For example, an architecture document may describe a background job system that has already been replaced. An agent following that document could implement a new feature using an approach the project no longer supports.
Maintaining context therefore needs to be part of the development lifecycle.
When a change affects architecture, business rules, or development conventions, the relevant documentation and instructions should be updated as part of the same work.
It is also useful to establish clear ownership for important information:
- Keep each important rule in one primary location.
- Reference existing documentation instead of duplicating it.
- Remove or update instructions when they become obsolete.
- Validate important constraints through automated checks whenever possible.
For example, architecture tests, lint rules, type checking, and CI pipelines can enforce certain conventions directly. This reduces the need to rely entirely on an agent remembering every instruction correctly.
Context should describe the system, but critical rules should also be enforced by the system whenever possible.
Building a Practical Project Context Architecture
Combining these ideas, a project does not need an overly complicated context management system to get started.
A practical structure could look like this:
project/
βββ AGENTS.md
β
βββ apps/
β βββ api/
β β βββ AGENTS.md
β βββ web/
β βββ AGENTS.md
β
βββ docs/
β βββ architecture.md
β βββ domain-model.md
β βββ development.md
β βββ decisions/
β
βββ .agents/
β βββ skills/
β βββ database-migration/
β βββ SKILL.md
β
βββ package.json
Each part has a specific responsibility.
The root AGENTS.md provides shared project instructions and references to important documentation.
Module-level instruction files describe conventions specific to individual applications or packages.
The docs/ directory contains more detailed project knowledge, such as architecture, domain models, development workflows, and technical decisions.
Skills provide reusable procedures for recurring tasks.
This structure is only an example. The actual layout should depend on the projectβs size, architecture, coding agent, and development workflow.
For a task, the agent can then receive a focused context containing the information it needs.
For example:
# Task: Implement User Profile Update API
## Context
- Follow the existing API architecture.
- Use the existing validation and authorization patterns.
- Refer to docs/domain-model.md for user-related business rules.
## Requirements
- Allow users to update their own profile.
- Restrict administrative fields to authorized roles.
- Record profile changes in the audit log.
## Constraints
- Use the existing repository layer.
- Follow the current transaction pattern.
- Do not introduce a new authorization mechanism.
## Verification
- Add integration tests for authorized and unauthorized requests.
- Verify audit log behavior.
- Run relevant type checks and tests.
The agent does not need to rediscover every project convention or guess important business rules from the source code. It receives the shared context, references the relevant documentation, and focuses on the actual task.
Over time, this approach creates a more predictable working environment for both developers and AI agents.
Conclusion
The effectiveness of AI coding agents is not determined only by the modelβs capabilities or the quality of individual prompts. It also depends on how well the project provides the information required to understand and work within the system.
Context should be treated as part of the software engineering process. It needs to be structured, scoped, discoverable, and maintained as the codebase evolves.
The objective is not to give agents access to everything. It is to help them find the right information, understand the relevant constraints, and make decisions based on the same knowledge that developers rely on.
Context therefore needs to be designed as part of the software engineering system, not treated as a matter of writing better prompts.