Skip to content

Cache the Claude conversation between turns in the anthropic plugin - #2701

Open
kvlshah wants to merge 1 commit into
GetStream:mainfrom
kvlshah:reduce-llm-cost
Open

kvlshah wants to merge 1 commit into
GetStream:mainfrom
kvlshah:reduce-llm-cost

Conversation

@kvlshah

@kvlshah kvlshah commented Nov 28, 2026

Copy link
Copy Markdown

Why

ClaudeLLM.simple_response resends the instructions, the tools and the whole conversation on every turn, and nothing in the request asks Anthropic to cache them, so each turn pays full input price for everything the previous turn already sent. The router's Anthropic provider already places breakpoints (acceleration/internal/llm/anthropic/messages.go); the Python plugin has none.

  • Rough estimate from reading the code: once the prefix passes the model's caching minimum (1,024 tokens on claude-sonnet-4-6, the default), each later turn reads the earlier prefix at 0.1x and writes only the new turn at 1.25x, roughly 60-80% less input-token cost on those turns (assumptions: turns arrive within the 5-minute cache lifetime, as they do in a live call; a few hundred tokens of instructions and tools; about 100-200 new tokens per exchange). Shorter conversations simply don't cache and cost the same as today.

Changes

  • simple_response sends the top-level cache_control: {"type": "ephemeral"} (automatic prompt caching), which puts the breakpoint on the last block and moves it forward as the conversation grows.
  • Only for an anthropic.AsyncAnthropic client, so a Bedrock or Vertex client passed in gets the same request as before. create_message and the tool follow-up requests are unchanged.
  • No change to prompts, messages, models or outputs.

Validation

  • ruff check and ruff format --check on plugins/anthropic: pass.
  • mypy plugins/anthropic/vision_agents: no issues.
  • pytest plugins/anthropic -m "not integration": 12 passed. The new test_each_turn_asks_to_cache_the_conversation fails without the change (no cache_control in the request body) and passes with it. Integration tests need an API key and were not run.

It may also be worth experimenting with smaller models for some of these calls, for faster and more efficient results.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant