Skip to content

Fix missing streaming usage tracking for OpenAI-compatible providers#2111

Merged
luispater merged 1 commit intorouter-for-me:devfrom
qzydustin:main
Mar 14, 2026
Merged

Fix missing streaming usage tracking for OpenAI-compatible providers#2111
luispater merged 1 commit intorouter-for-me:devfrom
qzydustin:main

Conversation

@qzydustin
Copy link
Contributor

OpenAI-compatible upstreams don't return usage data in streaming mode unless stream_options.include_usage is explicitly set to true in the request. This was already handled in kimi_executor and qwen_executor, but missed in openai_compat_executor.

As a result, all streaming requests through openai-compatibility providers showed 0 tokens in the usage statistics panel, while non-streaming requests worked fine.

Fix: Add stream_options.include_usage: true to the translated streaming payload in OpenAICompatExecutor.ExecuteStream(), matching the existing pattern in kimi/qwen executors.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug affecting OpenAI-compatible providers where streaming requests failed to report token usage statistics. The core issue stemmed from the omission of the stream_options.include_usage flag in the request, a detail already handled by other executors. The change ensures that all streaming interactions with these providers now accurately capture and report token consumption.

Highlights

  • Usage Tracking Fix: Addressed an issue where streaming requests to OpenAI-compatible providers were not reporting token usage by explicitly setting stream_options.include_usage to true in the request payload.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • internal/runtime/executor/openai_compat_executor.go
    • Added logic to include stream_options.include_usage: true in the translated streaming payload.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request correctly identifies and fixes an issue where usage statistics were not being tracked for streaming requests with OpenAI-compatible providers. The fix involves adding stream_options.include_usage: true to the request payload, which is the right approach. I have one suggestion to improve the robustness of the implementation by handling a potential error that is currently being ignored.


// Request usage data in the final streaming chunk so that token statistics
// are captured even when the upstream is an OpenAI-compatible provider.
translated, _ = sjson.SetBytes(translated, "stream_options.include_usage", true)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It's a best practice in Go to handle errors rather than ignoring them with _. The sjson.SetBytes function can return an error, for instance, if the translated byte slice does not contain valid JSON. While an error might be unlikely in the current code flow, explicitly checking for it makes the code more robust against future changes and prevents potential silent failures.

Suggested change
translated, _ = sjson.SetBytes(translated, "stream_options.include_usage", true)
translated, err = sjson.SetBytes(translated, "stream_options.include_usage", true)
if err != nil {
return nil, fmt.Errorf("failed to set stream_options.include_usage: %w", err)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sjson.SetBytes only errors on invalid path syntax. A hardcoded "stream_options.include_usage" with true can't fail. Same _ pattern used in qwen_executor:359 and all over claude_executor. Can switch to explicit check if the owner want, just matching existing style.

@luispater luispater changed the base branch from main to dev March 14, 2026 10:16
@luispater luispater merged commit 733fd8e into router-for-me:dev Mar 14, 2026
2 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.

2 participants