Agent FrameworksguideIntermediate8 min read

Context Engineering for Coding Agents

Martin Fowler explores how to structure and optimize context windows for AI agents that write code, including prompt design patterns and practical techniques for improving agent reasoning and output quality.

Updated 2026-03-17

Key Takeaways

  • Structure context hierarchically: architecture → relevant modules → tests → error history
  • Use relevance-based selection (embeddings, dependency graphs) to fetch only contextual code
  • Include test cases as executable specifications to reduce agent hallucination
  • Allocate token budget: ~15% instructions, 35% codebase, 25% tests, 25% history
  • Implement context caching across multi-step agent workflows to reduce token spend
  • Avoid static context, make context dynamic and task-aware for better agent outputs
  • Maintain codebase indexes (signatures, APIs) to avoid sending full file contents

Context Engineering for Coding Agents

Context engineering is the practice of structuring and optimizing the information fed to AI agents, particularly those tasked with code generation and software development. As large language models (LLMs) become central to autonomous coding workflows, understanding how to maximize the utility of context windows becomes critical for solo AI builders and small teams.

What is Context Engineering?

Context engineering differs from traditional prompt engineering by focusing on the architecture of information provided to an agent rather than just the wording of instructions. For coding agents, this means strategically selecting which codebase excerpts, test files, documentation, and prior task outcomes to include in each request.

When an agent operates with limited context, say, a 4K token window, it must choose between:

  • The full codebase structure
  • Relevant function implementations
  • Test cases that define expected behavior
  • Error logs and prior attempt history

Context engineering provides frameworks to make these trade-offs systematically.

Key Patterns for Coding Agents

1. Hierarchical Context Provision

Structure codebase information in tiers: start with high-level architecture and interfaces, include only the immediately relevant modules and functions, then add test cases and error examples as needed. This allows agents to work effectively even with smaller context windows.

2. Relevance-Based Selection

Use static analysis, dependency graphs, or embedding-based similarity to identify which code snippets are most relevant to the current task. Agents that search their own codebase using vector similarity tend to produce more coherent, integrated changes than those given random code samples.

3. Test-Driven Context

Include passing tests alongside code. Tests serve as executable specifications, reducing ambiguity about expected behavior. When an agent understands what must pass, it writes code closer to requirements on the first attempt.

4. Error and Iteration History

Context that includes "we tried X, it failed because Y" helps agents avoid repeating mistakes. This is especially valuable for multi-step coding tasks where earlier attempts inform later strategies.

5. Concrete Examples Over Abstract Instructions

For coding agents, showing a similar working example (e.g., "here's how we handle async in this codebase") is more effective than explaining the pattern in prose.

Practical Techniques

Token Budget Allocation With a typical 100K token context window, allocate roughly:

  • 10-15% to system instructions and task definition
  • 30-40% to relevant codebase excerpts
  • 20-30% to test cases and specifications
  • 20-30% to conversation history and prior attempts

Summarization Layers For large files, provide both a summary (50-100 tokens) and a "drill-down" mechanism so agents can request full details if needed. This keeps context lean while preserving accessibility.

Codebase Indexing Maintain an up-to-date index of your codebase, function signatures, class hierarchies, API definitions. Reference this index in agent context rather than including full file contents. Tools like tree-sitter can automate this indexing.

Session-Level Context Reuse For multi-step workflows (e.g., "write a feature, then tests, then docs"), cache the codebase context across requests using your LLM provider's context caching feature. This reduces redundant token consumption and improves latency.

Why This Matters for Solo Builders

Solo AI entrepreneurs often run tight cost budgets. A poorly engineered context can waste 30-50% of your token spend on irrelevant information. Better context engineering means:

  • Lower costs: Fewer tokens per request, faster completion
  • Better outputs: Agents make fewer off-target mistakes
  • Faster iteration: Lean context windows enable rapid agent refinement
  • Scalability: Techniques that work for a small app scale to larger codebases

Integration with Agent Frameworks

Modern agent frameworks (LangChain, Claude SDK, AutoGen) increasingly provide utilities for context management:

  • Retrieval chains that dynamically fetch relevant code
  • Memory systems that summarize conversation history to stay within windows
  • Caching layers for repeated tasks over the same codebase

When selecting or building an agent framework, prioritize those with explicit context management features.

Common Pitfalls

Over-inclusivity: Including the entire codebase, entire git history, and all documentation. This dilutes signal and wastes tokens.

Static context: Reusing the same codebase excerpt for every agent request, even when the task changes. Dynamic, task-aware selection performs far better.

Neglecting tests: Not including test files assumes agents will infer behavior. Including tests cuts hallucination and bugs significantly.

Ignoring prior failures: If an agent fails a task, the next attempt benefits enormously from knowing why it failed. Log failures and reference them.

Further Reading

Context engineering for coding agents is an emerging discipline. Complementary resources include:

  • Agent framework documentation (LangChain RAG patterns, Claude cookbook examples)
  • Papers on in-context learning and prompt optimization
  • Community discussions in agent-building forums

As LLM context windows grow and new models emerge, the principles of context engineering, relevance, hierarchy, and strategic information provision, remain foundational to building effective coding agents.

Do-Nothing Score

Find out how close you are to Ghost CEO.

Take the quiz