Context Engine - AI Agent Context Management
Tier: POWERFUL Category: Engineering Tags: context management, AI agents, memory systems, RAG, token optimization, knowledge graphs
Overview
Context Engine provides production-grade patterns for managing what AI agents know, remember, and retrieve. It covers the full lifecycle: ingestion of project knowledge, optimal packing of context windows, persistent memory across sessions, and retrieval-augmented generation for large codebases. The difference between a useful agent and a hallucinating one is context management.
Core Capabilities
1. Context Window Architecture
Every AI agent operates within a finite context window. Mismanaging it is the #1 cause of degraded agent performance.
Token Budget Allocation Framework
| Segment | Budget % | Purpose | Priority | |---------|----------|---------|----------| | System Instructions | 5-10% | Agent identity, rules, constraints | Fixed (always loaded) | | Task Context | 20-30% | Current task description, requirements | High (per-request) | | Relevant Code | 25-40% | Source files, dependencies, types | Dynamic (retrieved) | | Conversation History | 10-20% | Prior turns, decisions made | Sliding window | | Tool Results | 5-15% | Command output, search results | Ephemeral | | Reserved Buffer | 5-10% | Output generation headroom | Protected |
Context Packing Strategies
Greedy Relevance Packing
1. Score all candidate context by relevance to current task
2. Sort by score descending
3. Pack until budget exhausted
4. Always reserve output buffer
- Pros: Simple, fast, works well for focused tasks
- Cons: Misses cross-cutting context, no diversity
Tiered Loading
Tier 0 (always loaded): System prompt, project rules, active file
Tier 1 (task-specific): Related files, type definitions, tests
Tier 2 (on-demand): Documentation, examples, history
Tier 3 (retrieved): Search results, RAG chunks
- Pros: Predictable, debuggable, respects fixed costs
- Cons: Requires upfront tier classification
Adaptive Compression
1. Load full context for first pass
2. Identify low-signal sections (boilerplate, repetitive code)
3. Summarize or truncate low-signal sections
4. Re-pack with compressed context
5. Preserve high-signal sections verbatim
- Pros: Maximizes information density
- Cons: Risk of losing important details in compression
2. Memory Architecture
Three-Layer Memory Model
┌─────────────────────────────────────────────────┐
│ Layer 1: Working Memory (Context Window) │
│ Scope: Current conversation/task │
│ Lifetime: Single session │
│ Storage: In-context tokens │
│ Update: Every turn │
├─────────────────────────────────────────────────┤
│ Layer 2: Session Memory (Persistent Store) │
│ Scope: Project-level learnings │
│ Lifetime: Across sessions │
│ Storage: MEMORY.md, .claude/rules/, CLAUDE.md │
│ Update: End of session or on discovery │
├─────────────────────────────────────────────────┤
│ Layer 3: Knowledge Base (Indexed Corpus) │
│ Scope: Full codebase + documentation │
│ Lifetime: Persistent, versioned │
│ Storage: Vector store, graph DB, file index │
│ Update: On commit / scheduled reindex │
└─────────────────────────────────────────────────┘
Memory Promotion Protocol
Knowledge flows upward through layers based on recurrence and value:
| Signal | Action | Example | |--------|--------|---------| | Pattern seen 1x | Working memory only | "This file uses tabs" | | Pattern seen 2-3x | Candidate for session memory | "Project uses pnpm everywhere" | | Pattern confirmed across sessions | Promote to CLAUDE.md/rules | "Always use pnpm, never npm" | | Pattern is domain knowledge | Add to knowledge base | "Auth flow uses JWT + refresh tokens" |
Staleness Detection
Context has a shelf life. Stale context causes hallucinations.
Freshness Score = f(last_verified, change_frequency, confidence)
Fresh (< 7 days, file unchanged): Use directly
Aging (7-30 days, file changed): Re-verify before using
Stale (> 30 days): Flag, re-retrieve, or discard
Unknown (never verified): Treat as low-confidence
3. Retrieval Strategies for Code
File-Level Retrieval
Best for: navigating to the right file when the agent knows what it needs.
Query: "authentication middleware"
Strategy:
1. Filename pattern match: *auth*, *middleware*
2. Import graph: files that import auth modules
3. Symbol search: exported functions matching auth*
4. Content search: files containing auth-related patterns
5. Rank by: recency of edit + import centrality + name match
Chunk-Level Retrieval (RAG for Code)
Best for: finding specific implementations within large files.
Chunking Strategy for Source Code:
- Chunk by function/class boundaries (never mid-function)
- Include the function signature + docstring + body as one chunk
- Attach metadata: file path, language, exports, imports
- Overlap: include 2 lines above/below for context
- Max chunk size: 200 lines (larger functions get sub-chunked by logical block)
Embedding Considerations:
- Code-specific embeddings (CodeBERT, StarCoder embeddings) outperform general text embeddings by 15-30% on code retrieval tasks
- Hybrid search (keyword + semantic) outperforms either alone
- Index function signatures separately for fast symbol lookup
Dependency-Aware Retrieval
When retrieving a function, also retrieve:
- Its type definitions (interfaces, types it uses)
- Its direct dependencies (imported functions it calls)
- Its tests (to understand expected behavior)
- Its callers (to understand usage context)
This "context neighborhood" approach prevents the agent from seeing a function in isolation.
4. Knowledge Graph Construction
Codebase Graph Schema
Nodes:
- File (path, language, size, last_modified)
- Function (name, signature, docstring, complexity)
- Class (name, methods, properties, inheritance)
- Module (name, exports, dependencies)
- Test (name, covers, assertions)
- Config (type, values, affects)
Edges:
- IMPORTS (File → File)
- CALLS (Function → Function)
- IMPLEMENTS (Class → Interface)
- TESTS (Test → Function)
- CONFIGURES (Config → Module)
- DEPENDS_ON (Module → Module)
Graph Queries for Context
| Agent Question | Graph Query | Context Retrieved | |---------------|-------------|-------------------| | "How does auth work?" | Subgraph around auth module, 2 hops | Auth files + dependencies + tests | | "What breaks if I change X?" | Reverse dependency traversal from X | All callers + their tests | | "What's the API surface?" | All exported functions from API modules | Route handlers + types + middleware | | "How is this tested?" | TEST edges from target function | Test files + fixtures + mocks |
5. Context Window Optimization Patterns
Pattern: Sliding Window with Anchors
For long conversations, maintain fixed "anchor" messages while sliding recent history.
[System Prompt] ← Fixed anchor (never evicted)
[Task Definition] ← Fixed anchor
[Key Decision #1] ← Pinned (user marked as important)
[Key Decision #2] ← Pinned
...
[Turn N-4] ← Sliding window starts here
[Turn N-3]
[Turn N-2]
[Turn N-1]
[Current Turn]
[Output Buffer] ← Reserved
Pattern: Progressive Summarization
When conversation exceeds budget:
- Summarize oldest turns into a "conversation summary" block
- Keep the summary as a single anchor message
- Update summary every N turns
- Always keep: first system message, task definition, last 5 turns
Pattern: Selective Tool Result Caching
Tool outputs (file reads, search results, command output) consume the most to