Repository navigation
Engines: cache Claude's prompt prefix, hold the clearing boundary, stop the per-step snapshot - #2661
Merged
Merged
Conversation
…rop the per-step snapshot Prompt caching. agent-langgraph-ts, agent-langgraph-agui and agent-mastra sent Claude no cache_control, and the model gateway passes the body through unchanged, so every step of a turn was billed in full. Measured on Anthropic's API through a gateway-style passthrough: a two-step tool turn sent 18,796 and 18,864 input tokens and read none from the cache. Each engine now marks the last tool, the system prompt and the conversation (top-level cache_control): the second step reads 18,792 tokens back (langgraph-ts), 13,920 (langgraph-agui) and 11,879 (mastra). A proxy that refuses the field is remembered for the process and asked again unmarked. OpenAI and Gemini are sent nothing new. Clearing hysteresis. Past 100,000 tokens the local rule in context-room.ts cleared all but the last ten results on every step, so each new result pushed one more out and the cached prefix changed on every step. It now clears the oldest results down to 60,000 and holds the boundary until the sent context passes 100,000 again, worked out from the thread alone so every run of a turn lands on the same boundary. A 200-step task now changes its prefix on a handful of steps instead of nearly all of them. Claude keeps Anthropic's own clearing, which measured live holds its prefix past the trigger. No MESSAGES_SNAPSHOT. agent-langgraph-ts ended every run with a snapshot of the whole thread, and every step of a long task is a run, so a turn streamed about N^2/2 messages back. The events already build every message a step adds in the caller's thread (text, calls, recall results, and Gemini's thought signature via REASONING_ENCRYPTED_VALUE), which the server and the browser apply with @ag-ui/client as they do for any agent that sends no snapshot. Tests now read the thread the caller holds after applying the events with @ag-ui/client itself.
davidmckayv
requested review from
MikeRyanDev,
dustywusty,
guidovizoso,
mxmzb,
samjulien and
tylerslaton
as code owners
December 1, 2026 22:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three findings from the waste audit after 1.2.7, in the engines.
Prompt caching in the engines
agent-langgraph-ts,agent-langgraph-aguiandagent-mastrasent Claude nocache_control, and the model gateway passes a request body through unchanged, so nothing cached. Measured on Anthropic's API through a gateway-style passthrough, a two-step tool turn onclaude-sonnet-5-5:Each engine now marks the last tool, the system prompt (the server's instructions) and the conversation, by the request's top-level
cache_control, which Anthropic moves forward as the conversation grows (three of the four marks a request may carry). Volatile parts stay after the marks: the progress reminder lands in the same place on every call. A proxy in front of Claude that refuses the field is remembered for the life of the process and asked again unmarked, as the engines already do for context editing. OpenAI and Gemini are sent nothing new.Clearing hysteresis (
agent-langgraph-ts/src/context-room.ts)Past 100,000 tokens the local rule cleared all but the last ten tool results on every step, so each new result pushed one more out and the prefix a provider caches changed on every step of a long task. It now clears the oldest results down to 60,000 and then holds the boundary until the sent context passes 100,000 again. Each step is a run of its own and holds nothing, so the boundary is worked out by replaying the thread as it grew: every run of a turn lands on the same boundary.
Claude keeps Anthropic's own clearing (
clear_tool_uses). Measured live on a 75-result task past the trigger, each step read everything the step before sent from the cache, so it needed no change.No per-step MESSAGES_SNAPSHOT (
agent-langgraph-ts/src/run.ts)Every run ended with a snapshot of the whole thread, and every step of a long task is a run, so a turn streamed about N²/2 messages back. The step's events already build every message it adds in the caller's thread: its words, its calls, recall results, and Gemini's thought signature (
REASONING_ENCRYPTED_VALUE, which@ag-ui/clientapplies to the call). The server and the browser apply events with@ag-ui/client, as they do for any AG-UI agent that sends no snapshot, and the server's snapshot handlers all treat it as optional. A late step of a 150-step task now streams under 5,000 characters instead of the whole 300,000+ character thread.Tests
agent-langgraph-ts/test/prompt-cache.test.ts: the marks on every Claude request, none on OpenAI, and a refusing proxy asked again unmarked. Red before, green after.agent-langgraph-ts/test/prompt-cache.live.test.ts,agent-langgraph-agui/tests/test_prompt_cache.py, the live test inagent-mastra/src/mastra/index.test.ts: the usage numbers above, on Anthropic's own API. They run only withOPENBOT_LIVE_ANTHROPIC_KEYset. Each was run red on the old code and green on the new.agent-langgraph-ts/test/context-room.test.ts, "A 200-step task": 200 runs with results of 1,000 to 12,000 characters; the prefix changes only on steps that clear a stretch. Red on the old rule (step 41 cleared one result), green now.agent-langgraph-ts/test/no-snapshot.test.ts: no snapshot, a small stream, and the caller's thread (applied by@ag-ui/client) is the thread sent plus the step. The tests that read the snapshot now read the thread the caller holds, including the Gemini thought-signature round trip.Changelog
Long tasks.
LangGraph (Python) engine and the Mastra Bot now mark the tools, the instructions and the
conversation for Anthropic's prompt cache, so each step of a long task reads what the step before
it sent at a tenth of the price instead of paying for all of it again. A proxy in front of Claude
that refuses the field is asked again without it. Nothing changes for OpenAI or Gemini.
results are now cleared down to 60,000 at once and then left alone until the conversation grows
past 100,000 again, instead of one more result on every step, which changed the cached prefix on
every step.
(TypeScript) engine ended every step with a copy of the whole thread, so a task of many steps
sent the server the conversation over and over. It now sends only what the step added.