Skip to content

adapter-codex: parse "tokens used" footer into Cost event#404

Merged
edwin-zvs merged 1 commit into
mainfrom
codex-adapter-token-cost
Jun 14, 2026
Merged

adapter-codex: parse "tokens used" footer into Cost event#404
edwin-zvs merged 1 commit into
mainfrom
codex-adapter-token-cost

Conversation

@edwin-zvs

Copy link
Copy Markdown
Contributor

Summary

  • codex CLI prints a per-turn footer tokens used\n<N> on two stdout lines; the adapter was forwarding both as assistant Message events
  • swebench gap reports had blank token counts for codex sessions because no Cost event was ever emitted
  • now: spawn_stdout runs a two-line state machine — when a line equals tokens used, the next line is parsed as a comma/underscore-tolerant integer and emitted as SessionEvent::Cost { tokens_in: N, ... }. Both lines are swallowed so they no longer leak to Message.

codex reports a single total per turn (no in/out split), so the total goes into tokens_in as a conservative proxy — downstream analyzers can still distinguish "have tokens" from "don't" (tokens_out stays 0).

Test plan

  • cargo test --manifest-path crates/adapter-codex/Cargo.toml — 7 passed including new parse_token_count_handles_codex_formats
  • cargo build --release --manifest-path crates/adapter-codex/Cargo.toml — clean
  • After merge: rebuild release adapters, restart daemon, run a codex session, confirm swebench-zarvis/runner/analyze.py reports non-zero tokens_in

The codex CLI prints a per-turn footer:

    tokens used
    2,280

on two consecutive stdout lines. Until now the adapter forwarded both lines
as assistant `Message` events, polluting the transcript and leaving the
session with no `Cost` event for downstream analyzers (token counts came
back blank in our swebench gap reports).

spawn_stdout now tracks a two-line state machine: when a line equals
"tokens used", the next line is parsed as a comma/underscore-tolerant
integer and emitted as `SessionEvent::Cost { tokens_in: N, ... }`. Both
the header and the count are swallowed (no longer leak to Message).

codex reports a single "total tokens" per turn without splitting in/out,
so we store the total under `tokens_in` as a conservative proxy — the
prompt/context dominates and downstream analyzers can still distinguish
"have token info" from "don't" (tokens_out stays 0).
@edwin-zvs
edwin-zvs merged commit 46a5d33 into main Jun 14, 2026
1 check passed
@edwin-zvs
edwin-zvs deleted the codex-adapter-token-cost branch June 14, 2026 17:26
@edwin-zvs edwin-zvs mentioned this pull request Jun 16, 2026
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.

1 participant