Skip to content

Engines: cache Claude's prompt prefix, hold the clearing boundary, stop the per-step snapshot - #2661

Merged
davidmckayv merged 1 commit into
mainfrom
waste/agents
Dec 2, 2026
Merged

davidmckayv merged 1 commit into
mainfrom
waste/agents

Conversation

@davidmckayv

@davidmckayv davidmckayv commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Three findings from the waste audit after 1.2.7, in the engines.

Prompt caching in the engines

agent-langgraph-ts, agent-langgraph-agui and agent-mastra sent Claude no cache_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 on claude-sonnet-5-5:

Engine Before (step 2) After (step 2)
agent-langgraph-ts 18,864 input, 0 read from cache 18,792 read from cache, 2 input
agent-langgraph-agui 14,192 input, 0 read 13,920 read, 2 input
agent-mastra 11,940 input, 0 read 11,879 read, 2 input

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/client applies 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 in agent-mastra/src/mastra/index.test.ts: the usage numbers above, on Anthropic's own API. They run only with OPENBOT_LIVE_ANTHROPIC_KEY set. 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.
  • Mastra and langgraph-agui: request-shape and refusing-proxy tests through each engine's real endpoint.

Changelog

Long tasks.

  • Claude costs far less on the built-in engines. The LangGraph (TypeScript) engine, the
    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.
  • A long task on OpenAI or Gemini keeps its cached prefix. Past 100,000 tokens, old tool
    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.
  • A step of a long task no longer sends the whole conversation back. The LangGraph
    (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.

…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
davidmckayv added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 2f2a028 Dec 2, 2026
73 of 75 checks passed
@davidmckayv
davidmckayv deleted the waste/agents branch December 2, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant