Context Engineering
Curating what enters the window: instructions, tools, memory, and retrieved evidence.
In plain words
It's choosing exactly what to show the AI each time, so it has what it needs and none of the clutter.
Think of it like…
Preparing a briefing for a busy executive: include the decision needed, the three facts that matter and one-page evidence, not the whole filing cabinet. And update it as the situation changes rather than stapling new pages onto an old pile.
Where you’ve seen it
A coding agent kept failing on a large repository. Its context had grown to 90,000 tokens: full file contents, repeated test logs and old plans. After a redesign it keeps a 1,500-token system prompt, a 300-token task brief, a 'repo map' of file names and function signatures, only the files it is editing (retrieved on demand with search tools), the last test failure trimmed to the first error and stack frame, and a notes file it updates with decisions. Average context drops to 14,000 tokens, tasks complete 22 percent more often and cost per task falls by about 60 percent.
Words you’ll hear
- Context budget
- A plan for how many tokens each type of content may use.
- Just-in-time retrieval
- Loading data when needed through tools, instead of preloading everything.
- Context rot
- Declining accuracy as the window fills with long or irrelevant content.
- Signal-to-noise
- The share of tokens that actually help the current step.
Visual diagram
Theory & deep dive
Components and principles
The context holds: system instructions (clear, specific, free of contradictions, with examples that anchor behaviour); tool definitions (few, non-overlapping, with precise descriptions; every tool costs tokens and decisions); knowledge (retrieved passages, file excerpts); memory (profile, notes, summaries); history (recent messages, tool results); and state (plan, progress, constraints). Principles: be specific but not brittle; prefer the minimum that works; put stable material first (enabling prompt caching) and critical constraints where they won't be lost; format for the model (clear headings or XML-style tags, consistent structure); and remove what has stopped being useful.
Prefer just-in-time context: give the agent lightweight handles (file paths, ids, search tools) and let it load details when needed, rather than preloading large datasets "just in case". Combine with progressive disclosure: start from a summary or map, drill down into detail on demand. For very long tasks, use notes outside the context (a scratchpad file or memory store the agent writes to and reads from) and sub-agents that explore with their own clean windows and return distilled summaries.
Provide src/orders/service.py: create_order(), cancel_order(), refund_order() (names and signatures only) for the whole repository in about 2,000 tokens, plus a read_file tool. The agent fetches full code only for files it needs.
Diagnosing and improving context
Treat context as an engineering artefact you can inspect. Log the exact prompt sent for failing cases and ask: is the needed fact present? Is it findable (not buried in 40,000 tokens)? Is anything contradictory or outdated? Do tool results contain more than the model needs? Measure tokens per component, and run ablations: remove a section and see whether quality changes. Often removing content improves results.
Common fixes: trim and structure tool outputs (return the relevant fields, not whole JSON dumps); deduplicate repeated information; summarise or clear old turns and stale tool results (see context compaction); consolidate overlapping tools; split a big task into stages with fresh contexts; reorder so critical instructions come first and key constraints are restated near the end; and use retrieval with reranking to bring in only the top passages. Re-evaluate on a fixed test set after each change, since context changes can shift behaviour in unexpected ways.
A run_tests tool returned 6,000 tokens of logs. Returning only {passed: 41, failed: 1, first_failure: {test, message, top 5 stack frames}} (about 250 tokens) kept all useful signal and removed the noise.
How it works, step by step
Step 1 of 41. Inspect the real prompts
Log the exact context for failing runs and measure tokens per component.
- Look for
- noise and gaps
When to use it & trade-offs
What it is
Context engineering is the discipline of deciding what goes into the model's context window at each step: instructions, tool definitions, retrieved evidence, memory, conversation history and the current state, in what form, in what order and in what quantity. It extends prompt engineering from writing one good prompt to curating a dynamic information environment, particularly for agents whose context grows over many steps. The goal is the smallest set of high-signal tokens that lets the model do the next step well.
Why it matters: Model quality depends heavily on what's in front of it. Too little context and the model guesses; too much (or irrelevant) context and it gets distracted, loses instructions in the noise ("context rot"), runs slower and costs more. Most agent failures that look like model weakness are context failures: stale tool results, contradictory instructions, a thousand-line log pasted in, or the key constraint buried in the middle.
When to reach for it
- Coding and research agents over large corpora
- Long conversations and multi-step tool use
- Reducing cost and latency without changing the model
When to avoid or be careful
- Preloading everything "just in case"
- Overlapping tools with vague descriptions
- Pasting raw logs and JSON dumps into context
Prompt engineering vs context engineering
Strengths: Focused on wording and examples.
Watch out: Doesn't manage growing, dynamic context.
Strengths: Manages the whole information environment per step.
Watch out: Requires instrumentation and evaluation.
Strengths: Fits more.
Watch out: Doesn't fix noise, cost or attention problems.
Interview signals & numbers to know
When this topic comes up
- “The agent gets worse as the task gets longer”
- “Context window is full of tool output”
- “What's context rot?”
- Just-in-time retrieval
- Prompt caching strategy
Benchmarks & rules of thumb
- 90K → 14K
- average context tokens in the coding example
- +22%
- task completion improvement in the example
- −60%
- cost per task in the example
Common mistakes & follow-ups
Common interview mistakes
- Adding more instructions after each failure until they contradict each other.
- Returning whole API payloads from tools.
- Assuming a larger window means the model uses every token well.
Likely interviewer follow-ups
- How is context engineering different from prompt engineering?
- What would you do if an agent degrades on long tasks?
- How do sub-agents help manage context?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Context Engineering with generative AI interview Q&A and hands-on practice.