sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] LiteLLMModel drops cache_write_tokens and cache token fields omit explicit zeros

Open ekumar96 opened this issue 1 month ago • 0 comments

Human Overview

LiteLLMModel ignores prompt_tokens_details.cache_write_tokens from Azure/OpenAI-compatible responses, so cacheWriteInputTokens is missing. LiteLLMModel, OpenAIModel, and OpenAIResponsesModel also omit explicitly reported zero cache metrics because they use truthiness checks, making zero indistinguishable from unsupported or absent provider telemetry.

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

1.58.0

Language Runtime Version

Python 3.12

Operating System

Linux (AL2)

Installation Method

pip

Steps to Reproduce

  1. Install strands-agents==1.58.0.
  2. Run:
from types import SimpleNamespace

from strands.models.litellm import LiteLLMModel

usage = SimpleNamespace(
    prompt_tokens=3000,
    completion_tokens=10,
    total_tokens=3010,
    prompt_tokens_details=SimpleNamespace(
        cached_tokens=0,
        cache_write_tokens=2713,
    ),
)

model = LiteLLMModel(model_id="azure/test")
result = model.format_chunk(
    {
        "chunk_type": "metadata",
        "data": usage,
    }
)

print(result["metadata"]["usage"])
  1. Observe that cacheWriteInputTokens is absent despite the provider reporting cache_write_tokens=2713. cacheReadInputTokens is also absent despite the provider explicitly reporting zero.

Expected Behavior

The metadata usage should include:

{
  "cacheReadInputTokens": 0,
  "cacheWriteInputTokens": 2713
}

Provider-reported zero values should remain present so consumers can distinguish zero usage from unsupported or missing telemetry.

Actual Behavior

LiteLLMModel only reads cache writes from the Anthropic-style top-level cache_creation_input_tokens field. It ignores prompt_tokens_details.cache_write_tokens.

Cache read and write fields are also guarded by truthiness checks, so explicitly reported zero values are omitted.

Possible Solution

Read prompt_tokens_details.cache_write_tokens when present, while retaining cache_creation_input_tokens as a fallback.

For cache reads and writes, test field presence and type rather than truthiness:

if isinstance(value, int):
    usage_data[field] = value

Apply the explicit-zero behavior consistently to LiteLLMModel, OpenAIModel, and OpenAIResponsesModel, with tests for positive, zero, and absent values.

Additional Context

Extended context

Positive cache-write handling was added to the OpenAI Chat Completions and Responses adapters in PRs #4361 and #4193. However, the LiteLLM adapter still only supports top-level cache_creation_input_tokens.

All three adapters continue to omit explicit zero values. This matters for usage and cost consumers because an absent field means "unknown or unsupported," while zero means the provider reported that no cache operation occurred.

Related Issues

No response

ekumar96 avatar Oct 06 '26 17:10 ekumar96