fix(usage): default token counts to 0 when provider returns nil#32
Merged
fix(usage): default token counts to 0 when provider returns nil#32
Conversation
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>
Tornamorell
approved these changes
Jan 27, 2026
oriolgual
approved these changes
Jan 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
default: 0toinput_tokens,output_tokens, andtotal_tokensin theUsagestructWhy fix here instead of the monolith?
The error
TypeError: Parameter 'input_tokens': Can't set Ai::Usage.input_tokens to niloccurs when:TypeCoercetries to construct theUsagestruct with nil valuesTypeErrorbecause the fields are required IntegersThe monolith code already has error handling for
Ai::Error, butTypeErroris a different exception class that bypasses this handling.Options considered:
TypeErrorin the monolith - This is a workaround, not a fix. Every consumer would need to add this handling.T.nilable(Integer)- Forces all consumers to handle nil values.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
Usagestruct was introduced in commitbd8a706(Sep 23, 2025) without defaults. The issue likely started appearing after commit02e83fdwhich "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
Fixes FCT-45236
🤖 Generated with Claude Code