Repository navigation
Description
Problem Description
A chat call of LLMClient (request types chat_dispatch and stream) records all its prompt tokens at the full prompt price. It does not read the cached prompt tokens of the reply (usage.prompt_tokens_details.cached_tokens in the OpenAI form). The pricing registry prices cached prompt tokens at the cached input rate since #4403, and the agent runs of the roadmap jobs give it cache_read_tokens from pydantic-ai. The chat path gives it 0.
StreamUsage in src/devops_cli/ai/client/network.py has no field for cached tokens, _record_stream_chunk_usage in src/devops_cli/ai/client/openai.py reads only prompt_tokens, completion_tokens, total_tokens and reasoning_tokens, and the track_request_spend calls in src/devops_cli/ai/client/unified.py pass no cache_read_tokens.
Reproduction Steps
- Send two chat calls through the gateway with the same prompt of more than 4,000 tokens to
deepseek-v4.1-flash-1017orclaude-sonnet-5-5, withLLMClient. - Read the ledger rows of the two calls and compare
cost_usdwith the cost that the gateway reports (litellm_spend_metric_total).
Expected vs Actual Behavior
Expected: the second call costs the cached input rate for its cached prompt tokens, as an agent run does.
Actual: both calls cost the full prompt price. For the DeepSeek group, 94% of the prompt tokens of the gateway came from the cache from 2026-11-08 to 2026-11-11, so a chat call can read up to 30 times high on its prompt part. A probe of three agent runs with a prompt of 4,437 tokens read 4,352 cached tokens on the second and third run.
Environment Diagnostics
- DevOps CLI Version: main after fix(ai): price the DeepSeek models in the price table #4403
- Execution Environment: DevContainer and the roadmap Service
Logs, Traces, or Terminal Output
None. The evidence is the code paths above and the gateway counters litellm_input_cached_tokens_metric_total and litellm_input_tokens_metric_total.
Proposed design
Problem statement
Chat calls through LLMClient record every prompt token at the full prompt rate. They ignore the cached prompt tokens that the reply reports. Agent runs already pass cached tokens to the pricing registry. This change carries the cached token count from the provider reply to the spend ledger for both chat_dispatch and stream calls.
Acceptance criteria
Requirement: StreamUsage cached token field
The StreamUsage type SHALL carry the cached prompt tokens of a streamed reply.
Scenario: Streamed usage with cached tokens
- WHEN A streamed OpenAI-compatible reply reports
usage.prompt_tokens_details.cached_tokensof 4352. - THEN The recorded
StreamUsagehascache_read_tokensequal to 4352.
Requirement: Streamed chunk reader
_record_stream_chunk_usage SHALL read prompt_tokens_details.cached_tokens from the usage chunk.
Scenario: Cached tokens in a streamed usage chunk
- WHEN A usage chunk has
prompt_tokens_details.cached_tokensof 4352. - THEN
stream_usageholds aStreamUsagewithcache_read_tokensequal to 4352.
Requirement: Non-streamed reply field
A non-streamed OpenAI-compatible reply SHALL carry its cached prompt tokens on LLMResponse.
Scenario: Cached tokens in a non-streamed reply
- WHEN A non-streamed chat reply reports
usage.prompt_tokens_details.cached_tokensof 4352. - THEN The
LLMResponsehascache_read_tokensequal to 4352.
Requirement: Chat dispatch spend
The chat_dispatch spend recorder SHALL pass the reply's cached prompt tokens to track_request_spend.
Scenario: Dispatch spend with cached tokens
- WHEN A non-streamed chat call records spend for an
LLMResponsewithcache_read_tokensof 4352. - THEN
track_request_spendreceivescache_read_tokensequal to 4352 for request typechat_dispatch.
Requirement: Stream spend
The stream spend recorder SHALL pass the stream's cached prompt tokens to track_request_spend.
Scenario: Stream spend with cached tokens
- WHEN A streamed chat call records spend with
stream_usage.cache_read_tokensof 4352. - THEN
track_request_spendreceivescache_read_tokensequal to 4352 for request typestream.
Requirement: Cached rate in the ledger
A chat call with cached prompt tokens SHALL record a cost_usd that prices the cached subset at the cached input rate.
Scenario: Ledger cost with cached prompt tokens
- WHEN A chat call records 4437 prompt tokens, of which 4352 are cached, at a known model price.
- THEN The ledger row's
cost_usdequals the full rate for 85 tokens plus the cached rate for 4352 tokens.
Existing Tool Considered
No alternatives check has run. The OpenAI Python SDK covers the need: CompletionUsage.prompt_tokens_details.cached_tokens reports the cached prompt tokens of a reply. The SDK lacks pricing, which the project's existing pricing registry already does. pydantic-ai also reports cache_read_tokens for agent runs. No new library or parser is needed.
Key questions
- Which chat code paths drop the cached prompt tokens?:
StreamUsagehas no cached token field._record_stream_chunk_usagereads only prompt, completion, total and reasoning tokens. Thetrack_request_spendcalls inunified.pypass nocache_read_tokens. [sources: src/devops_cli/ai/client/network.py:184, src/devops_cli/ai/client/openai.py:410, src/devops_cli/ai/client/unified.py] - How should cached prompt tokens travel from the provider reply to
track_request_spend?: Addcache_read_tokenstoStreamUsageandLLMResponse. Read it fromprompt_tokens_details.cached_tokens. Pass it totrack_request_spendin both spend recorders. (best practice) [sources: src/devops_cli/ai/client/network.py:184, src/devops_cli/ai/client/openai.py:410, src/devops_cli/ai/client/unified.py]- Chosen: Add fields to StreamUsage and LLMResponse. Both reply paths already carry token counts on these objects, so the cached count travels the same way.
- Rejected: Read cached tokens in unified.py from provider-specific reply objects. The provider mixins build
LLMResponseandStreamUsage;unified.pynever sees the provider reply object. - Rejected: Pass cached tokens through a ContextVar. A ContextVar adds hidden state; the reply objects already carry the usage.
- Should this fix include the direct Claude provider?: No. This fix covers the OpenAI-compatible form that the gateway and the reproduction use. The issue cites no Claude cache field, and the Claude path reads only
usage.input_tokens. (best practice) [sources: src/devops_cli/ai/client/claude.py:201, src/devops_cli/ai/client/openai.py:410, src/devops_cli/ai/client/network.py:184]- Chosen: OpenAI-compatible path only. The issue names
prompt_tokens_details.cached_tokens, and the reproduction uses the gateway. - Rejected: Include the direct Claude provider. The issue cites no Claude cache field, and the Claude path reads only
usage.input_tokens.
- Chosen: OpenAI-compatible path only. The issue names
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 11, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(ai)scope of the title gives the scope.Chat calls over-report cost by up to 30x on the prompt part, and 94% of gateway prompt tokens came from cache.Cost reporting for the main service is materially wrong for every chat call with a cached prefix.Three files need a new field and a pass-through: StreamUsage, _record_stream_chunk_usage, and the track_request_spend calls in unified.py.Both add a token field to StreamUsage and the spend path, but #591 adds reasoning tokens while the candidate adds cached prompt tokens.devops-cli-service commentedon Nov 11, 2026
Status set to Ready at 3180bf1 (see proposed design in issue body).
dan-petty commentedon Nov 11, 2026
Correction to the problem description: "the agent runs of the roadmap jobs give it
cache_read_tokensfrom pydantic-ai" is true only for the implement step. That step records its spend withrecord_run_spend(src/devops_cli/roadmap/agent_run.py). The agents thatbuild_agentmakes (src/devops_cli/ai/client/factory.py), for example the analysis step of refine, record their spend through_spend_hook, and that hook gives nocache_read_tokens. Thus most agent runs also cost the full prompt rate for their cached tokens.#4483 covers the
build_agenthook. This issue keeps its scope: theLLMClientchat path only. I did not edit the body, so that the refine record of this issue stays valid.devops-cli-service commentedon Nov 11, 2026
Released in v0.112.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.112.0
devops-cli-service commentedon Nov 11, 2026
Live since 2026-11-11 14:13 UTC