Skip to content
All posts

· 9 min read · Aravind Baranitharan

context engineering.

Prompt engineering is choosing the words. Context engineering is choosing everything the model sees on every turn: instructions, tools, memory, documents and history, inside a fixed budget.

  • Context engineering
  • AI agents
  • Retrieval
  • Architecture

A model knows nothing about your task except what's in its context window on this turn. Not your codebase, not yesterday's decision, not the tool result from twenty steps ago, unless something put it there. Context engineering is deciding what that something is, every turn, under a fixed budget.

Prompt engineering asks "what words should I write?" Context engineering asks "of everything this agent could see, what should it see right now, in what form and in what order?" For agents that run many steps, that second question decides most of the quality. These are my notes on how to answer it. Every section is open for feedback at the bottom.

The context window is a budget

Big windows make it tempting to include everything. Three things push back:

  • Attention isn't even. Lost in the Middle showed models use information at the start and end of a long context better than what's in the middle. Quality also tends to fall as inputs grow, even on easy tasks.
  • Distraction. Irrelevant but plausible text pulls the model off course. A wrong document is worse than no document.
  • Cost and latency. Every token is paid for on every call, and an agent makes a lot of calls.

What competes for the window

  • Instructions: the system prompt, rules and examples. Stable across turns.
  • Tool definitions: every tool's name, description and schema costs tokens on every call, used or not.
  • Memory: facts and lessons from earlier sessions. See agentic memory.
  • Retrieved documents: whatever search found for this step.
  • History: the conversation and the agent's own earlier steps.
  • Tool results: often the biggest and least curated item. One careless API response can fill half the window.

Four moves: select, compress, order, isolate

  • Select. Include only what this step needs. Load tools for the current phase, not all forty. Retrieve for the sub-task, not the whole goal.
  • Compress. Summaries instead of transcripts, IDs instead of full records, diffs instead of whole files, structured fields instead of raw HTML.
  • Order. Stable content first (instructions, tools), the task and fresh results last, where the model reads them best. A stable prefix also lets prompt caching cut cost and latency.
  • Isolate. Give a sub-agent a clean window for a messy sub-task, and have it return a short note instead of its whole working history.

Compression is where I've seen the biggest wins. In the Figma-to-code agent, a custom parser translates Figma layouts into compact, LLM-ready structure so large designs stay inside the context window. Raw design files are huge; the model only needs the structure that matters for code.

A context pipeline

In practice context engineering is a small pipeline that runs before every model call: gather candidates, apply the four moves, assemble the window.

Context engineering pipelineContext engineering pipeline: six sources (instructions, tool definitions, memory, retrieved documents, history and tool results) pass through four operations (select, compress, order and isolate) to build a budgeted context window with a stable cached prefix at the top, the task and fresh material at the bottom, and headroom left for the answer.all of itthis turn01sourcesinstructionsstable · cache ittool definitionsonly tools for this stepmemoryranked, budgetedretrieved docstop-k, re-rankedhistorysummarise old turnstool resultstrimmed at the source02engineerselectwhat does this stepactually need?compresssummaries, ids, diffsinstead of raw dumpsorderstable prefix first,the ask lastisolatesub-agents get cleanwindows, return notes03context windowsystem + rulestoolstask + planretrieved + memoryrecent turnslatest resultheadroomstable on top · freshest last · room to think
Fig. 1 · Everything the agent could see, cut down to what it should see. Numbers match the walkthrough below.
  1. 01

    Sources

    Everything the agent could see. Each source gets its own rule: instructions are cached, tools are filtered by phase, memory and documents are ranked, history is summarised, tool results are trimmed before they arrive.

  2. 02

    Engineer

    Select, compress, order, isolate. Make this an explicit function in your code, not something that happens by accident as messages pile up. Then you can log it, test it and change it.

  3. 03

    Context window

    A stable prefix of rules and tools, then the task and plan, then retrieved material and recent turns, then the latest result, with headroom left for the model to think and answer. Give each section a token budget.

Keeping long runs clean

  • Compact before you hit the limit. When history grows, replace old turns with a summary of decisions, open threads and facts learned. Keep the last few turns verbatim.
  • Write state outside the window. A task ledger or notes file the agent reads and updates beats hoping the history still contains the plan. This is the long-horizon checkpoint idea applied to context.
  • Clear stale tool results. Once the agent has acted on a 5,000-token search result, the raw result usually isn't needed again. Keep a one-line note of what it showed.
  • Watch for poisoning. A hallucination or wrong fact that lands in context gets repeated and built on. Verify before you summarise it into something permanent.

How context goes wrong

  • Overflow: the window fills, the provider truncates, and the instructions at the top quietly disappear.
  • Distraction: loosely related documents pull the model toward the wrong file, customer or question.
  • Confusion: forty similar tools, and the model picks the wrong one or invents arguments.
  • Clash: two sources disagree (an old memory and a fresh result) and nothing tells the model which wins. Put dates on things and state the rule.
  • Invisibility: nobody can see what the model actually saw. Log the assembled context for every call; you can't debug what you can't see.

What I'd build first

  1. Log the full context of every call and look at a few by hand. Most people are surprised by what's in there.
  2. Token budgets per section, with a warning when one overflows.
  3. Trim tool results at the source: pagination, field selection and sensible defaults.
  4. History compaction with a summary that keeps decisions and open questions.
  5. Phase-based tool loading and sub-agents, once the basics are measured. Check every change against your evals.

Open questions

  • Should the model manage its own context, deciding what to keep and drop, or should code do it?
  • How do you test a summariser so compaction doesn't lose the one detail that matters later?
  • When do sub-agents help, and when does the hand-off lose more than the clean window gains?
  • As windows keep growing, which of these techniques still matter? My bet is all of them, for cost and focus if nothing else.

Rate the post, start a thread on any concept below, or share it with someone whose agent gets worse the longer it runs. I'll update it as the discussion changes my mind.

Discussion

feedback.

Rate the post, then start a thread on any concept. Disagreements are especially welcome. No sign-up needed.

Reader rating

–No ratings yet. Be the first.

How useful was this?

Start a thread

Loading the discussion…