Skip to content

feat(ai): cache Claude prompts and price the cache tokens - #5577

Merged
dan-petty merged 6 commits into
mainfrom
feat/3658-claude-prompt-cache
Nov 14, 2026
Merged

dan-petty merged 6 commits into
mainfrom
feat/3658-claude-prompt-cache

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Each Claude group of the gateway now asks Anthropic to cache its prompt prefix. claude-sonnet-5-5 and claude-haiku-5-5 set two LiteLLM cache injection points: one on the system messages, which caches the tools and the system prompt, and one on the last message, which caches the history of an agent run. Before, each Claude request paid the full input price for each prompt token, and an agent run sends its full history again on each request.

The spend ledger now keeps the cache read and cache write tokens of each call in two new columns, and genai-prices prices both at the cache rates of the model. Before, a cache write cost the input price, and no row showed the counts. The spend hook of build_agent, record_run_spend and the chat calls of LLMClient give the cache write count. A gateway test fails when a Claude group loses a point or another group sets one.

Closes #3658
Change: openspec/changes/archive/2026-11-13-3658-cache-claude-prompts-and-price-the/

@dan-petty dan-petty added type/feature Type feature: a new function scope/ai AI review pipeline, personas, prompts, LLM client, or RAG labels Nov 14, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Checks run before the push (head a8877fc26, base ffce5d999)

  • uv run devops ci fail-to-pass --base origin/main: 13 new or changed tests fail on the base and pass on the head (171 others pass on both).
  • uv run devops ci test tests/ai/benchmark/test_k8s_llm_gateway.py tests/ai/spend tests/ai/client/test_agent_factory.py tests/ai/client/test_roadmap_agent_run.py: 314 passed.
  • uv run devops ci test tests/ai/client: 494 passed.
  • uv run devops ci --check --only lint,format,domain: pass. --only typecheck (mypy strict): pass.
  • uv run devops ci docs: pass. uv run devops openspec check: pass. openspec validate --all --strict: 354 passed.

Premise evidence (from the build)

  • LiteLLM 1.103.11 (the pinned image ghcr.io/berriai/litellm:v1.103.11), local proxy with a loopback stub: an agent-loop request got cache_control: {type: ephemeral} on the system block and on the tool_result block of the last message only. The blocking reply and the last stream frame gave prompt_tokens 1,550, cached_tokens 300, cache_write_tokens 1,200.
  • Live gateway, 2 small claude-haiku-5-5 calls with the same points in the request (cost under 0.001 USD): call 1 cache_write_tokens 3,310, call 2 cached_tokens 3,310.
  • pydantic-ai _map_usage maps prompt_tokens_details.cache_write_tokens to RequestUsage.cache_write_tokens (checked again in review).
  • Prometheus has litellm_input_cached_tokens_metric_total and litellm_input_cache_creation_tokens_metric_total. In the last 24 h only the DeepSeek group has cached tokens; the Claude groups have none.

Render diff (kubectl kustomize, origin/main against the branch)

k8s/llm and k8s/llm/gateway give the same differences:

  1. ConfigMap llm-gateway-config: config.yaml gets 7 comment lines and, on claude-sonnet-5-5 and claude-haiku-5-5 only, litellm_params.cache_control_injection_points: [{location: message, role: system}, {location: message, index: -1}].
  2. The generated name changes from llm-gateway-config-fd69km6265 to llm-gateway-config-kt9mh48289.
  3. Deployment llm-gateway: its config volume names the new ConfigMap. The pod template changes, so Argo CD rolls the gateway once after the merge.

No other object changes. The DeepSeek, Gemini, OpenRouter and Ollama groups and the router settings do not change. The ledger and pricing change takes effect with the next release.

Review fixes

After the merge and the release

Measure the cache hit share of the live Sonnet calls and post it on #3658: litellm_input_cached_tokens_metric_total over litellm_input_tokens_metric_total for claude-sonnet-5-5, and litellm_input_cache_creation_tokens_metric_total for the writes. Baseline for the 24 h to 2026-11-13 23:30 UTC: about 0.82M Sonnet input tokens at the gateway and 0 cached.

@dan-petty

Copy link
Copy Markdown
Owner Author

Push 25977a4aa:

…okens

The plan of the gateway cache points of the two Claude groups and of the cache read and cache write tokens of the spend ledger.
Each Claude group of the gateway sets two LiteLLM cache injection points: the system messages and the last message. Anthropic then caches the tools, the system prompt and the history of an agent run. The spend ledger keeps the cache read and cache write tokens of each call in two new columns, and genai-prices prices both at the cache rates of the model. The spend hook of build_agent, record_run_spend and the chat calls of LLMClient give the cache write tokens. The gateway test fails when a Claude group loses a point or another group gets one.

Closes #3658
…plan

A request that no request follows within 5 minutes, such as a refine_judge review, writes the part after the system point at 1.25 times the input price and reads none of it. The design now names that cost and the metric that shows it.
record_run_spend takes its model as Any, so the ignore suppressed nothing. It put the tests one over the type-ignore ceiling of test_suppressions_stay_under_their_ceilings.
…e tokens

The gateway spec gets the requirement that a Claude group caches its prompt prefix. The spend pricing spec gets the cache token columns, the cache write rate and the callers that give the cache write tokens.
@dan-petty
dan-petty force-pushed the feat/3658-claude-prompt-cache branch from 25977a4 to de1dfb1 Compare November 14, 2026 01:19
@dan-petty
dan-petty marked this pull request as ready for review November 14, 2026 01:35
@dan-petty
dan-petty merged commit a42931c into main Nov 14, 2026
24 checks passed
@dan-petty
dan-petty deleted the feat/3658-claude-prompt-cache branch November 14, 2026 01:37
dan-petty-release Bot added a commit that referenced this pull request Nov 14, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.135.0](v0.134.1...v0.135.0)
(2026-11-14)


### Features

* **ai:** cache Claude prompts and price the cache tokens
([#5577](#5577))
([a42931c](a42931c))


### Bug Fixes

* **k8s:** the DeepSeek group keeps its structured reply flag
([#5572](#5572))
([ffce5d9](ffce5d9))
* **security:** bump helm to 4.3.2 and renew the kubectl Trivy ignores
([#5584](#5584))
([7b27444](7b27444))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/ai AI review pipeline, personas, prompts, LLM client, or RAG type/feature Type feature: a new function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ai): cache Claude prompts and price the cache tokens

1 participant