fix(codex): extract turn/completed token usage on app-server runtime - #37196
Closed
briandevans wants to merge 1 commit into
Closed
fix(codex): extract turn/completed token usage on app-server runtime#37196briandevans wants to merge 1 commit into
briandevans wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds per-turn token usage extraction for the Codex app-server transport, and surfaces it to the runtime so the conversation loop can observe context growth similar to other transports.
Changes:
- Parse
turn.usagefrom theturn/completednotification intoTurnResult.usage(supporting dict and bare-int shapes). - Surface the parsed usage in
run_codex_app_server_turnoutput ascodex_usage. - Add/extend tests to cover usage presence/absence and total derivation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/agent/transports/test_codex_app_server_session.py | Adds assertions and a new test class to validate per-turn usage parsing behavior. |
| agent/transports/codex_app_server_session.py | Extends TurnResult and parses usage from turn/completed events. |
| agent/codex_runtime.py | Exposes per-turn usage on the returned runtime turn dict (codex_usage). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+596
to
+602
| if tot is None and isinstance(inp, int): | ||
| tot = inp + (out or 0) | ||
| result.usage = { | ||
| "input_tokens": inp or 0, | ||
| "output_tokens": out or 0, | ||
| "total_tokens": tot or 0, | ||
| } |
| # per-turn usage; the codex app-server path was the lone blind spot | ||
| # (issue #36801) — without this the runtime cannot see a session growing | ||
| # toward the context window until the backend 400s and the session resets. | ||
| usage: Optional[dict] = None |
Comment on lines
+587
to
+595
| # Capture per-turn token usage when codex reports it. The | ||
| # app-server nests it under turn.usage; tolerate both the | ||
| # {input_tokens,output_tokens,total_tokens} dict shape and a | ||
| # bare int total. Advisory only — never raises. | ||
| raw_usage = turn_obj.get("usage") | ||
| if isinstance(raw_usage, dict): | ||
| inp = raw_usage.get("input_tokens") | ||
| out = raw_usage.get("output_tokens") | ||
| tot = raw_usage.get("total_tokens") |
| # so the conversation loop can observe context growth on this runtime | ||
| # path the way the chat-completions path already does — the | ||
| # prerequisite signal for proactive compaction (left to a follow-up). | ||
| "codex_usage": getattr(turn, "usage", None), |
The codex app-server runtime (provider: openai-codex) extracted no token usage from turn/completed events, so Hermes was blind to a session growing toward the context window until the backend 400s and the session hard-reset to history=0 (issue NousResearch#36801, root cause NousResearch#2). Other transports already surface per-turn usage (bedrock.py, chat_completions.py, anthropic.py); the codex app-server path was the lone gap. Parse turn.usage off the terminal turn/completed event into TurnResult.usage (tolerant of the {input_tokens,output_tokens,total_tokens} dict shape and a bare-int total; derives total when absent; never raises) and surface it as codex_usage on the codex_runtime return dict, giving the conversation loop the context-growth signal it needs.
briandevans
force-pushed
the
fix/codex-app-server-extract-turn-usage-36801
branch
from
June 5, 2026 22:16
8c0e05f to
605f0e3
Compare
Contributor
Author
|
Closing to keep the queue focused — no maintainer pickup in 26 days. Happy to reopen if the Codex app-server token-usage extraction is still useful. |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The codex app-server runtime (
provider: openai-codex) extracts NO token usage fromturn/completedevents, so Hermes is blind to a session growing toward the context window until the backend 400s and the session hard-resets tohistory=0(issue #36801, root cause #2). Other transports already surface per-turn usage (bedrock.py,chat_completions.py,anthropic.py); the codex app-server path was the lone gap. This PR parsesturn.usageoff the terminalturn/completedevent intoTurnResult.usageand surfaces it ascodex_usageon the runtime return dict, giving the conversation loop the context-growth signal it needs.Related Issue
Fixes #36801
Type of Change
Changes Made
agent/transports/codex_app_server_session.py: addusage: Optional[dict]toTurnResult; parseturn.usagein the existingturn/completedhandler (tolerant of the{input_tokens,output_tokens,total_tokens}dict shape and a bare-int total; derives total when absent; never raises).agent/codex_runtime.py: surfacecodex_usageon therun_codex_app_server_turnreturn dict.tests/agent/transports/test_codex_app_server_session.py: addTestTurnUsage(4 cases) + a no-usage regression assertion on the existing happy-path turn.How to Test
uv run --with pytest --with pytest-xdist --with pytest-asyncio python3 -m pytest tests/agent/transports/test_codex_app_server_session.py -vturn.usageparse turns the 3 extraction tests red (TypeError: 'NoneType' object is not subscriptable); restoring it returns all 61 to green.test_turn_completed_missing_usage_is_none) proves older app-server builds / omitted usage leaveusageasNonerather than fabricating zeros.Checklist
Code
fix(scope):,feat(scope):, etc.)tests/agent/transports/test_codex_app_server_session.py, 61 passing) — fullpytest tests/ -qnot run locallyDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/A (no config keys)CONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/A