fix: extract usage and finish reason from OpenRouter provider metadata#188
Merged
fix: extract usage and finish reason from OpenRouter provider metadata#188
Conversation
Adding CLAUDE.md with task information for AI processing. This file will be removed when the task is complete. Issue: #187
When using OpenRouter-compatible APIs (like Kilo Gateway), the standard AI SDK usage object may be empty while the actual usage data is in providerMetadata.openrouter.usage. This fix adds fallback logic to extract token counts and finish reason from provider metadata. Root cause: The @openrouter/ai-sdk-provider SDK initializes usage with NaN values and only populates them when streaming chunks contain usage data in the expected format. When the Kilo API sends usage data in a different format or timing, the standard usage remains empty while providerMetadata contains the actual data. Changes: - session/index.ts: Fall back to openrouter metadata when standard usage is empty (inputTokens/outputTokens undefined) - session/processor.ts: Infer finish reason as "stop" when standard finishReason is undefined but openrouter usage is present This enables accurate token counting and cost calculation for all OpenRouter-compatible providers including Kilo Gateway. Fixes #187 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
kilo/glm-5-free modelCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This reverts commit 8c748c3.
Contributor
Author
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
Now working session is ended, feel free to review and add any feedback on the solution draft. |
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
Fixes #187 - Not all data provided about response of
kilo/glm-5-freemodelWhen using OpenRouter-compatible APIs (like Kilo Gateway), the standard AI SDK usage object may be empty while the actual usage data is in
providerMetadata.openrouter.usage. This PR adds fallback logic to extract token counts and finish reason from provider metadata.Root Cause
The
@openrouter/ai-sdk-providerSDK initializes usage withNaNvalues and only populates them when streaming chunks contain usage data in the expected format. When the Kilo API sends usage data in a different format or timing, the standard usage remains empty whileproviderMetadatacontains the actual data.Before fix:
{ "reason": "unknown", "cost": 0, "tokens": { "input": 0, "output": 0, "reasoning": 0, "cache": { "read": 0, "write": 0 } } }After fix:
{ "reason": "stop", "cost": 0, "tokens": { "input": 12004, "output": 103, "reasoning": 86, "cache": { "read": 0, "write": 0 } } }Changes
openroutermetadata when standard usage is empty (inputTokens/outputTokensundefined)"stop"when standardfinishReasonis undefined but openrouter usage is presentCase Study
A detailed case study with root cause analysis is documented in docs/case-studies/issue-187/case-study.md.
Upstream Issue
Reported to the OpenRouter AI SDK provider maintainers: OpenRouterTeam/ai-sdk-provider#419
Testing
Verified manually with
kilo/glm-5-freemodel:Token counts and finish reason are now correctly extracted.