Skip to content

fix: extract usage and finish reason from OpenRouter provider metadata#188

Merged
konard merged 5 commits intomainfrom
issue-187-8d71545972a6
Feb 15, 2026
Merged

fix: extract usage and finish reason from OpenRouter provider metadata#188
konard merged 5 commits intomainfrom
issue-187-8d71545972a6

Conversation

@konard
Copy link
Contributor

@konard konard commented Feb 15, 2026

Summary

Fixes #187 - Not all data provided about response of kilo/glm-5-free model

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 PR 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.

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

  • js/src/session/index.ts: Fall back to openrouter metadata when standard usage is empty (inputTokens/outputTokens undefined)
  • js/src/session/processor.ts: Infer finish reason as "stop" when standard finishReason is undefined but openrouter usage is present

Case 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-free model:

echo 'hi' | agent --model kilo/glm-5-free

Token counts and finish reason are now correctly extracted.

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #187
@konard konard self-assigned this Feb 15, 2026
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>
@konard konard changed the title [WIP] Not all data provided about response of kilo/glm-5-free model fix: extract usage and finish reason from OpenRouter provider metadata Feb 15, 2026
@konard konard marked this pull request as ready for review February 15, 2026 11:50
konard and others added 3 commits February 15, 2026 12:51
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@konard
Copy link
Contributor Author

konard commented Feb 15, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $8.867155
  • Calculated by Anthropic: $6.546125 USD
  • Difference: $-2.321029 (-26.18%)
    📎 Log file uploaded as Gist (1064KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard konard merged commit f64a61b into main Feb 15, 2026
8 checks passed
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.

Not all data provided about response of kilo/glm-5-free model

1 participant