Skip to content

Conversation

@lifeizhou-ap
Copy link
Collaborator

@lifeizhou-ap lifeizhou-ap commented Jan 14, 2026

Summary

Fix Issue #5907

Root Cause
When sending api request to openrouter via streaming mode, the response chunks with usage comes in the chunk without finish_reason. Example

- choices: [(content=Some(""), finish_reason=None, has_usage=true)], usage: Some({prompt_tokens: 5897, ...}

Fix
Skip the check for finish_reason to get the usage

Type of Change

  • Feature
  • Bug fix
  • Refactor / Code quality
  • Performance improvement
  • Documentation
  • Tests
  • Security fix
  • Build / Release
  • Other (specify below)

AI Assistance

  • This PR was created or reviewed with AI assistance

Testing

Manual testing and also tested open_ai, databricks and github copilot provider using the same open_ai logic

@lifeizhou-ap lifeizhou-ap marked this pull request as ready for review January 14, 2026 05:03
Copilot AI review requested due to automatic review settings January 14, 2026 05:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where OpenRouter streaming responses resulted in zero token counts being reported. The issue occurred because OpenRouter sends usage data in chunks that don't have finish_reason set, and the code was filtering out usage unless finish_reason was present.

Changes:

  • Removed the conditional check that only yielded usage when finish_reason is present, allowing usage to be captured regardless of finish_reason status

Copy link
Collaborator

@jamadeo jamadeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me wonder why the check is there in the first place -- does this hold up for all openai format providers?

@DOsinga
Copy link
Collaborator

DOsinga commented Jan 14, 2026

you should ask the author of that line

@lifeizhou-ap
Copy link
Collaborator Author

To be safe, I also tried Ollama, Xai and Tetrate providers which uses openai format, and it works.

I traced the origin of the logic, it was introduced in this PR https://github.com/block/goose/pull/2677/.

It was a while ago, maybe this is not valid any more.

@lifeizhou-ap lifeizhou-ap merged commit 9a96b84 into main Jan 15, 2026
26 checks passed
@lifeizhou-ap lifeizhou-ap deleted the lifei/openroute-stream-usage branch January 15, 2026 02:43
lifeizhou-ap added a commit that referenced this pull request Jan 15, 2026
* main:
  fixed 0 token in openrouter steaming (#6493)
  feat(goose-acp): enable parallel sessions with isolated agent state (#6392)
  copilot instruction to flag prelease docs (#6504)
  docs: acp mcp support (#6491)
  feat: add flatpak support for linux (#6387)
  fix(code_execution): serialize record_result output as JSON (#6495)
  perf(google): avoid accumulating thoughtSignatures across conversation history (#6462)
  fix(openai): make tool_call arguments optional and fix silent stream termination (#6309)
  fix: Improve error messages for invalid tool calls (#6483)
@jamadeo
Copy link
Collaborator

jamadeo commented Jan 15, 2026

To be safe, I also tried Ollama, Xai and Tetrate providers which uses openai format, and it works.

I traced the origin of the logic, it was introduced in this PR https://github.com/block/goose/pull/2677/.

It was a while ago, maybe this is not valid any more.

oh yeah that was me. If only I could remember why I did it that way. Thanks for the fix :)

zanesq added a commit that referenced this pull request Jan 15, 2026
…ased

* 'main' of github.com:block/goose:
  Fix popular topics not starting chat when clicked (#6508)
  fix[desktop]: deeplink ui repeat on refresh (#6469)
  fixed test compilation on main branch (#6512)
  fix: correctly parse extension name from tool call for MCP apps (#6482)
  fixed 0 token in openrouter steaming (#6493)
  feat(goose-acp): enable parallel sessions with isolated agent state (#6392)
  copilot instruction to flag prelease docs (#6504)
  docs: acp mcp support (#6491)
  feat: add flatpak support for linux (#6387)
zanesq added a commit that referenced this pull request Jan 16, 2026
* 'main' of github.com:block/goose: (28 commits)
  chore(deps): bump aiohttp from 3.13.0 to 3.13.3 in /scripts/provider-error-proxy (#6539)
  chore(deps): bump brotli from 1.1.0 to 1.2.0 in /scripts/provider-error-proxy (#6538)
  docs: temp correction for agent directory (#6544)
  chore: upgrade rmcp (#6516)
  docs: clarify directory in /documentation readme (#6541)
  Release 1.20.0
  Standalone mcp apps (#6458)
  don't add escaping to the command field (#6519)
  Fix popular topics not starting chat when clicked (#6508)
  fix[desktop]: deeplink ui repeat on refresh (#6469)
  fixed test compilation on main branch (#6512)
  fix: correctly parse extension name from tool call for MCP apps (#6482)
  fixed 0 token in openrouter steaming (#6493)
  feat(goose-acp): enable parallel sessions with isolated agent state (#6392)
  copilot instruction to flag prelease docs (#6504)
  docs: acp mcp support (#6491)
  feat: add flatpak support for linux (#6387)
  fix(code_execution): serialize record_result output as JSON (#6495)
  perf(google): avoid accumulating thoughtSignatures across conversation history (#6462)
  fix(openai): make tool_call arguments optional and fix silent stream termination (#6309)
  ...
@lifeizhou-ap
Copy link
Collaborator Author

lifeizhou-ap commented Jan 20, 2026

Hi @jamadeo,

I found why the check was there :) When I tested this PR, I only checked the used tokens for the context window. not the accumulated tokens to calculate the cost.

fix: #6587

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.

4 participants