Skip to content

Conversation

@skhedim
Copy link
Contributor

@skhedim skhedim commented Jan 29, 2026

What this PR does

Adds stream_options={"include_usage": True} to OpenAI streaming API calls to enable token usage metadata in streaming responses.

Problem

Without this parameter, the OpenAI API does not return token usage statistics (prompt_tokens, completion_tokens, total_tokens) in streaming mode, causing usage_metadata to always be None. This breaks token counting and usage tracking features.

Solution

Pass stream_options with include_usage=True when calling the OpenAI chat completions API in streaming mode. This feature is available in the OpenAI API since November 2023.

Testing

Added unit tests to verify:

  • stream_options is passed correctly in streaming calls
  • Usage metadata is properly propagated from streaming responses

Related Issue

Fixes token counting not working in streaming mode (usage_metadata is always None).

Add stream_options parameter with include_usage=True to OpenAI streaming
API calls. Without this option, the OpenAI API does not return token
usage statistics (prompt_tokens, completion_tokens, total_tokens) in
streaming mode, causing usage_metadata to always be None.

This change enables proper token counting for streaming responses,
which is required for usage tracking and monitoring.

Added unit tests to verify:
- stream_options is passed correctly in streaming calls
- Usage metadata is properly propagated from streaming responses

Signed-off-by: skhedim <sebastien.khedim@gmail.com>
Copilot AI review requested due to automatic review settings January 29, 2026 09:24
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 pull request enables usage metadata tracking in OpenAI streaming responses by adding the stream_options={"include_usage": True} parameter to streaming API calls.

Changes:

  • Modified OpenAI streaming implementation to include stream_options parameter for usage metadata
  • Added comprehensive unit tests to verify the parameter is passed correctly and usage metadata is properly propagated

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/kagent-adk/src/kagent/adk/models/_openai.py Added stream_options={"include_usage": True} parameter to streaming API call to enable usage metadata in responses
python/packages/kagent-adk/tests/unittests/models/test_openai.py Added two new tests: one verifying stream_options is passed correctly, and another verifying usage metadata propagation from streaming responses

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@supreme-gg-gg supreme-gg-gg left a comment

Choose a reason for hiding this comment

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

Makes sense, good catch 👍

@EItanya EItanya merged commit d332ea8 into kagent-dev:main Jan 29, 2026
21 of 22 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.

3 participants