Best Practices
The difference between agents that reliably ship code and agents that spin their wheels? Context. Learning to engineer contextâand structure your code to help agents succeedâmakes agentic development predictable.
Context engineering
Section titled âContext engineeringâContext engineering is the discipline of providing AI agents with the right information at the right time. Itâs not just prompt engineeringâitâs the entire system of what an agent knows when it makes decisions.
Think of it this way: an agentâs context window is its entire working memory. Everything it can âseeâ at onceâyour instructions, the code, conversation history, tool outputsâshapes every decision it makes. Engineer that context poorly, and even the smartest model produces garbage.
Why context matters more than prompts
Section titled âWhy context matters more than promptsâA perfect prompt with bad context fails. A mediocre prompt with excellent context often succeeds.
Context includes:
- System instructions â The agentâs baseline behavior and constraints
- Codebase knowledge â Files, patterns, conventions the agent can reference
- Conversation history â Whatâs been discussed and decided
- Tool outputs â Results from file reads, command execution, searches
- External documentation â API docs, specs, requirements
Prompts are just one piece. The agentâs entire context window determines output quality.
The context engineering workflow
Section titled âThe context engineering workflowâ1. Curate what goes in
Not everything belongs in context. More isnât betterâitâs often worse. Irrelevant context dilutes signal and wastes tokens.
Ask: What does the agent need to complete this task? Include that. Exclude everything else.
2. Structure for retrieval
Agents scan context looking for relevant information. Make it findable:
- Use clear headings and sections
- Put critical constraints early
- Group related information together
- Use consistent formatting
3. Manage context lifecycle
Context windows fill up. When they do, older content gets truncatedâthe agent literally forgets it.
- Start fresh conversations for new tasks
- Summarize long discussions before continuing
- Re-state critical constraints when context is getting full
- Use tools like AGENTS.md to persist important context across sessions
Practical context techniques
Section titled âPractical context techniquesâInclude relevant code, not all code. If youâre modifying a function, include that function and its direct dependencies. Donât dump the entire codebase.
Provide examples of desired output. Show the agent what good looks like. A single example often beats paragraphs of description.
State constraints explicitly. âDonât modify the public APIâ is context. âFollow the error handling pattern in auth.tsâ is context. Make implicit knowledge explicit.
Use documentation as context. When working with unfamiliar APIs, paste relevant docs into the conversation. The agent canât hallucinate whatâs right in front of it.
Leverage persistent context files. AGENTS.md, README files, and similar documents provide context that persists across sessions. Keep them current.
Context anti-patterns
Section titled âContext anti-patternsâKitchen sink context â Dumping everything âjust in case.â Dilutes signal, wastes tokens, confuses the agent.
Stale context â Outdated documentation or code that contradicts current state. Leads to hallucinations and wrong assumptions.
Implicit context â Assuming the agent knows things you havenât told it. Your mental model isnât in the context window.
Context fragmentation â Spreading related information across many messages. Group related context together.
Failure modes to watch
Section titled âFailure modes to watchâAgents fail in predictable ways. Learning these patterns helps you catch problems early.
Hallucinations
Section titled âHallucinationsâAgents use APIs, methods, or parameters that donât exist. Code looks right but fails at runtime.
Triggers: Less popular libraries, recently changed APIs, domain-specific tools.
Catch it: Be suspicious of unfamiliar method names. Verify imports exist. Run the codeâdonât just read it.
Prevent it: Include relevant docs in your context. Use well-known patterns. Ask the agent to explain its reasoning.
The agent starts solving your problem but gradually shifts to a different, easier problem.
Triggers: Long prompts with multiple objectives. Ambiguous requirements.
Catch it: Re-read the original requirement after each iteration. Ask: âDoes this still address my actual problem?â
Prevent it: Break into smaller, focused pieces. State success criteria explicitly.
Looping
Section titled âLoopingâThe agent tries the same failing approach repeatedly with minor variations.
Triggers: Errors the agent doesnât understand. Tasks beyond capability. Missing context.
Catch it: Track iterationsâif youâre past 3-4, somethingâs wrong.
Prevent it: Provide error context. Break out manually and try a different approach.
Overconfidence
Section titled âOverconfidenceâThe agent declares success when code doesnât work, or claims certainty about incorrect information.
Catch it: Never trust self-assessment. Run the code yourself. Write tests for actual requirements.
Prevent it: Ask how it verified correctness. Request specific edge case tests.
Context overflow
Section titled âContext overflowâEarly context gets âforgottenâ as conversations grow. Agent contradicts earlier decisions or ignores constraints.
Triggers: Long conversations. Large codebases in context. Complex multi-step tasks.
Catch it: Watch for inconsistencies. Notice when agent asks for info you already provided.
Prevent it: Keep conversations focused and short. Start fresh for new tasks. Re-state critical context.
Plausible nonsense
Section titled âPlausible nonsenseâCode looks sophisticated but fundamentally misunderstands the problem or domain.
Catch it: Trace logic mentallyâdoes it make sense? Get domain expert review.
Prevent it: Provide domain context explicitly. Include examples of correct solutions.
Building AI-compatible code
Section titled âBuilding AI-compatible codeâThe same patterns that help new developers help agents: clear structure, explicit types, good documentation, consistent patterns.
Structure
Section titled âStructureâKeep files focused. One concept per file. Agents request specific filesâmake those requests useful.
Use descriptive names. UserAuthenticationService.ts beats uas.ts. Agents infer purpose from names.
Flatten when possible. Deep nesting forces agents to understand hierarchy.
Keep functions short. Under 50 lines. Single responsibility. Clear inputs and outputs.
Types and contracts
Section titled âTypes and contractsâUse types generously. TypeScript, Python type hints. Types are machine-readable documentation.
// Harder for agentsfunction process(data) { /* what's data? */}
// Easier for agentsfunction processOrder(order: Order): ProcessedResult { /* clear context */}Define interfaces at boundaries. Explicit interfaces prevent integration bugs.
Avoid any/unknown. More type information enables better inference.
Documentation
Section titled âDocumentationâDocument the âwhy,â not the âwhat.â Agents read what code does. They canât read your mind.
// Less useful// This function calculates the discountfunction calculateDiscount(order: Order): number;
// More useful// Business rule: Premium customers get 10% off orders over $100// This discount stacks with promotional codesfunction calculateDiscount(order: Order): number;Keep READMEs current. Agents often start by reading them. Outdated docs mislead.
Document non-obvious constraints. Rate limits, known limitations, things that âjust work that way.â
Testing
Section titled âTestingâWrite tests as specification. Tests tell agents what code should do.
Keep tests fast. Agents run tests frequently. Slow tests break feedback loops.
Test edge cases explicitly. Tests covering edges tell agents about edges they might miss.
Use descriptive names. test_user_creation_fails_with_duplicate_email beats test_user_3.
Patterns to avoid
Section titled âPatterns to avoidâ- Magic and metaprogramming â Dynamic method generation, heavy reflection confuse agents
- Implicit dependencies â Service locators, ambient context hide needed information
- Circular dependencies â Confuse agents about what depends on what
- Inconsistent patterns â If you do the same thing three ways, agents wonât know which to follow
Incremental improvement
Section titled âIncremental improvementâYou donât need to rewrite everything. As you touch code:
- Improve the file youâre modifying
- Add types where you add code
- Write a test when you fix a bug
- Update docs when you discover theyâre wrong
Over time, the codebase becomes more agent-friendlyâand more human-friendly.
Resources
Section titled âResourcesâContext engineering
Section titled âContext engineeringâ- Context Engineering for AI Agents â Tobi Lutke - Shopify CEO on why context engineering is the new skill
- Context Engineering â Andrej Karpathy - âPrompt engineering is dead, context engineering is kingâ
- AGENTS.md - Open format for persistent agent context, used by 60k+ open-source projects
Failure modes and recovery
Section titled âFailure modes and recoveryâ- âI shipped code I donât understandâ â Jake Nations, Netflix - The âInfinite Software Crisisâ and how to avoid it
- Agent Readiness â Eno Reyes, Factory AI - Eight categories for agent-ready codebases
Deep dives
Section titled âDeep divesâ- No Vibes Allowed â Dex Horthy, HumanLayer - Making agents work in 300k LOC codebases
- RepoCoder: Repository-Level Code Completion - Framework for leveraging repository context