Skip to content

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

Closed

Description

@dan-petty

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

  1. Send two chat calls through the gateway with the same prompt of more than 4,000 tokens to deepseek-v4.1-flash-1017 or claude-sonnet-5-5, with LLMClient.
  2. Read the ledger rows of the two calls and compare cost_usd with 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

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_tokens of 4352.
  • THEN The recorded StreamUsage has cache_read_tokens equal 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_tokens of 4352.
  • THEN stream_usage holds a StreamUsage with cache_read_tokens equal 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_tokens of 4352.
  • THEN The LLMResponse has cache_read_tokens equal 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 LLMResponse with cache_read_tokens of 4352.
  • THEN track_request_spend receives cache_read_tokens equal to 4352 for request type chat_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_tokens of 4352.
  • THEN track_request_spend receives cache_read_tokens equal to 4352 for request type stream.

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_usd equals 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?: StreamUsage has no cached token field. _record_stream_chunk_usage reads only prompt, completion, total and reasoning tokens. The track_request_spend calls in unified.py pass no cache_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?: Add cache_read_tokens to StreamUsage and LLMResponse. Read it from prompt_tokens_details.cached_tokens. Pass it to track_request_spend in 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 LLMResponse and StreamUsage; unified.py never 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.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/ai
    status/new
    value/high
    effort/medium
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P1-High. Chat calls over-report cost by up to 30x on the prompt part, and 94% of gateway prompt tokens came from cache.
    • Value: High. Cost reporting for the main service is materially wrong for every chat call with a cached prefix.
    • Effort: Medium. Three files need a new field and a pass-through: StreamUsage, _record_stream_chunk_usage, and the track_request_spend calls in unified.py.
    • Related: feat(cli): reasoning-token share in the spend ledger and provider-reported stream usage #591, which asks for a different change. Both add a token field to StreamUsage and the spend path, but #591 adds reasoning tokens while the candidate adds cached prompt tokens.
  3. added
    priority/p1-high
    on Nov 11, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  5. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 3180bf1 (see proposed design in issue body).

  6. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Correction to the problem description: "the agent runs of the roadmap jobs give it cache_read_tokens from pydantic-ai" is true only for the implement step. That step records its spend with record_run_spend (src/devops_cli/roadmap/agent_run.py). The agents that build_agent makes (src/devops_cli/ai/client/factory.py), for example the analysis step of refine, record their spend through _spend_hook, and that hook gives no cache_read_tokens. Thus most agent runs also cost the full prompt rate for their cached tokens.

    #4483 covers the build_agent hook. This issue keeps its scope: the LLMClient chat path only. I did not edit the body, so that the refine record of this issue stays valid.

  7. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 11, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 11, 2026
  9. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor
  10. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-11 14:13 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p1-highscope/aisource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions