Repository navigation
Description
Summary
When running OpenHands locally with minimax-m3, every LLM call crashes in usage telemetry:
AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'
This does not reproduce on the hosted SaaS, and does not reproduce with other models locally.
Root cause
openhands/sdk/llm/utils/telemetry.py normalize_usage() reads cache-write tokens via a bare attribute access guarded only by model_fields_set:
cache_write = 0
if (
prompt_details is not None
and "cache_creation_tokens" in prompt_details.model_fields_set
):
cache_write = int(prompt_details.cache_creation_tokens or 0) # AttributeErrorlitellm's PromptTokensDetailsWrapper.__init__() deletes optional fields whose value is None from the instance:
if self.cache_creation_tokens is None:
del self.cache_creation_tokensSo when a provider reports prompt_tokens_details with cache_creation_tokens: null, the field is present in model_fields_set (guard passes) but physically deleted from the instance (bare access raises AttributeError).
minimax-m3's OpenAI-compatible usage emits exactly {"cached_tokens": ..., "cache_creation_tokens": null}; the other tested models either omit the field or report a real integer, so they don't trip it.
Reproduced against litellm 1.93.0 (the SDK pin) directly:
model_fields_set has it: True
AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'
hasattr: False
Why SaaS is unaffected / why this is a regression
- The older SDK telemetry read cache-write tokens via
getattr(usage, "_cache_creation_input_tokens", 0)— a litellmPrivateAttrthat is always present (default0) and never deleted. The SaaS still runs that code path. - Commit
52feb470("fix(sdk): normalize LLM usage telemetry through a typed adapter", fix(sdk): normalize LLM usage telemetry through a typed adapter #5029, shipped in SDK v1.49.5/v1.49.6) rewrote this to accessprompt_details.cache_creation_tokensguarded only bymodel_fields_set, which is unsafe given litellm'sdel-on-Nonebehavior.
Can it be fixed in litellm?
No — not by upgrading. The del self.cache_creation_tokens (on None) behavior is present across 1.93.0 → 1.100.0 → latest main → latest PyPI (1.102.1). The mismatch between model_fields_set (says present) and the bare attribute (deleted) is inherent to the SDK's access pattern regardless of litellm version.
Proposed fix
Use getattr with a default instead of a bare attribute access in normalize_usage():
cache_write = int(getattr(prompt_details, "cache_creation_tokens", 0) or 0)This tolerates the field being set-but-deleted while preserving the existing model_fields_set semantics (only count a cache write when the provider actually reported the field). A regression test is added in the accompanying PR.
Environment
- SDK: v1.49.5+ (regression introduced in
52feb470) - litellm: 1.93.0 (and reproduces on every version through latest)
- Model:
minimax-m3
OpenHands AI triage
The following comments and acceptance criteria were added by the OpenHands AI agent.
Triage
This is a well-scoped, already-diagnosed regression. Verified against the repository at v1.49.6: normalize_usage() in openhands-sdk/openhands/sdk/llm/utils/telemetry.py guards the cache-write read with "cache_creation_tokens" in prompt_details.model_fields_set and then does a bare prompt_details.cache_creation_tokens. In litellm 1.93.0 (the workspace pin) PromptTokensDetailsWrapper.__init__ deletes that attribute when its value is None, so a provider that reports cache_creation_tokens: null (as minimax-m3 does) passes the guard but raises AttributeError. The documented history (52feb470 in #5029) makes this a local-only regression, consistent with the report.
Bounded scope: make normalize_usage() tolerant of a cache_creation_tokens field that is recorded in model_fields_set but deleted from the instance, so a chat-completions response from such a provider normalizes to cache_write_tokens == 0 instead of crashing. The canonical normalization boundary (normalize_usage() / UsageSnapshot) is the place to fix this; no other call site should need to change.
Explicit non-goals: upgrading or patching litellm (the del-on-None behavior is present across 1.93.0 through current releases), changing UsageSnapshot fields or their meaning, altering cost accounting, and changing behavior for providers that report a real integer or omit the field.
Steps to Reproduce
- Run OpenHands locally with
minimax-m3(litellm 1.93.0) against a provider whose OpenAI-compatible usage reportsprompt_tokens_detailscontainingcache_creation_tokens: null. - Make any LLM call.
- Usage telemetry raises
AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'.
Acceptance Criteria
- A chat-completions
Usagewhoseprompt_tokens_detailslistscache_creation_tokensinmodel_fields_setbut has that attribute deleted (litellm'sdel-on-None) is normalized bynormalize_usage()tocache_write_tokens == 0with noAttributeError. - A provider that reports an integer
cache_creation_tokensstill yields that value, and a provider that never sets the field still yieldscache_write_tokens == 0(existingmodel_fields_setsemantics preserved). - Other normalized fields (
prompt_tokens,completion_tokens,reasoning_tokens,cache_read_tokens) are unchanged for the affected response, andnormalize_usage()still returnsNonefor unrecognized/absent usage. - An LLM call against a usage shape containing
cache_creation_tokens: nullcompletes without the telemetryAttributeErrorand records token usage/cost as before. - A regression test reproducing the set-but-deleted field lives under
tests/sdk/llm/test_llm_telemetry.pyand fails on the current code and passes after the change; the existingtests/sdk/llmtelemetry tests continue to pass. - No public API, serialized
UsageSnapshot/Metricsshape, or persisted-data compatibility change is introduced.
Activity
all-hands-bot commentedon Sep 25, 2026
Issue #5326 reports the same AttributeError from Telemetry._cache_buckets reading details.cache_creation_tokens guarded only by model_fields_set, breaking litellm PromptTokensDetailsWrapper when the field is None/deleted. This is the identical root cause, file, and getattr-based fix already tracked by several open issues. #5168 is the community-designated canonical thread (it carries ready-for-dev and the active fix PR #5169), and #5099/#5134 already redirect there. #5326 adds only provider-specific detail (minimax-m3, explicit cache_creation_tokens: null, regression commit 52feb47/#5029), which belongs as a comment on the canonical issue rather than a separate thread.
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
This comment was created by an AI assistant (OpenHands) on behalf of the repository maintainer.