Repository navigation
Description
Problem or Motivation
Anthropic reports a cache hit rate of 37% over seven days for the API key of the owner and says that prompt caching could save up to 23 USD a month. The gateway groups claude-sonnet-5-5 and claude-haiku-5-5 send repeated prefixes: the system prompt and the tool definitions of each stage, review and eval call. Nothing in the repository asks Anthropic to cache them:
git grep -n cache_control -- src k8s/llmfinds no use ofcache_control, andk8s/llm/gateway/config.yamlsets nocache_control_injection_pointsfor the two groups. It sets only the cache prices (cache_creation_input_token_cost,cache_read_input_token_cost).- The local spend ledger holds about 13.4 million Claude prompt tokens for the seven days to 2026-11-05, almost all from the evals of 2026-11-02, with the same prefix in each call of a case.
- The spend ledger stores
prompt_tokensandcompletion_tokensonly. It stores no cache read or cache write tokens, so a cached call is priced at the full input price. Cache reads cost 0.05 times the input price on Sonnet 5.5, and 0.1 times on Haiku 5.5.
Proposed Solution & Architectural Design
- Check the premise on the real system first (feasibility): (a) the pinned LiteLLM image accepts
cache_control_injection_points, or passes a top-levelcache_control, for the two Anthropic groups and reaches Anthropic with it; (b) the gateway reply carries the cache token counts (usage.prompt_tokens_details.cached_tokensandcache_creation_input_tokensor their LiteLLM names). Record the evidence in the proposal. If (a) fails, return the item with the evidence and write no compensating code. - If it holds: set the injection point on the system message of the two Claude groups in
k8s/llm/gateway/config.yaml, and extend the gateway test that holds the hosted-group rules. - Record the cache read and cache write tokens of a reply in the spend ledger, and price them at the cache prices that genai-prices holds for the model, so the cost of a cached call is not overstated.
- A test fails if a Claude group loses its injection point.
Not in scope: the DeepSeek group and the Gemini group.
Target Subsystem
k8s/llm/gateway/config.yaml, src/devops_cli/ai/spend/ (ledger and pricing), tests/ai/benchmark/test_k8s_llm_gateway.py.
Existing Tool Considered
LiteLLM prompt caching (cache_control_injection_points) and the Anthropic automatic caching field (cache_control at the top level of the request, documented at https://platform.claude.com/docs/en/build-with-claude/prompt-caching). genai-prices 0.1.13 prices Claude cache tokens: for claude-sonnet-5-5 one million cache read tokens cost 0.10 USD and one million cache write tokens cost 2.50 USD. No new tool.
Standards Compliance
Related: #3656 (a hosted gateway call is recorded as local). The ledger change of this item builds on it.
Alternatives check of 2026-11-07: covers.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-07, maintained.
Proposed design
Problem statement
The gateway sends the same system prompt and tool definitions on each stage, review and eval call to the two Claude groups, and nothing asks Anthropic to cache that prefix, so Anthropic caches nothing. The spend ledger stores neither the cache read nor the cache write token count: the agent-run path passes cache reads to genai-prices, but the chat path does not, and no path prices cache writes, so a cache write is billed at the full input price and the cache read count is invisible. This item turns on prompt caching for the two Claude groups and records and prices both cache token counts at the cache rates of the model.
Acceptance criteria
Requirement: Claude cache injection point
Each Claude group SHALL set cache_control_injection_points in its litellm_params, with one point on the system message.
Scenario: System-message cache point
- WHEN The gateway test reads
k8s/llm/gateway/config.yaml. - THEN It finds a point with
location: messageandrole: systemonclaude-sonnet-5-5and onclaude-haiku-5-5.
Requirement: Last-message cache point
Each Claude group SHALL set a second injection point with index: -1 on the last message.
Scenario: Last-message cache point
- WHEN The gateway test reads the injection points of a Claude group.
- THEN It finds a message point with
index: -1beside the system point.
Requirement: Cache point guard
_hosted_violations MUST report a problem that names the group when a Claude group loses its cache injection point.
Scenario: Removed cache point
- WHEN A test edit removes the injection point from
claude-sonnet-5-5. - THEN
_hosted_violationsreturns a problem that namesclaude-sonnet-5-5.
Requirement: Cache scope
Only the two Claude groups SHALL set cache_control_injection_points.
Scenario: Other groups hold no cache point
- WHEN The gateway test reads the
litellm_paramsof every group ink8s/llm/gateway/config.yaml. - THEN No group other than
claude-sonnet-5-5andclaude-haiku-5-5holdscache_control_injection_points.
Requirement: Cache token storage
The spend ledger SHALL store the cache read tokens and the cache write tokens of each reply.
Scenario: Chat reply with cache usage
- WHEN A Claude chat reply reports 300 cache read tokens and 1,200 cache write tokens.
- THEN The ledger row stores 300 cache read tokens and 1,200 cache write tokens.
Requirement: Existing ledger migration
An existing spend ledger MUST gain the two cache token columns in place without losing its rows.
Scenario: Ledger created before the cache columns
- WHEN The ledger opens a database whose
ai_spend_recordstable has no cache token columns. - THEN The table has both columns and the rows it held before are still there.
Requirement: Cache token pricing
The spend ledger MUST price cache read tokens and cache write tokens at the cache rates of the model.
Scenario: Sonnet cache prices
- WHEN A
claude-sonnet-5-5reply reports 2,000,000 prompt tokens, of which 1,000,000 are cache reads and 1,000,000 are cache writes. - THEN The ledger cost is 2.60 USD, with the cache reads at 0.10 USD and the cache writes at 2.50 USD per million tokens, not 4.00 USD at the full input price.
Requirement: Agent-run cache tokens
The spend ledger SHALL record the cache read tokens and the cache write tokens of a pydantic-ai agent run.
Scenario: Agent usage with cache tokens
- WHEN An agent run reports 400 cache read tokens and 900 cache write tokens.
- THEN The ledger row stores both counts and prices them at the cache rates of the model.
Requirement: Streamed cache usage
A streamed Claude reply MUST record the cache token counts of its final usage frame.
Scenario: Streamed reply final usage
- WHEN A streamed Claude reply ends with a usage frame that holds cache read and cache write tokens.
- THEN The ledger row stores both counts.
Existing Tool Considered
LiteLLM v1.103.10 (https://github.com/BerriAI/litellm) is the gateway image pinned at k8s/llm/gateway/deployment.yaml:33. It writes the Anthropic cache_control marker from cache_control_injection_points in a group's litellm_params, and its reply usage carries the cache token counts. genai-prices 0.1.13 (https://pypi.org/project/genai-prices), pinned at uv.lock:913, prices cache read and cache write tokens at the cache rates of the model. What they lack is the project's own spend ledger: no tool writes cache read and cache write tokens into a ledger row, and LiteLLM response_cost covers a gateway call only, not the agent runs that the ledger also prices. LiteLLM's injection points name message locations only; a cache point on the tool block needs a separate setting. The cache fields of the final usage frame of a streamed reply and the live gateway stay unverified. That ledger code is about 120 lines across src/devops_cli/ai/spend/, src/devops_cli/ai/client/ and src/devops_cli/ai/agents/: about 15,000 tokens to build, about 3,000 tokens a year to keep and fix, and about one day of elapsed time. The alternatives check of 2026-11-07 recorded the verdict covers for BerriAI/litellm.
Key questions
- The alternatives check found that LiteLLM covers this item. Adopt the tool, or keep the item and give the reason?: Keep the item. LiteLLM v1.103.10 is already the pinned gateway image and genai-prices 0.1.13 is already the pinned price source, so there is nothing to adopt: the alternatives check of 2026-11-07 recorded
coversfor BerriAI/litellm, which means the item uses the tool. Neither tool writes the project's ledger rows, so the item stays open with the existing tooling named. It waits on the Sonnet 5.5 hold. (best practice) [sources: k8s/llm/gateway/deployment.yaml:33, uv.lock:913, https://github.com/BerriAI/litellm]- Chosen: Keep the item with the pinned LiteLLM and genai-prices. LiteLLM supplies the cache marker and genai-prices supplies the cache rates; neither writes the ledger rows.
- Rejected: Adopt LiteLLM as the replacement and close the item. LiteLLM cannot write cache tokens into the project's spend ledger, so the ledger work would go undone.
- Rejected: Price the ledger with LiteLLM
response_cost. It covers a gateway call only; the ledger also prices pydantic-ai agent runs and local tokens.
- Does the item start now, or wait for the owner to lift the Sonnet hold?: It waits. The owner's hold of 2026-11-11 stops Sonnet 5.5, and the repository records that the analysis and stage slots name
deepseek-v4.1-flash-1017again after the Sonnet trial (fix(roadmap): the stage job stages no item (placeholder replies) #4204, owner decision fix(k8s): the ConfigMap template names DeepSeek for analysis #4369). The prompt cache saves nothing while no Claude call runs, so the item starts when the owner lifts the hold. (a person decides) [sources: k8s/devops/configmap.example.yaml:67, k8s/devops/configmap.example.yaml:73, docs/roadmap/stage.md:59, SECURITY.md:40] - Which setting turns on Anthropic prompt caching for the two Claude groups?: Set
cache_control_injection_pointsin thelitellm_paramsof each Claude group. The pinned LiteLLM writes the Anthropiccache_controlmarker from it, and the gateway config holds every group setting. Anthropic documents the marker at the top level or on blocks; the gateway's single control point is the group config. (best practice) [sources: k8s/llm/gateway/config.yaml:149, k8s/llm/gateway/config.yaml:165, https://github.com/BerriAI/litellm, https://platform.claude.com/docs/en/build-with-claude/prompt-caching]- Chosen:
cache_control_injection_pointsin each Claude group'slitellm_params. The pinned LiteLLM writes the marker from it, and the gateway config is the single control point for every caller. - Rejected: A top-level
cache_controlin each group'sextra_body. Anthropic's automatic caching is the simplest provider path, but LiteLLM's pass-through of that field is unverified, while the alternatives check exercisedcache_control_injection_pointsoffline. - Rejected: A
cache_controlmarker in each stage, review and eval call. It repeats the same logic in each caller and misses a call that a new caller adds. - Rejected: A top-level
cache_controlon each request from the client. Every caller must send it, and a caller that forgets it loses the cache with no gateway check.
- Chosen:
- Does the pinned LiteLLM write the marker and return the cache token counts?: The alternatives check of 2026-11-07 verified offline against a stub that
ghcr.io/berriai/litellm:v1.103.10writes thecache_controlmarker fromcache_control_injection_pointsand that its reply usage carriesprompt_tokens_details.cached_tokensand the cache creation count. The live gateway is unverified, so the first real call records the evidence; a missing marker returns the item with that evidence and no compensating code. [sources: k8s/llm/gateway/deployment.yaml:33, k8s/llm/gateway/config.yaml:149, https://github.com/BerriAI/litellm] - Should the gateway mark the last message as a second cache breakpoint?: Yes. Add
{location: message, index: -1}beside the system point. The tool block precedes the system prompt in the cache prefix, so the system point caches the tool definitions and the system prompt, and the last-message point caches the history of a multi-turn agent run. Two points stay under the four that Anthropic allows. (best practice) [sources: k8s/llm/gateway/config.yaml:149, https://platform.claude.com/docs/en/build-with-claude/prompt-caching]- Chosen: System point and last-message point. The system point caches the tool definitions and system prompt, and the last-message point caches the growing history of a multi-turn run; two points stay under the limit.
- Rejected: System point only. It leaves the growing history of a multi-turn run uncached.
- Rejected: System, tool and last-message points. The tool block precedes the system prompt in the cache prefix, so the system point already caches it; a third point adds nothing.
- Does the spend ledger already price cache read tokens?: Yes, in part. The agent-run spend hook passes
cache_read_tokenstotrack_request_spend, which passes it tocalculate_request_cost; the genai-prices path puts it inUsage(cache_read_tokens=...), and an override price uses its cached input rate (fix(ai): price the DeepSeek models in the price table #4403). The chat dispatch path passes no cache read count, the ledger table has no cache read or cache write column, and no path prices cache writes. [sources: src/devops_cli/ai/client/factory.py:244, src/devops_cli/ai/client/unified.py:500, src/devops_cli/ai/spend/pricing.py:616, src/devops_cli/ai/spend/models.py:37, src/devops_cli/ai/spend/ledger.py:958] - How should the spend ledger price a cached Claude call?: Pass the full prompt count as
input_tokens, withcache_read_tokensandcache_write_tokensas its parts, to the genai-pricesUsagein_genai_prices_cost. The library applies the cache rates of the model: Sonnet 5.5 reads at 0.10 USD and 5-minute writes at 2.50 USD per million tokens, Haiku 5.5 at 0.01 and 0.125. The two Claude groups are priced by genai-prices (_HOSTED_GROUP_PRICESholds only DeepSeek), so the override path's lack of a cache-write rate is out of scope. (best practice) [sources: src/devops_cli/ai/spend/pricing.py:616, src/devops_cli/ai/spend/pricing.py:62, uv.lock:913, k8s/llm/gateway/config.yaml:158, k8s/llm/gateway/config.yaml:160, k8s/llm/gateway/config.yaml:174, k8s/llm/gateway/config.yaml:176, https://platform.claude.com/docs/en/build-with-claude/prompt-caching]- Chosen: genai-prices
Usagewith both cache fields. The two Claude groups are priced by genai-prices, and the library holds the cache read and cache write rates of the model. - Rejected: LiteLLM
response_cost. It covers a gateway call only; the ledger also prices pydantic-ai agent runs. - Rejected: The full input price for every prompt token. It overstates a Sonnet 5.5 cache read by 20 times and understates a 5-minute cache write by 20 percent, so a cached call is wrong in both directions.
- Chosen: genai-prices
- Which source gives the ledger the cache token counts?: The Anthropic SDK usage of a blocking reply (
_extract_claude_usage) and of the final message of a streamed reply (_finalize_stream_telemetry) exposecache_read_input_tokensandcache_creation_input_tokens; the pydantic-ai spend hook readsresponse.usage.cache_read_tokensandcache_write_tokens, andrecord_run_spendsums them.track_request_spendprices the counts and writes the row. The reads of the cache fields are the work this item adds. [sources: src/devops_cli/ai/client/claude.py:184, src/devops_cli/ai/client/claude.py:231, src/devops_cli/ai/client/factory.py:241, src/devops_cli/roadmap/agent_run.py:419, src/devops_cli/ai/spend/ledger.py:927] - Do the cache prices of the gateway config match the provider's prices?: Yes. The Anthropic pricing page fetched 2026-11-11 lists Sonnet 5.5 at 2.00 USD input, 2.50 USD for 5-minute cache writes and 0.10 USD for cache hits per million tokens, and Haiku 5.5 at 0.10, 0.125 and 0.01. The gateway config sets exactly those rates. The genai-prices snapshot was not read;
tests/ai/spend/test_hosted_group_prices.pycompares the client's cache read price with the gateway price for each hosted group, and the alternatives check of 2026-11-07 states the same Sonnet cache rates for genai-prices 0.1.13. [sources: k8s/llm/gateway/config.yaml:158, k8s/llm/gateway/config.yaml:160, k8s/llm/gateway/config.yaml:174, k8s/llm/gateway/config.yaml:176, tests/ai/spend/test_hosted_group_prices.py:137, uv.lock:913, https://platform.claude.com/docs/en/build-with-claude/prompt-caching] - How should an existing spend ledger gain the two cache token columns?: Add both columns in place through
_ADDED_COLUMNSwithINTEGER NOT NULL DEFAULT 0, as every earlier column did, and extend_QUERY_INSERT_RECORD,record_requestandSpendRecordwith the two counts. A database created before this change then keeps its rows. (best practice) [sources: src/devops_cli/ai/spend/ledger.py:76, src/devops_cli/ai/spend/ledger.py:88, src/devops_cli/ai/spend/ledger.py:469, src/devops_cli/ai/spend/models.py:50]- Chosen: Add the columns in place in
_ADDED_COLUMNS. The ledger gains every added column this way and keeps its rows. - Rejected: Recreate the table on open. It would drop the spend history that the ledger exists to keep.
- Rejected: Require a fresh ledger. An existing ledger would fail on the new insert.
- Chosen: Add the columns in place in
- Are fix(ai): a hosted gateway call is recorded as a local call at $0 #3656 and fix(ai): an agent run on a hosted group is not local spend #3665 still open dependencies of this item?: No. Both changes are in the tree at this commit: the hosted-versus-local rule of fix(ai): a hosted gateway call is recorded as a local call at $0 #3656 is in
is_localand the gateway test, and the asked-model pricing of fix(ai): an agent run on a hosted group is not local spend #3665 is intrack_request_spend. No dependency remains. [sources: src/devops_cli/ai/spend/pricing.py:324, src/devops_cli/ai/spend/ledger.py:979, tests/ai/benchmark/test_k8s_llm_gateway.py:2097, openspec/changes/archive/2026-11-09-3665-an-agent-run-on-a-hosted/proposal.md]
Open questions
- Does the item start now, or wait for the owner to lift the Sonnet hold? (a person decides)
Fit
Fits in one pull request.
Suspected block
The owner's hold on Sonnet 5.5, set 2026-11-11; no Sonnet traffic has run since 2026-11-10 18:00Z, and the item starts when the owner lifts the hold.
Dependencies
None.
Activity
dan-petty commentedon Nov 5, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
featprefix of the title gives the type.(ai)scope of the title gives the scope.It saves money and fixes overstated cost, but it does not block a release or fix a critical defect.It cuts Claude spend by up to 23 USD per month and stops overstating the cost of cached calls.It touches gateway config, the spend ledger schema, pricing logic, and adds a regression test across three files.Both extend the spend ledger in the ai module, but #474 records the serving backend while this candidate records cache token counts and prices them at cache rates.dan-petty commentedon Nov 5, 2026
Feasibility of the premise, checked on 2026-11-05 with LiteLLM 1.103.0 (the version of the gateway image), offline: a
Routerbuilt from the groupclaude-sonnet-5-5ofk8s/llm/gateway/config.yaml, with a loopback stub in place of Anthropic that records each request body./v1/messagesholds 0cache_controlmarkers. Nothing caches today.cache_control_injection_points: [{location: message, role: system}]inlitellm_params: the system block carriescache_control: {type: ephemeral}. The group needs nosupports_prompt_cachingflag inmodel_infofor this.{location: message, index: -1}as a second point: the last message also carries the marker (2 of the 4 allowed). A multi-turn agent run then reads the history of its last request from the cache.prompt_tokens1,550. Soprompt_tokensis the sum of the three, and the spend ledger prices cached tokens at the full input price.prompt_tokens_detailsholdscached_tokens300 andcache_creation_tokens1,200, and the usage also holdscache_read_input_tokensandcache_creation_input_tokens. Theresponse_costof LiteLLM, 0.00318 USD, is the correct cache-aware cost.Usage(input_tokens=<sum>, cache_read_tokens=..., cache_write_tokens=...)prices a Claude call at the cache prices.input_tokensincludes the cache tokens.Not verified yet: the final usage frame of a streamed reply holds the same cache fields, and the live gateway.
Dependency: two paths record Claude spend. A chat call records
prompt_tokensfrom the reply (#3656 makes it hosted). An agent run records a sum of the pydantic-ai usage, which hascache_read_tokensandcache_write_tokens, and it is local $0 today (#3665). The ledger part of this item records cache tokens on both paths, so #3665 lands first.devops-cli-service commentedon Nov 6, 2026
Refine cannot set this item to Ready at 662828d while the items below stay open. The next refine round looks at the item again when they close, or when a person replies in a comment or edits the body.
devops-cli-service commentedon Nov 7, 2026
Alternatives check: covers
Reason: The web search provider returned no answer for every query, so I checked the Anthropic prompt-caching page the issue names plus the facts of the two tools it names: LiteLLM already writes the
cache_controlmarker fromcache_control_injection_pointsand returns the cache token counts (the issue's own offline stub check shows this for LiteLLM 1.103.0), and genai-prices prices cache read and write tokens at the model's cache rates. Only the project's own spend-ledger storage and pricing code is left, which no external tool can supply, so this is existing in-use tooling rather than a new tool.Tool: BerriAI/litellm (https://github.com/BerriAI/litellm)
Covers:
cache_control_injection_pointsin a group'slitellm_paramsadds the Anthropiccache_control: {type: ephemeral}marker to the system message, and a second point{location: message, index: -1}marks the last message (verified offline against a stub in the issue reply)cache_controlor explicit block breakpoints, with up to four breakpoints, and prices Sonnet 5.5 cache reads at 0.05x the input priceprompt_tokens_details.cached_tokens/cache_creation_tokensandcache_read_input_tokens/cache_creation_input_tokens)Usage(input_tokens=..., cache_read_tokens=..., cache_write_tokens=...)at the model's cache read and write ratesLacks:
src/devops_cli/ai/spend/response_costcovers only a gateway call and not the locally priced agent runsTools considered:
Searches run:
Sources:
Checked on 2026-11-07. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 8, 2026
Refine cannot set this item to Ready at ab74843 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.
https://github.com/BerriAI/litellmcovers this item. Adopt the tool, or keep the item and give the reason. (a person decides)dan-petty commentedon Nov 11, 2026
The owner put Sonnet on hold on 2026-11-11, and no Sonnet traffic has run since 2026-11-10 18:00Z. The Claude prompt cache saves nothing while Sonnet is off, so this item waits until the owner lifts the hold. Evidence: the observability and caching study of 2026-11-11 (the skeptic refuted the estimate of about 75 USD a week, because the Sonnet input came from a one-day trial).
devops-cli-service commentedon Nov 11, 2026
Refine cannot set this item to Ready at 453aed0 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.
dan-petty commentedon Nov 13, 2026
Input for this item from the start-context review of the roadmap jobs (2026-11-13):
Data gap: for 2026-11-09 and 2026-11-10, devops-cli counted 102M Sonnet prompt tokens and LiteLLM counted 42.6M. Nobody has explained the 2.4 times difference. Find its cause before anyone measures this item on Claude.
Until this item is done: no larger start context on a Claude route.
1 remaining item
docs(openspec): archive #3658 cache Claude prompts and price the cach…
docs(openspec): plan #3658 cache Claude prompts and price the cache t…
feat(ai): cache Claude prompts and price the cache tokens
docs(openspec): name the DeepSeek fallback overlap in the #3658 plan
docs(openspec): name the write cost of a single request in the #3658 …
docs(openspec): archive #3658 cache Claude prompts and price the cach…
devops-cli-service commentedon Nov 14, 2026
Released in v0.135.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.135.0
devops-cli-service commentedon Nov 14, 2026
Live since 2026-11-14 02:01 UTC