Summary

Context engineering is the practice of curating the smallest high-signal context set for each turn instead of stuffing everything into the prompt. It treats context as a finite resource to be budgeted, scored, and refreshed rather than a dumping ground for every potentially useful fact.

How it works

  1. Inventory context sources -- list all possible context inputs: conversation history, retrieved documents, tool outputs, system prompts, user profile data.
  2. Score by relevance -- for each turn, score each context source on its relevance to the current query or task.
  3. Budget allocation -- allocate a limited context window based on scores, ensuring the most critical information fits.
  4. Summarize if needed -- when relevant context exceeds the budget, summarize the lowest-scoring items to preserve signal.
  5. Deliver -- assemble the final context set and deliver it to the model in a consistent structure.

Key principles

  • Minimal viable context: Only include context the model demonstrably needs for the current turn.
  • Stale context detection: Flag and discard context that has been superseded or is older than a threshold.
  • Budget enforcement: Treat the context window as a hard constraint and prioritize aggressively.

Common pitfalls

  • Overstuffing: Including too much context dilutes signal and increases cost and latency.
  • Stale tool output: Past tool results that no longer reflect the current state mislead the model.
  • Redundant context: Repeating information across different context sources wastes budget.

Build This Pattern

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.

Build a context engineering workflow for optimal prompt composition. ROLE: You are a context management system that filters, truncates, and prioritizes context before each LLM call to maximize signal-to-noise ratio. CONSTRAINTS: - Context budget: configurable per task; default 8000 tokens - Scoring system: rank context items by relevance to current query - Stale context detection: identify and flag outdated information - Tool output poisoning prevention: sanitize outputs before adding to context - Log context composition per turn for analysis TOOL CALLING: - Use function calling for: score_context(items[], query), filter_context(scored_items[], budget), detect_staleness(items[]), sanitize_tool_output(output), get_context_stats(conversation_id?) - Each tool returns structured JSON with context data and metadata STRUCTURED OUTPUT: - Scored context must return JSON: { items: [{ id: string, content: string, score: number, source: string, created_at: string }], total_score: number, token_count: number } - Filtered context must return JSON: { included: [{ id: string, content: string, score: number }], excluded: [{ id: string, reason: string }], total_tokens: number, budget_used: number } - Staleness report must return JSON: { items: [{ id: string, content: string, staleness_score: number, last_updated: string, recommendation: 'keep' | 'flag' | 'remove' }] } - Context stats must return JSON: { conversation_id: string, total_items: number, avg_score: number, budget_utilization: number, stale_items: number, turns_analyzed: number } CHAIN OF THOUGHT: - Scoring: analyze query → match against context items → calculate relevance scores → rank - Filtering: apply budget limit → include high-score items → exclude low-score → log decisions - Staleness detection: check timestamps → compare to current state → flag outdated information - Sanitization: receive tool output → validate structure → remove sensitive data → add to context FEW-SHOT EXAMPLES: Query: 'How do I deploy the API to production?' Context items: [ { id: 'c1', content: 'API uses Express.js with TypeScript', score: 0.92, source: 'codebase' }, { id: 'c2', content: 'Deployment uses Docker and AWS ECS', score: 0.95, source: 'docs' }, { id: 'c3', content: 'User asked about weather yesterday', score: 0.05, source: 'conversation' } ] Filtered: { included: [c1, c2], excluded: [c3], budget_used: 0.25 } EVALUATION CRITERIA: - Relevance accuracy: percentage of included context items that are relevant to the query - Budget efficiency: percentage of token budget used effectively - Staleness detection: percentage of outdated items correctly identified - Poisoning prevention: percentage of malicious tool outputs caught The system should: 1) Implement context manager that filters, truncates and prioritizes context before each LLM call, 2) Use scoring system to rank context items by relevance, 3) Include budget limits for total context size, 4) When context budget exceeded, implement summarization fallback, 5) Handle empty context gracefully, 6) Detect stale context, 7) Prevent context poisoning from tool outputs, 8) Log context composition per turn, 9) Measure signal-to-noise ratio, 10) Verify context budgets with max-size inputs.