Recipe · recipe:context-engineering

My agent's context decides its quality and I do not know what to put in it each turn

Curated. Written from research and curation. Not run in our harness yet.

The job

Builders put it to us in their own words: "My agent's context is the thing that decides its quality and I do not know what to put in it each turn."

The live argument inside that question is whether the answer is more curation or less. The Anthropic engineering blog reports that for the Claude 5 generation, over 80 percent of Claude Code's system prompt was removed with no measurable loss on its coding evaluations (https://claude.com/blog/the-new-rules-of-context-engineering-for-claude-5-generation-models). That is the direct opposite of the 2024-era advice to write longer, more specific, example-heavy prompts, and any recipe for this job has to survive it.

Recipe state is curated, not tested, in site/recipes/schema.json. We have not run anything in this recipe end to end. Versions, licences and dates are as of 2026-10-14.

Sibling recipes this one is deliberately scoped against: tool use and function calling, retrieval and RAG, adding memory to an agent, evaluating agent quality, multi-agent orchestration. See Scope.

The approach

Recommended: smallest sufficient context, assembled per turn, disclosure deferred, clearing automated

Treat context as a budget you spend on purpose, not a container you fill. Four parts that compose.

First, cut the always-on block to what actually changes behavior. The 80 percent removal result is the evidence that the always-on prefix is usually over-provisioned. Second, defer what you can: keep lightweight identifiers (file paths, stored queries, links, tool names) in context and let the agent pull the body in with a tool when it needs it. This is progressive disclosure, the shift from pre-inference embedding retrieval to just-in-time context. Third, automate the relief valve: use the provider's server-side compaction or context editing rather than maintaining your own summarizer. Fourth, measure the budget: explicit token ceilings per section, and the ratio of signal to plastic tracked per turn.

Why this one:

Switch condition: if your prefix is genuinely stable across requests (fixed agent, fixed toolset), keep the definitions and add prompt caching rather than trimming further. The two compose. Caching is what makes a stable prefix cheap, not an argument for a large one.

Alternative not chosen as the default: assemble everything relevant up front, summarize when it fills

The 2024-era default and still the most common shape in the wild: one large system prompt with every rule and example, every tool definition at the front, retrieved documents pasted in, full message history, and a summarizer that fires when the window is nearly full.

Not the default for three reasons. Context Rot measured degradation across 18 models as input length grows, so a bigger prompt is not a safer prompt at any tier. The 80 percent removal result means the large prefix was mostly cost. And a summarizer-only relief valve is lossy at exactly the moment it matters: the Anthropic engineering post warns that "overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later", and its recommended tuning is to maximize recall first, then iterate on precision.

Keep it for the subset of cases where the context is genuinely small and never grows: a fixed classification or extraction task, a single-turn transform. Below that floor, assembling everything is fine and simpler.

Alternative not chosen as the default: treat it as a retrieval problem, top-k it back

Retrieve top-k chunks each turn from an embedding index, or hand state to a memory service such as mem0 or Letta and pull relevant memories back, and consider the context job done.

This is the strongest approach when the hard part really is that the relevant facts live in a corpus you cannot fit. It is not the default for general agent work because retrieval is one input to context, not the whole of it, and the other inputs are where the failures are: the tool block, the system prompt, the accumulated tool_result blocks, the ordering of what is already in the window, and the lifetime of each piece of state are all context decisions top-k does not touch. The Claude docs ship four separate mechanisms for exactly this reason, because they are four different sources of pressure. Context Rot's finding is also about distractors and position, not about retrieval recall: plausible but irrelevant retrieved text makes a model worse, so a retrieval layer tuned for recall can actively degrade the context it feeds.

Adopt retrieval for the corpus problem, and still do the four things above.

What you need

Every row below has a canonical URL. Where a version or licence could not be confirmed, the row says unconfirmed rather than guessing.

Any platform.claude.com doc page also serves raw markdown by appending .md to the path; those are the variants our research pass rendered.

The MCP TypeScript SDK is listed because tool schemas arriving from MCP servers are one of the largest single context consumers in a typical agent, and therefore one of the main targets for deferred loading.

tiktoken covers the OpenAI family only. The Anthropic tokenizer is not tiktoken; use the provider's own counting for Claude. A budget measured with the wrong tokenizer is not a budget.

Four facts that are traps rather than obvious, and that the recipe depends on:

Two component facts worth stating here rather than burying:

Steps

Steps 1 to 4 cut the always-on block. Steps 5 to 7 defer what can be deferred. Steps 8 to 10 are the relief valve and the measurement.

  1. Write down a context budget before writing any prompt. Name an explicit token ceiling for each of: system prompt, tool definitions, retrieved material, recent turns, free space for reasoning. The target framing is the smallest set of high-signal tokens that maximizes the likelihood of the desired outcome. Without a number per section you cannot distinguish trimming from cutting into muscle.
  2. Count tokens with the tokenizer the model actually uses. Do not estimate from characters. tiktoken (0.14.0, MIT) covers the OpenAI family. The Anthropic tokenizer is not tiktoken, so use the provider's own counting for Claude. Re-measure after every material prompt change.
  3. Cut the system prompt to what changes behavior, then prove the cut. Move rules to judgement, single-source any instruction that appears twice, replace examples with better interfaces where the model supports it. The test is the same one used in the source result: remove a section, run your evaluation, keep the cut only if the eval does not move. Their measured result was over 80 percent removed with no measurable loss on coding evaluations; your number will differ, the method does not.
  4. Decide examples versus interfaces by model capability, and record the decision. For the newest models, examples constrain the exploration space and interface design is the stronger lever: expressive parameters on the tool, script or file rather than a demonstration. For weaker or older models, keep diverse canonical examples. The two sources disagree by model generation, so make this a deliberate per-model choice with a written reason, not a silent one.
  5. Move tool definitions out of the front of every request. Two mechanisms. Deferred loading, where the agent searches for a tool's full definition before use. Prompt caching, where a stable prefix is cached rather than trimmed. Pick by where the tokens are going: tool search for large toolsets where most tools are unused per turn, programmatic tool calling for chains of tool_result roundtrips, prompt caching for stable definitions, context editing for old results.
  6. Keep identifiers in context and bodies out. Store paths, queries, links and handles; fetch the payload with a tool when needed. The named tradeoff is real: runtime exploration is slower than pre-computed retrieval, so use just-in-time where the material is dynamic and pre-compute where it is not. The hybrid reference is CLAUDE.md files up front, glob and grep on demand.
  7. Move durable state out of the prompt entirely. Session objectives, decisions, open questions, known failures and next actions belong in a file or store the agent reads and writes, not in a growing transcript. A NOTES.md the agent maintains, or a to-do list, pulled back in when relevant, is the documented pattern.
  8. Turn on the server-side relief valve, and know which one you turned on. Compaction replaces older turns with a server-written summary (beta header compact-2026-09-04). Context editing clears by rule instead of summarizing (beta header context-management-2025-06-27, clear_tool_uses_20250919 and clear_thinking_20251015). Do not enable both on one request; the docs state they cannot be combined. Clear tool results and old thinking first, because that is the cheapest and least lossy cut, then compact.
  9. Tune the summarizer against real traces, recall first. Maximize recall so the compaction captures every relevant piece of the trace, then iterate on precision to remove superfluous content. Keep the parts of the history that are still live: on-demand compaction can keep recent turns word for word and can run in the background so the conversation does not pause.
  10. Measure the ratio, not just the total, and watch the cache. Track tokens per section per turn and the signal-to-plastic ratio, so bloat is visible before it appears on a bill. Check the prompt cache hit rate alongside it, because a compaction or a context edit rewrites the prefix and the default 5-minute TTL leaves little slack. If edits keep busting the cache, move them earlier in the prefix or batch them at a turn boundary.

Traps

Trap 1: Believing more context is safer

Trap 2: Writing an ever-longer system prompt to fix each failure

Trap 3: Carrying the whole transcript because it fits

Trap 4: Compacting too aggressively and losing the thing that mattered

Trap 5: Destroying the prompt cache between turns without noticing

Trap 6: Turning on compaction and context management together

Trap 7: Loading every tool definition on every request

Trap 8: Assuming examples are always the cheap way to steer

Trap 9: Treating context engineering as retrieval and stopping there

Trap 10: Mirroring server-side edits in client state

Trap 11: Trusting a licence you did not read

Trap 12: Thinking the job is solved

Scope

Covers

Does not cover

Sources

Hacker News points, dates and comment counts above are attributed to the source brief's 2026-10-14 Algolia reads. They were not re-queried for this recipe, so no page text rests on them as independently confirmed this pass.

Not cited: Reddit. Every fetch returned HTTP 403 on 2026-10-14, with and without a browser user agent, so no Reddit thread appears above even though it is where the question is asked most informally. Also not cited: zeli.app and hackernews.betacat.io, which are aggregators rather than originals.