Skip to content

Record Codex token usage in cost tracking (SECFOO-37) - #21

Merged
vsn411 merged 1 commit into
secfoo-com:mainfrom
Varunpoojari:fix/secfoo-37-codex-token-usage
Oct 1, 2026
Merged

vsn411 merged 1 commit into
secfoo-com:mainfrom
Varunpoojari:fix/secfoo-37-codex-token-usage

Conversation

@Varunpoojari

Copy link
Copy Markdown
Collaborator

Why

Codex runs showed tokens as 0 / "-" in secfoo cost and the dashboard, even though codex exec reports usage: it ends stderr with a tokens used line followed by the run total. extract_usage() only ever receives stdout, so this was never read.

Found during SECFOO-13 E2E testing (run 423e1d8a…: Codex used 15,201 tokens; secfoo recorded 0).

What

  • base.py: new extract_usage_from_stderr() hook on AgentAdapter. It is only consulted when extract_usage() finds nothing, so Claude/Gemini behaviour is unchanged.
  • codex.py: parse the last tokens used total from stderr.
  • Codex reports a single total, with no input/output split and no cost. It is stored as input_tokens; output_tokens and cost_usd stay None, so they show as "-".

Tests

  • 7 new tests: real stderr sample, last-occurrence wins, missing summary, prose containing "tokens used", and run() fallback in both directions.
  • pytest tests/test_agents: 64 passed.
  • Full suite: 705 passed. The 4 failures in test_web/test_activity_routes.py / test_architecture_dashboard.py also fail on main @ eeb2d17 and are unrelated.

Follow-up

A dedicated total_tokens column would be more accurate than using input_tokens for Codex, but it needs a schema change, so it's out of scope here.

Jira: SECFOO-37

codex exec prints its run total as a 'tokens used' line on stderr, but
extract_usage() only ever saw stdout, so Codex runs showed 0 / '-' in
secfoo cost and the dashboard.

- base.py: add an extract_usage_from_stderr() hook, consulted only when
  extract_usage() finds nothing, so claude/gemini are unaffected.
- codex.py: parse the last 'tokens used' total from stderr. Codex gives
  a single total with no split and no cost, so it is stored as
  input_tokens and cost stays unknown.
- Tests for the parser and for the run() fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BnvD24nuxVRZF72ZGNT6Rx
@vsn411
vsn411 merged commit 23bb624 into secfoo-com:main Oct 1, 2026
2 checks passed
@Varunpoojari Varunpoojari mentioned this pull request Oct 9, 2026
4 tasks done
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.

2 participants