Zenaique

Spot the context engineering error in this tool result handling code

Spot the error·Medium·4.0 · 0·~2 min·Asked atSnowflakeTypeface
Attempt it

Click any words you think contain an error. Click again to unmark.

Mark at least one word to submit.
TL;DR

The snippet puts growing tool results into the system prompt, which breaks prompt caching and pushes the rest of the conversation into the lost-in-the-middle trough at the same time.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Picture a noticeboard at a coffee shop with a sign that says 'this stays the same all day.' If the barista starts pinning every customer's receipt to that board as the day goes on, two things break. The 'stays the same' promise is gone, so any system that was relying on it stops working. And the board fills up so fast that the original menu, the thing customers actually need, is buried under a day's worth of receipts. The system prompt is that noticeboard. Tool results are the receipts. They belong in a different place, and old ones should come down when they are no longer useful.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

This snippet is short, but it violates three distinct context engineering principles at once. The violations interact in a way that makes the design strictly worse than any one violation alone.

The design says: (1) tool results go in a persistent list in the system prompt, (2) tool results are never removed, and (3) the same system prompt is sent every turn for prompt-caching benefits. Claims 1, 2, and 3 cannot all be true at the same time. The system prompt cannot be both "the same on every turn" and "the place where new tool results are appended on every turn."

Why the prompt-caching claim is structurally impossible

Prompt caching on Anthropic, OpenAI, and Google all share one property: the cache key is the literal byte sequence of the cached prefix. When the prefix matches byte for byte across two requests, the second request reuses the cached state. When it differs by even one character, the cache misses and the model recomputes from scratch.

The snippet says the system prompt is sent the same every turn. The snippet also says tool results are appended to a list inside the system prompt on every turn. Both cannot be true. After the first tool call, the system prompt has one extra result. After the second, it has two. The byte sequence changes at the list on every request, so the list and everything after it miss the cache. Only the static text before the list can still hit.

The error survives because each half of the design looks correct in isolation. "Cache the system prompt for cost savings" is a textbook-correct move. "Put persistent state in the system prompt so it is always available" is plausible-sounding. The combination is incoherent.

The fix is to move the mutating content out of the cached span. Tool results go into the conversation message sequence, in the uncached tail. The system prompt becomes truly byte-stable and the cache actually hits.

Why the system prompt is the wrong home for tool results regardless
The missing eviction policy
The fix and the framework lesson
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Anthropic's Claude tool-use format threads tool_result blocks directly into the conversation, with the system prompt reserved for stable instructions and tool schemas sent in the separate tools parameter.
  • OpenAI's Responses API represents tool calls and their outputs as function_call and function_call_output items in the input sequence, never as system-prompt content.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QIf you wanted some tool results to be durable across many turns, what is the right way to do it without breaking the system-prompt cache?
A

Store the durable observations in agent state (LangGraph typed state, a memory store, a vector index) rather than in the prompt text. Expose a recall_observation(id) tool the model can call when it needs an older result. The system prompt stays cacheable, and the model fetches durable content on demand into the recency end of context where attention is strong.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Conflating 'I want this content to be cached' with 'put it in the system prompt'. Caching only works while the block is byte-stable, which a growing tool log never is.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Identify which property of prompt caching the snippet violates

  • Explain why the system prompt is the wrong location for mutating tool results

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
List the trace spans a context engineering pipeline should emit per call
Flashcard·Medium