Missing `stream_options: { include_usage: true }` when using `Wire::Chat`
Resolved 💬 7 comments Opened Sep 12, 2025 by fannnzhang Closed Sep 12, 2025
What version of Codex is running?
source code on main branch
Which model were you using?
_No response_
What platform is your computer?
_No response_
What steps can reproduce the bug?
Description
When Codex uses Wire::Chat, the request payload for the Chat Completions API is currently built like this:
let tools_json = create_tools_json_for_chat_completions_api(&prompt.tools)?;
let payload = json!({
"model": model_family.slug,
"messages": messages,
"stream": true,
"tools": tools_json,
});
However, it does not set the stream_options field with include_usage: true.
As a result, during a rollout, the tokenusage information is missing from the stream.
The JSONL logs show empty usage events:
{"timestamp":"2025-09-12T01:59:35.844Z","type":"event_msg","payload":{"type":"token_count","info":null}}
Expected Behavior
Example of correct streamed output:
{
"id": "chatcmpl-xxx",
"object": "chat.completion.chunk",
"usage": {
"prompt_tokens": 50,
"completion_tokens": 100,
"total_tokens": 150
}
}
What is the expected behavior?
_No response_
What do you see instead?
_No response_
Additional information
_No response_
7 Comments
Also see PR #1583.
@fannnzhang, why did you give up an just create your own fork of Codex CLI to fix this issue in:
?
Was OpenAI just not going to accept a patch that allowed you to even optionally turn this on?
The main reason I ended up creating my own fork was simply that my original PR sat for a long time without any response or review from the OpenAI team. At the time, it also felt like wire::chat wasn’t a major focus for Codex anymore, so I gradually lost motivation to keep pushing the patch.
Much later the Codex team did reach out on some of my other open PRs, apologized for the long silence, and explained they didn’t have the bandwidth to review community contributions earlier. By the time I received that message, I had already moved on and no longer had the enthusiasm to maintain or update the earlier PRs
@fannnzhang, thanks for the response. We really need model inference metrics for our benchmarking and research activities. If OpenAI will not at least add an option to request model inference metrics for the streaming response format, then we may look into a man-in-the-middle server to set
stream_options: { include_usage: true }and then forward the request on the to target model endpoint.According to Gemini 3.0, this may be pretty easy to do with LiteLLM:
I am curious to see if that works.
The advantage of going with a MITM server like this is that it should work with any AI agent and any OpenAI-compatible model endpoint server. And we could even implement custom logging with such a server, in case we can't get it out of the agent very easily (but hopefully the
codexsession *.jsonl file would list the inference metrics as they are returned).@fannnzhang, with your permission, can we reopen this issue? OpenAI thinks it is resolved because your fork's PR:
auto-closed this issue (because it had "Fixes #3513" in the description field).
See the OpenAI response to your PR in:
where they said:
and from that, they closed that PR! I think had that PR stayed open, OpenAI might have considered the PR and merged the change?
Or should I just open a new issue?
It would be great if we did not need to have forks of codex to do our jobs 👍
I’m interested in helping reopen this PR and implementing the missing
pieces of the wire::chat protocol — aligning it with the relevant
functionality already defined in wire::response inside Codex. I’m happy to
contribute the necessary work to get this feature into a merge-ready state.
However, before I invest further effort, I would like to confirm whether
the maintainers are open to having this functionality included in the main
branch. If I resubmit the PR but it receives no review or cannot be merged,
then the work may end up without practical value.
So if the team is supportive of adding this feature, I’m willing to proceed
and prepare a new PR with the required updates.
Thanks, and looking forward to your feedback.
Roscoe A. Bartlett @.***> 于2025年11月26日周三 01:57写道:
pieces of the wire::chat protocol — aligning it with the relevant
functionality already defined in wire::response inside Codex. I’m happy to
contribute the necessary work to get this feature into a merge-ready state.
@fannnzhang, perhaps we can start by just reopening this issue? Then there is a chance this might get onto OpenAI's backlog to consider?
But in case you want to do more, it looks like you have been merging upstream into your fork by looking at the commit:
so you might just be able to to rebase your commits on top of the current upstream 'main' and push a new PR?
Or I could give that a try. I have never built and run
codexfrom source but this might be a chance for me to go through that. (I have been wanting to tweak the baked-in codex system prompt from some of our use cases anyway which will require building from a fork.)