Skip to content

[codex] add account usage API#15

Merged
upsetbit merged 10 commits into
masterfrom
codex/account-usage-api
Jul 7, 2026
Merged

[codex] add account usage API#15
upsetbit merged 10 commits into
masterfrom
codex/account-usage-api

Conversation

@upsetbit

@upsetbit upsetbit commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a separate account-usage API across all codexcw surfaces:

  • Go: GetAccountUsage(ctx, AccountUsageRequest)
  • Rust: get_account_usage(AccountUsageRequest)
  • Node: getAccountUsage()
  • Python: get_account_usage() and codexcw.aio.get_account_usage()

The helper talks to codex app-server --stdio, reads account rate limits, token usage, and account metadata, and accepts executable/env overrides with CODEX_HOME defaulting to ~/.codex when absent.

Details

  • Preserves raw JSON-RPC results for rate limits, token usage, and account data.
  • Handles real app-server payloads where optional rate-limit fields can be null.
  • Exposes token counters as strings to avoid precision loss in JavaScript.
  • Adds opt-in live tests guarded by CODEXCW_LIVE_CODEX=1 for real Codex validation.
  • Documents account usage and the /fast recipe via service_tier="priority".

Validation

Normal checks:

  • go test ./...
  • cargo test --workspace
  • npm test
  • uv run pytest
  • git diff --check
  • pre-push markdownlint-cli2, lychee, and gitleaks

Live checks against the real local Codex executable:

  • CODEXCW_LIVE_CODEX=1 go test . -run TestLiveGetAccountUsageAndFastMode -v
  • CODEXCW_LIVE_CODEX=1 cargo test -p codexcw live_account_usage_and_fast_mode -- --nocapture
  • CODEXCW_LIVE_CODEX=1 node --test --test-name-pattern 'live getAccountUsage' "__test__/**/*.test.mjs"
  • CODEXCW_LIVE_CODEX=1 uv run pytest tests/test_smoke.py -k live_get_account_usage_and_fast_mode -q

upsetbit and others added 10 commits July 7, 2026 14:37
- Treat JSON-RPC error replies on the optional usage/account reads as
  absent fields and propagate transport failures instead of swallowing
  them after killing the child.
- Skip messages that carry a method (notifications and server-initiated
  requests) when matching responses by id.
- Capture a bounded stderr tail and attach it to app-server errors.
- Add a per-request timeout to AccountUsageRequest (default 10s).
- Share the default executable name through a crate constant.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Send params {} on account/read; the real app-server rejects the
  request without it, leaving Account permanently nil.
- Replace the per-request read goroutines with a single long-lived
  reader, removing a data race on the shared bufio.Reader.
- Skip messages that carry a method when matching responses by id.
- Treat JSON-RPC error replies on the optional reads as absent fields
  and propagate transport failures instead of swallowing them.
- Capture stderr through the bounded, mutex-guarded tail buffer.
- Accept string-encoded numbers in rate-limit window fields, matching
  the core decoder and observed payloads.
- Add AccountUsageRequest.Timeout (default 10s), reuse the
  defaultExecutable constant and the runner's env merge convention.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add timeoutMs to the getAccountUsage options, mapped to the core
per-request JSON-RPC timeout (default 10s).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Add AccountUsageRequest.timeout (seconds), mapped to the core
  per-request JSON-RPC timeout (default 10s).
- Raise CodexcwError (kind "process") when the account usage result is
  missing, matching the Node binding, and unwrap outcomes through
  _result_or_raise.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@upsetbit upsetbit marked this pull request as ready for review July 7, 2026 18:53
@upsetbit upsetbit merged commit 75a4403 into master Jul 7, 2026
11 checks passed
@upsetbit upsetbit deleted the codex/account-usage-api branch July 7, 2026 18:53
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