Skip to content

Fix: Add stream_options.include_usage for Chat Completions API to enable token usage reporting - #3516

Closed
fannnzhang wants to merge 2 commits into
openai:mainfrom
fannnzhang:fix/chat-completions-usage
Closed

fannnzhang wants to merge 2 commits into
openai:mainfrom
fannnzhang:fix/chat-completions-usage

Conversation

@fannnzhang

Copy link
Copy Markdown

Description

This change adds the missing stream_options: { include_usage: true } field to the Chat Completions API requests, which enables the API to return token usage information in the final streaming chunk.

This allows the Codex client to properly track and report token usage for billing and monitoring purposes.

Additionally, a test case was added to verify that the stream_options field is correctly included in the request payload.

Related Issue

Fixes #3513

Changes

  • Added "stream_options": { "include_usage": true } to Chat Completions API request payloads.
  • Added a test to confirm the field is present and properly serialized.

Impact

  • Restores token usage reporting in rollouts.
  • Ensures accurate token accounting for monitoring and billing.

@fannnzhang

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@fannnzhang
fannnzhang force-pushed the fix/chat-completions-usage branch from 775b300 to aff744a Compare September 12, 2025 08:43
…ble token usage reporting

This change adds the missing stream_options: { include_usage: true } field to the Chat Completions API requests, which enables the API to return token usage information in the final streaming chunk. This allows the codex client to properly track and report token usage for billing and monitoring purposes.

Added a test case to verify that the stream_options field is correctly included in the request payload.
@fannnzhang
fannnzhang force-pushed the fix/chat-completions-usage branch from b1d8c1a to 202b2af Compare September 17, 2025 08:30
- Remove duplicate ChatCompletionsUsage struct in favor of protocol::TokenUsage
- Add field aliases to TokenUsage to support Chat Completions API field names
- Fix token usage tracking in chat completions streaming
- Set cached_input_tokens and reasoning_output_tokens to 0 for Chat Completions API

This fixes an issue where codex exec was not correctly tracking or using tokenUsage when using the wire::chat protocol.
@etraut-openai

Copy link
Copy Markdown
Contributor

Thanks for the contribution, and apologies for the slow response. We've received many PRs, and we don't have the bandwidth on the codex team to review all of them.

It looks like this PR is no longer relevant. The linked issue is marked as completed.

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 30, 2025
@openai openai unlocked this conversation Oct 30, 2025
@huntkalio

Copy link
Copy Markdown

@etraut-openai @fannnzhang
#3516
#1583
both pr were closed,

"model": model_family.slug,

so the Latest code have not set stream_options.include_usage

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.

Missing stream_options: { include_usage: true } when using Wire::Chat

3 participants