Skip to content

fix(llm): cache token usage (cache_read_tokens) always reported as 0 when streaming is enabled #2896

Closed

Description

@xingyaoww

Bug Description

When stream=true is set in the LLM config, cache_read_tokens and cache_write_tokens are always reported as 0 in the SDK's usage metrics, even though prompt caching is working correctly at the provider level.

This is a reporting/display bug only — actual caching at the provider still works and cost savings are realized. But cache hit rate monitoring, cost tracking dashboards, and telemetry are affected.

Reproduction

  1. Configure the SDK with stream: true and caching_prompt: true
  2. Make multiple LLM calls through a LiteLLM proxy (e.g., llm-proxy.app.all-hands.dev)
  3. Observe that cache_read_tokens is always 0 in the metrics

With stream: false, cache_read_tokens correctly shows non-zero values.

Root Cause

When the SDK makes a streaming request to a LiteLLM proxy:

  1. The proxy auto-adds stream_options={"include_usage": True} to the upstream provider call, so the provider sends cache token usage in the final streaming chunk.

  2. The proxy correctly builds a usage-only final chunk (with prompt_tokens_details.cached_tokens) and sends it to the client.

  3. The client's LiteLLM streaming handler discards the usage-only chunk because the SDK doesn't pass stream_options={"include_usage": True}. In streaming_handler.py:

    else:  # no choices in chunk
        if (self.stream_options is not None 
            and self.stream_options["include_usage"] is True):
            return model_response  # ← Not taken (stream_options is None)
        return  # ← Usage-only chunk silently discarded!
  4. The client falls back to calculate_total_usage() which only extracts prompt_tokens and completion_tokens, dropping prompt_tokens_details (which contains cached_tokens).

Proposed Fix

When streaming is enabled, the SDK should automatically set stream_options={"include_usage": True} on the request kwargs. This ensures the client-side streaming handler properly receives and propagates the usage-only chunk with cache token details.

In _transport_call():

if enable_streaming:
    kwargs.setdefault("stream_options", {"include_usage": True})

Environment

  • SDK: openhands-sdk (software-agent-sdk)
  • LiteLLM proxy: v1.82.3-stable.patch.2
  • Models affected: minimax-m2.5, claude-sonnet-4.6, and likely all models that support prompt caching
  • Reported by user comparing trajectories from April 6 (stream=false, working) and April 17 (stream=true, broken)

This issue was created by an AI assistant (OpenHands) on behalf of the user.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions