Skip to content

Comments

fix(usage): default token counts to 0 when provider returns nil#32

Merged
Rogermax merged 1 commit intomainfrom
fix/usage-nil-tokens
Jan 27, 2026
Merged

fix(usage): default token counts to 0 when provider returns nil#32
Rogermax merged 1 commit intomainfrom
fix/usage-nil-tokens

Conversation

@Rogermax
Copy link
Contributor

@Rogermax Rogermax commented Dec 30, 2025

Summary

  • Adds default: 0 to input_tokens, output_tokens, and total_tokens in the Usage struct

Why fix here instead of the monolith?

The error TypeError: Parameter 'input_tokens': Can't set Ai::Usage.input_tokens to nil occurs when:

  1. The AI provider returns a response without usage information
  2. TypeCoerce tries to construct the Usage struct with nil values
  3. Sorbet's runtime type checking throws TypeError because the fields are required Integers

The monolith code already has error handling for Ai::Error, but TypeError is a different exception class that bypasses this handling.

Options considered:

  1. ❌ Catch TypeError in the monolith - This is a workaround, not a fix. Every consumer would need to add this handling.
  2. ❌ Make fields T.nilable(Integer) - Forces all consumers to handle nil values.
  3. ✅ Add default: 0 - If the provider doesn't return usage data, we default to 0 (unknown usage). This is the most graceful solution.

Root cause investigation

The Usage struct was introduced in commit bd8a706 (Sep 23, 2025) without defaults. The issue likely started appearing after commit 02e83fd which "dropped backfilling from Mastra" - Mastra may have previously been filling in these values, and now they can arrive as nil in edge cases (partial timeouts, incomplete responses, etc.).

Test plan

  • All existing specs pass (83 examples, 0 failures)
  • Verified the change is backwards compatible

Fixes FCT-45236

🤖 Generated with Claude Code

Some AI providers may return responses without usage information
(input_tokens, output_tokens, total_tokens as nil). Previously this
caused a TypeError when Sorbet tried to construct the Usage struct
with nil values for required Integer fields.

By adding `default: 0` to these fields, the gem now gracefully handles
missing usage data instead of crashing.

Fixes FCT-45236

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Rogermax Rogermax requested a review from a team as a code owner December 30, 2025 10:19
@Rogermax Rogermax merged commit bf4cb90 into main Jan 27, 2026
1 check passed
@Rogermax Rogermax deleted the fix/usage-nil-tokens branch January 27, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants