Skip to content

fix(ai): price cached prompt tokens of chat calls - #4584

Merged
dan-petty merged 2 commits into
mainfrom
fix/4442-price-cached-prompt-tokens-of-chat
Nov 11, 2026
Merged

dan-petty merged 2 commits into
mainfrom
fix/4442-price-cached-prompt-tokens-of-chat

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

A chat call of LLMClient priced all its prompt tokens at the full prompt rate. The reply names the prompt tokens that the provider read from its cache (usage.prompt_tokens_details.cached_tokens), but the client did not read them. The agent paths give them since #4403 and #4475. Thus a chat call on the DeepSeek group with 900,000 of 1,000,000 prompt tokens from the cache recorded 0.10192 USD, not 0.033477 USD.

LLMResponse and StreamUsage now carry cache_read_tokens. The OpenAI-compatible provider reads it from a reply and from the usage chunk of a stream. The chat_dispatch and stream spend recorders give it to track_request_spend. The failed-attempt recorder reads it from the usage of an error body.

Closes #4442

Change: openspec/changes/archive/2026-11-11-4442-price-cached-prompt-tokens-of-chat/

A chat call of LLMClient, streamed or not, now gives track_request_spend
the prompt tokens that its reply names as cached
(usage.prompt_tokens_details.cached_tokens). A failed attempt whose
error body names that usage gives them too. LLMResponse and StreamUsage
carry the new field cache_read_tokens.
@dan-petty dan-petty added type/bug Type bug: the code does not do what it must scope/ai AI review pipeline, personas, prompts, LLM client, or RAG labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Fail-to-pass run: uv run devops ci fail-to-pass --base origin/main at bd6ab8d on base 5f75d25.

  failed on base: tests.ai.spend.test_ai_spend_dispatch::test_a_chat_call_on_the_deepseek_group_costs_its_cached_and_new_input_and_its_output[chat] (AssertionError: assert [('chat_dispa...op', 0.10192)] == [('chat_dispa...p', 0.033477)])
  failed on base: tests.ai.spend.test_ai_spend_dispatch::test_a_chat_call_on_the_deepseek_group_costs_its_cached_and_new_input_and_its_output[stream] (AssertionError: assert [('stream', 'stop', 0.10192)] == [('stream', 'stop', 0.033477)])
  failed on base: tests.ai.spend.test_ai_spend_dispatch::test_a_chat_call_on_the_deepseek_group_costs_its_cached_and_new_input_and_its_output[failed-attempt] (AssertionError: assert [('chat_dispa...op', 0.10192)] == [('chat_dispa...p', 0.033477)])
3 failed on base (3 failed and 16 passed in tests/ai/spend/test_ai_spend_dispatch.py; 0 errored).

Premise check: the paths that record spend for a chat call on main are _record_dispatch_spend (chat_dispatch), the stream recorder (stream), _record_cache_hit (chat_cache_hit, 0 tokens, no cost) and record_failed_attempt (ai/spend/transport.py). Before this change, none of them gave cache_read_tokens. Only build_agent and record_run_spend did.

Local checks: devops ci test tests/ai/spend tests/ai/client tests/telemetry/test_telemetry_instruments.py (682 passed), devops ci lint, devops ci format, devops ci typecheck and devops openspec check pass.

Live steps: none. The change is client code only. A person can compare the ledger with litellm_spend_metric_total for the DeepSeek group after the next release reaches the Service.

@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 10:08
@dan-petty
dan-petty merged commit f84bbe6 into main Nov 11, 2026
32 checks passed
@dan-petty
dan-petty deleted the fix/4442-price-cached-prompt-tokens-of-chat branch November 11, 2026 12:00
dan-petty pushed a commit that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.112.0](v0.111.0...v0.112.0)
(2026-11-11)


### Features

* **k8s:** export gateway spans without key metadata
([#4520](#4520))
([7234d71](7234d71))
* **k8s:** set the homelab domain in container env values
([#4608](#4608))
([a3bcf7c](a3bcf7c))
* **telemetry:** send pydantic-ai spans through the exporter seam
([#4538](#4538))
([fcf2a7a](fcf2a7a))


### Bug Fixes

* **ai:** price cached prompt tokens of chat calls
([#4584](#4584))
([f84bbe6](f84bbe6))
* **devcontainer:** stop uv noise lines in agent shells
([#4592](#4592))
([9ba375c](9ba375c))
* **k8s:** delete gateway management bodies from spans
([#4551](#4551))
([b835d3c](b835d3c))
* **k8s:** set the kube context and namespace on devops pods
([#4514](#4514))
([9087b39](9087b39))
* **roadmap:** a refused spec after the archive is a plan defect
([#4549](#4549))
([1db29f7](1db29f7))
* **roadmap:** count and log the reason of a left-out stage item
([#4541](#4541))
([5600179](5600179))
* **roadmap:** default the step ceiling to twice the budget
([#4556](#4556))
([1dfb6c8](1dfb6c8))
* **roadmap:** explain --trace shows the argument digest
([#4526](#4526))
([c92195f](c92195f))
* **roadmap:** explain totals count dropped model events
([#4543](#4543))
([fe0e979](fe0e979))
* **roadmap:** give each eval verify run its own report file
([#4534](#4534))
([ad27f2d](ad27f2d))
* **roadmap:** one event loop and a time limit for decide_all
([#4585](#4585))
([ec51e01](ec51e01))
* **roadmap:** scoped updates send requests with no row scope
([#4531](#4531))
([5f75d25](5f75d25))
* **roadmap:** the caller gate reads an added parameter span
([#4548](#4548))
([1ba07c1](1ba07c1))
* **roadmap:** the implement shell waits for its command
([#4536](#4536))
([571e044](571e044))
* **roadmap:** the module-test and caller gates read imports and calls
([#4490](#4490))
([8a22e82](8a22e82))
* **service:** put trace ids on Service JSON log lines
([#4511](#4511))
([0d7a0d2](0d7a0d2))


### Performance Improvements

* **ai:** keep intake embeddings between CLI runs
([#4546](#4546))
([812871e](812871e))
* **roadmap:** intake reads only the issues that changed
([#4552](#4552))
([7dfd155](7dfd155))

---
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/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ai): price cached prompt tokens of chat calls

1 participant