claude-code - 💡(How to fix) Fix Bug: "undefined is not an object (evaluating '$.input_tokens')" error crashes the session [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46932Fetched 2026-04-12 13:29:22
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Author
Timeline (top)
labeled ×4commented ×2

Error Message

TypeError: undefined is not an object (evaluating '$.input_tokens')

Code Example

TypeError: undefined is not an object (evaluating '$.input_tokens')
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

The application crashes with a error: "undefined is not an object (evaluating '$.input_tokens')".

This happens during long-running tasks or complex code analysis. It seems the UI component responsible for displaying token usage fails to handle cases where the usage object or specific token fields (like input_tokens) are missing or structured differently in the API response. Once this error occurs, the session becomes completely unresponsive.

What Should Happen?

The application should gracefully handle missing or differently structured usage data from the API without crashing the entire session. If token information is unavailable, it should either skip the display or show a placeholder instead of throwing an 'undefined' error.

Error Messages/Logs

TypeError: undefined is not an object (evaluating '$.input_tokens')

Steps to Reproduce

  1. Use Claude Code with an OpenAI-compatible provider (via CCswitch/LongCat).
  2. Perform a complex analysis task on a large codebase involving multiple file reads and git diffs.
  3. After several turns of conversation, the UI attempts to update the token usage.
  4. The error "undefined is not an object (evaluating '$.input_tokens')" appears and the session hangs.

Claude Model

Other

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

Claude Code version 2.1.96.cc1, running as a VSCode extension

Platform

Other

Operating System

Windows

Terminal/Shell

VS Code integrated terminal

Additional Information

I am using CCswitch to connect Claude Code to a third-party API provider (LongCat-Flash-Chat) that uses an OpenAI-compatible format.

The bug likely stems from a mismatch between the expected Anthropic usage object structure and the one returned by the compatible provider (which might use 'prompt_tokens' instead of 'input_tokens'). The frontend does not seem to have a null-check for the '$' or 'usage' object before accessing '.input_tokens'.

<img width="359" height="41" alt="Image" src="https://github.com/user-attachments/assets/730aaeb9-f59a-4aa7-b6b8-4f0c1cb63d58" />

extent analysis

TL;DR

The most likely fix is to add null checks for the 'usage' object and its properties, such as 'input_tokens', before accessing them in the UI component.

Guidance

  • Verify that the API response from the third-party provider (LongCat-Flash-Chat) indeed uses a different structure for token usage, such as 'prompt_tokens' instead of 'input_tokens'.
  • Add null checks for the 'usage' object and its properties before accessing them in the UI component to prevent the "undefined is not an object" error.
  • Consider adding a fallback or placeholder for cases where token information is missing or structured differently to ensure a graceful handling of such scenarios.
  • Review the CCswitch and LongCat integration to ensure that the expected usage object structure is correctly handled or adapted to the actual response from the provider.

Example

// Example null check for the 'usage' object and 'input_tokens' property
if (usage && usage.input_tokens) {
  // Access and display input_tokens
} else {
  // Handle missing input_tokens, e.g., display a placeholder
}

Notes

The provided fix assumes that the issue stems from a mismatch between the expected and actual API response structures. However, without direct access to the codebase or the specific API responses, this remains an educated guess. Further investigation into the API responses and the UI component's handling of these responses is necessary for a definitive solution.

Recommendation

Apply workaround: Add null checks and handle missing token information to prevent the application from crashing due to "undefined is not an object" errors, ensuring a more robust and user-friendly experience.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING