You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug Report: /compact Command Fails in Chat Completions API Mode #2417
Result: Error message "🖐 stream disconnected before completion: token_usage was None in ResponseEvent::Completed"
What is the expected behavior?
No response
What do you see instead?
The /compact command (summarization feature) fails when using the Chat Completions API mode, causing the stream to disconnect and the session to become unresponsive.
Additional information
Possible reason:
Chat parser never populates token usage:
// In the Chat SSE parser, Completed events are created with token_usage: NoneResponseEvent::Completed{response_id:String::new(),token_usage:None}
Compact flow requires token usage:
// In drain_to_completed, None token_usage causes an errormatch event {ResponseEvent::Completed{ token_usage } => {ifletSome(token_usage) = token_usage {// Success path...}else{returnErr(CodexErr::Stream("token_usage was None in ResponseEvent::Completed",None,));}}// ...}
I tried to reproduce this but I couldn't with the following basic one:
model = "gpt-5"
model_provider = "openai-chat-completions"
[model_providers.openai-chat-completions]
# Name of the provider that will be displayed in the Codex UI.
name = "OpenAI using Chat Completions"
# The path `/chat/completions` will be amended to this URL to make the POST
# request for the chat completions.
base_url = "https://api.openai.com/v1"
# If `env_key` is set, identifies an environment variable that must be set when
# using Codex with this provider. The value of the environment variable must be
# non-empty and will be used in the `Bearer TOKEN` HTTP header for the POST request.
env_key = "OPENAI_API_KEY"
# Valid values for wire_api are "chat" and "responses". Defaults to "chat" if omitted.
wire_api = "chat"
# If necessary, extra query params that need to be added to the URL.
# See the Azure example below.
query_params = {}
Does this still always happen to you? Do you have repro steps so we can clearly see the situation?
I tried to reproduce this but I couldn't with the following basic one:
model = "gpt-5"
model_provider = "openai-chat-completions"
[model_providers.openai-chat-completions]
# Name of the provider that will be displayed in the Codex UI.
name = "OpenAI using Chat Completions"
# The path `/chat/completions` will be amended to this URL to make the POST
# request for the chat completions.
base_url = "https://api.openai.com/v1"
# If `env_key` is set, identifies an environment variable that must be set when
# using Codex with this provider. The value of the environment variable must be
# non-empty and will be used in the `Bearer TOKEN` HTTP header for the POST request.
env_key = "OPENAI_API_KEY"
# Valid values for wire_api are "chat" and "responses". Defaults to "chat" if omitted.
wire_api = "chat"
# If necessary, extra query params that need to be added to the URL.
# See the Azure example below.
query_params = {}
Does this still always happen to you? Do you have repro steps so we can clearly see the situation?
Thanks for your reply!
I've reproduced this issue consistently with my third-party provider. I'll test with OpenRouter or the official API to check if it's related to SSE chunk differences and share what I find.
In the meantime, I notice that in source code, for chat completion path, it never reads or forward the usage field from upstream. As I originally posted, the issue is likely relative to this.
With OpenRouter and gpt-5, I sent a prompt to Codex and the response did not include any token counts, but no error was raised.
However, when using /compact, the code raised an error if token_usage was None.
I opened a PR to align the behavior in both cases: #2640.
It may also be worth investigating why OpenRouter does not return any token counts at all — that could require a separate fix.
Activity
seratch commentedon Aug 18, 2025
I tried to reproduce this but I couldn't with the following basic one:
Does this still always happen to you? Do you have repro steps so we can clearly see the situation?
astrosheep-zero commentedon Aug 20, 2025
Thanks for your reply!
I've reproduced this issue consistently with my third-party provider. I'll test with OpenRouter or the official API to check if it's related to SSE chunk differences and share what I find.
In the meantime, I notice that in source code, for chat completion path, it never reads or forward the usage field from upstream. As I originally posted, the issue is likely relative to this.
codex/codex-rs/core/src/chat_completions.rs
Line 226 in 202af12
avoid error when /compact response has no token_usage (openai#2417)
oyiakoumis commentedon Aug 24, 2025
I was able to reproduce this issue with OpenRouter using the following config:
With OpenRouter and
gpt-5, I sent a prompt to Codex and the response did not include any token counts, but no error was raised.However, when using
/compact, the code raised an error iftoken_usagewasNone.I opened a PR to align the behavior in both cases: #2640.
It may also be worth investigating why OpenRouter does not return any token counts at all — that could require a separate fix.
avoid error when /compact response has no token_usage (#2417) (#2640)
avoid error when /compact response has no token_usage (openai#2417) (o…
avoid error when /compact response has no token_usage (openai#2417) (o…
avoid error when /compact response has no token_usage (openai#2417)
avoid error when /compact response has no token_usage (openai#2417) (o…