Skip to content

AttributeError in Telemetry._cache_buckets for providers without prompt caching #5168

Description

@carvalab

Any provider whose usage response includes prompt_tokens_details without cache fields crashes profile validation and token recording with:

'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'

Reproduced with MiniMax (Custom Model openai/MiniMax-M3, also the native minimax/MiniMax-M3 provider, Base URL https://api.minimax.io/v1, openhands-sdk 1.49.1), and other reporters hit it with different providers and models, including GLM-5.3 (see #5099 and the comments below). Also verified working with ZAI glm-5.3-flash.

Root cause: Telemetry._cache_buckets in openhands-sdk/openhands/sdk/llm/utils/telemetry.py reads details.cache_creation_tokens directly, guarded only by "cache_creation_tokens" in details.model_fields_set. LiteLLM's PromptTokensDetailsWrapper.__init__ deletes unset optional fields, while its __setattr__ mirroring keeps the name in model_fields_set. So the guard passes and the read raises AttributeError.

Actual Behavior

Saving the profile fails validation with the AttributeError above. Reproduced against the installed SDK with python:

from litellm.types.utils import Usage, PromptTokensDetailsWrapper
from openhands.sdk.llm.utils.telemetry import Telemetry

usage = Usage(
    prompt_tokens=100,
    completion_tokens=50,
    total_tokens=150,
    prompt_tokens_details=PromptTokensDetailsWrapper(cached_tokens=0),
)
Telemetry._cache_buckets(usage)

model_fields_set contains cache_creation_tokens even though the attribute was deleted, so the guard passes and the direct read raises.

Acceptance Criteria

  • Saving an LLM profile for a provider without prompt caching passes validation instead of raising AttributeError
  • The cache buckets default to 0 when the provider returns no cache fields

Activity

  1. github-actions commented on Sep 18, 2026

    @github-actions
    Contributor

    ✅ ready-for-dev label applied

    This issue meets the readiness criteria and is ready for contribution.
    A contributor can now open a PR that links this issue (Fixes #<number>).

    This is an automated check — no AI was used to generate this comment.

  2. added
    bug
    llm
    priority:medium
    on Sep 18, 2026
  3. added
    ready-for-dev
    on Sep 18, 2026
  4. all-hands-bot commented on Sep 18, 2026

    @all-hands-bot
    Collaborator

    Issue #5168 reports the exact same root cause as open issues #5099 and #5134: Telemetry._cache_buckets in openhands-sdk/openhands/sdk/llm/utils/telemetry.py reads details.cache_creation_tokens guarded by a model_fields_set membership check, which passes even though LiteLLM's PromptTokensDetailsWrapper.__init__ deleted the unset attribute, raising AttributeError for any provider (e.g. MiniMax) whose prompt_tokens_details lacks cache fields. All three issues describe the same file, same attribute, same mechanism, and the same getattr(..., 0) fix; #5134 even documents the identical Agent Canvas + minimax-m3 reproduction. #5099 is the earliest and most thorough write-up, so it is the natural canonical thread. Discussion and fix planning should be consolidated there rather than spawning a third parallel fix (the author already opened PR #5169 against #5168, while #5135 targets #5134).

    Found 3 possible duplicate issues:

    1. #5099 — 1.48.0: _cache_buckets raises AttributeError on litellm PromptTokensDetailsWrapper
    2. #5134 — AttributeError: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens' in Telemetry._cache_buckets
    3. #4817 — [Bug]: LLM span cost ignores prompt-cache tokens and diverges from the provider/proxy cost

    This issue will be automatically closed as a duplicate in 3 days.

    • If your issue is a duplicate, please close it and 👍 the existing issue instead
    • To prevent auto-closure, add a comment or 👎 this comment

    This comment was created by an AI assistant (OpenHands) on behalf of the repository maintainer.

  5. added
    duplicate-candidate
    on Sep 18, 2026
  6. carvalab commented on Sep 18, 2026

    @carvalab
    Author

    I would keep this thread as the canonical one: it is the only one of the three carrying ready-for-dev, so it is currently the only issue a fix PR can link against. The linked PR #5169 is green on all checks, while #5135 is red on PR description validation and pre-commit.

    Coverage differs too: #5135 only reads cache_creation_tokens, so cache writes reported under the OpenAI naming (cache_write_tokens) get counted as 0. #5169 falls back to cache_write_tokens and also guards the cached_tokens read.

    Happy to rebase #5169 onto whichever thread maintainers prefer, but closing this issue would stall the one PR that currently passes the gates.

  7. removed
    duplicate-candidate
    on Sep 18, 2026
  8. Zowlyfon commented on Sep 18, 2026

    @Zowlyfon

    I'm experience this issue with more models, such as GLM-5.3

    It appears to be dependant on the provider as well as the model.

    The error currently means these models/providers aren't usable with OpenHands, it stalls the agent preventing it from continuing.

  9. changed the title [-]AttributeError saving MiniMax LLM profile: 'PromptTokensDetailsWrapper' object has no attribute 'cache_creation_tokens'[/-] [+]AttributeError in Telemetry._cache_buckets for providers without prompt caching[/+] on Sep 18, 2026
  10. carvalab commented on Sep 18, 2026

    @carvalab
    Author

    @Zowlyfon thanks, your GLM-5.3 report confirms it depends on what the provider sends back, not on one model. PR #5169 should cover it since it stops assuming the cache fields exist at all. Which provider are you using for GLM-5.3? I verified the fix with ZAI glm-5.3-flash and it works there, so if yours is a different route it would be good to know whether it behaves the same.

  11. Zowlyfon commented on Sep 18, 2026

    @Zowlyfon

    @carvalab For me I have no issues with GLM-5.3 Flash from OpenRouter, but Nebius with GLM-5.3 Flash has the issue.

  12. carvalab commented on Sep 18, 2026

    @carvalab
    Author

    @Zowlyfon you can verify it on your machine right now without waiting for the merge. The bug sits in the installed SDK, so patch it locally:

    1. Stop agent-canvas and start it fresh with npx so you run the latest backend.
    2. Find the installed file (it lives in uv tool environments, patch every copy shown):
      find ~/.cache/uv -path "*openhands/sdk/llm/utils/telemetry.py"
    3. In Telemetry._cache_buckets, replace this:
    cache_write = (
        details.cache_creation_tokens
        if "cache_creation_tokens" in details.model_fields_set
        else 0
    )
    return int(details.cached_tokens or 0), int(cache_write or 0)

    with this:

    cache_write = getattr(details, "cache_creation_tokens", 0) or getattr(
        details, "cache_write_tokens", 0
    )
    return int(getattr(details, "cached_tokens", 0) or 0), int(
        cache_write or 0
    )
    1. Restart agent-canvas.
    2. Important: open a NEW conversation and re-save the profile there. Retrying inside the same conversation keeps failing because it reuses the config snapshot from when it broke.

    If you would rather have the agent apply the patch, paste it this prompt:

    Find every copy of openhands/sdk/llm/utils/telemetry.py under ~/.cache/uv. In Telemetry._cache_buckets, replace the direct reads of cache_creation_tokens and cached_tokens on prompt_tokens_details with getattr-with-default reads so missing cache fields default to 0 instead of raising AttributeError. Print each file you changed with the before and after lines.

  13. Blaze147 commented on Sep 19, 2026

    @Blaze147

    I'm experiencing this exact issue as well on openhands/agent-canvas@1.20.0

    Most of my prompts seem to fail if using an OpenAI-compatable provider and I can't seem to find a way around this bug. I'm going to try uninstalling and reverting to 1.19.0.

  14. bpenner commented on Sep 20, 2026

    @bpenner

    I'm experiencing this exact issue as well on openhands/agent-canvas@1.20.0

    Same here. Running Qwen3.6-35b-A3B locally, connect through litellm, connected to openhands/agent-canvas@1.20.0. It worked fine with 1.18.0 with no issues. Updated to 1.20.0, now every prompt returns the error message.

  15. Blaze147 commented on Sep 20, 2026

    @Blaze147

    I'm experiencing this exact issue as well on openhands/agent-canvas@1.20.0

    Same here. Running Qwen3.6-35b-A3B locally, connect through litellm, connected to openhands/agent-canvas@1.20.0. It worked fine with 1.18.0 with no issues. Updated to 1.20.0, now every prompt returns the error message.

    @bpenner Same here. There is an apparently a fix PR in #5169, but it has yet to be reviewed and accepted. There are about 300 PRs still in the queue for the maintainers so I wouldn't expect this to be fixed in a public update anytime soon. I was able to work around the problem by always launching OpenHands using the following arguments in Terminal (since I'm on MacOS):

    source *environment* OH_AGENT_SERVER_VERSION=1.46.0 npx @openhands/agent-canvas@1.19.0

    ...basically forcing the OpenHands UI to launch with version 1.19.0 and the python sdk it sits on top of to launch with version 1.46.0.

    *** Update ***
    Thanks @bpenner for mentioning that OH_AGENT_SERVER_VERSION=1.47.0 and use OpenHands 1.20.0 also worked! I haven't had a chance to switch to those versions yet, but it would make sense if the Python SDK 1.47.0 was pre-break.

  16. openhands-ai commented on Sep 20, 2026

    @openhands-ai

    @Blaze147 it looks like you haven't created an OpenHands account yet. Please sign up at OpenHands Cloud and try again.

  17. bpenner commented on Sep 21, 2026

    @bpenner

    @Blaze147 Thanks for the reference to PR #5169. I did find this finally. I was able to pin export OH_AGENT_SERVER_VERSION=1.47.0 and use OpenHands 1.20.0.

    I had made so many changes, I didn't realize that I was using the code change from PR#5169 as well (cloned the sdk, edited the files, rebult the files, and then added the environment variable to point at the directory for the SDK). So, this another option to resolve the issue.

    Yeah, sounds like a lot of pull requests to work through.

  18. jerem62 commented on Sep 22, 2026

    @jerem62

    @Blaze147 Thanks for the reference to PR #5169. I did find this finally. I was able to pin export OH_AGENT_SERVER_VERSION=1.47.0 and use OpenHands 1.20.0.

    I had made so many changes, I didn't realize that I was using the code change from PR#5169 as well (cloned the sdk, edited the files, rebult the files, and then added the environment variable to point at the directory for the SDK). So, this another option to resolve the issue.

    Yeah, sounds like a lot of pull requests to work through.

    Thank you for the trick, this workaround is working fine.

  19. Dawncoral commented on Sep 25, 2026

    @Dawncoral

    Adding a second impact surface for this crash (agent-canvas 1.23.0 / agent-server 1.49.5 = SDK 1.49.5 / litellm 1.102.1):

    Beyond dying mid-run, the same AttributeError breaks LLM profile validation: profiles_router pre-flight sends a 1-token completion, normalize_usage raises on the missing attribute, and validation returns valid=False for what is actually a working configuration. The Canvas UI then blocks the profile save with no actionable error — the save request never reaches the server. From the user's seat it looks like "my API key won't save."

    #5169 should heal this path too, but it may be worth an explicit test: pre-flight arguably should treat telemetry-layer failures as non-blocking regardless.

    Local workaround remains a one-line getattr(..., None) guard in normalize_usage — profile save and full runs both pass with it.

    Disclosure: I operate an OpenAI-compatible API gateway; our endpoint returns cache_creation_tokens inside prompt_tokens_details.

  20. VascoSch92 commented on Oct 12, 2026

    @VascoSch92
    Member

    Fixed by #5327 (v1.50.0), same root cause as #5099. The repro from this issue now returns zero cache buckets instead of raising.

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

    bugllmpriority:mediumready-for-devsdk

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions