Codex-related libraries and tools
README.md

codex #

codex-x manages the Codex CLI accounts of a machine: what the codex command does not do for more than one account.

Each account is a Codex home: ~/.codex is default and ~/.codex-NAME is NAME, as CODEX_HOME selects them; nothing else records them. For each, codex-x reads the ChatGPT account it is signed in to and the rate-limit windows it has left, ranks the accounts by allowance left per hour until reset, and runs codex in the one named.

Installation #

opam install codex

Usage #

$ codex-x account list            # name, state, plan, identity, directory
$ codex-x account show work --json
$ codex-x usage                   # each window: share used, next reset
$ codex-x usage --recorded        # the newest rate limits Codex recorded
$ codex-x pick                    # highest allowance left per hour
$ codex-x pick --at 2026-10-14T22:00:00Z --json
$ eval "$(codex-x env work)"
$ codex-x run work -- exec "hello"
$ codex-x eval work -m gpt-6.1-sol --repo ~/src/mono --trials sol.jsonl cases/*

usage and pick ask the endpoint Codex reads its rate limits from (GET https://chatgpt.com/backend-api/wham/usage), with a 15 s bound, or with --recorded read the last record in the newest session file under the home's sessions/. A signed-out, expired or unreadable account, or one signed in with an API key, which has no plan windows, is reported by name with the reason. Account inspection reports stale access tokens; Codex renews them when it runs. No command output carries a token.

pick ranks accounts with headroom by the percentage left in their longest window per hour until its reset, highest first. A tie prefers more left, then the input order. Accounts whose windows are spent follow, ordered by when headroom returns; unreadable accounts follow those. usage sorts by weekly allowance remaining, most first. Exhausted accounts follow, soonest reset first, with only their weekly row. The note is empty except for excluded, exhausted or unread; the reset column gives when allowance returns. --json keeps every window and the order of pick. --at TIME ranks as at another time. The commands and the ranking are llm-eio's accounts library, which claude-x (in ocaml-claude) shares.

eval grades Codex on ocaml-llm's eval cases (ocaml-llm's README, eval). Each trial runs codex exec in the named account's home, in a fresh copy of the case's tree/ or a detached worktree of --repo at the case's base with its case.patch applied, with the case's answer schema as --output-schema. The answer is graded as mistral eval grades it, then the case's check runs in the tree, and the trial line takes the same shape, so the fleet supervisor (ocaml-agent) reads it as a capability. Codex keeps its own tools and budgets: a case's allow, steps and tokens do not apply, and variables naming a key, token, secret or password are removed from its environment.

Relay #

A credential holder serves native Responses over an explicit account list:

codex-x relay --listen 127.0.0.1:8445 work personal

Each name selects its private Codex home; default selects ~/.codex. The relay reads those homes alone. It replaces the client's placeholder and account-id header with the selected sign-in and preserves Responses bodies, protocol headers and response bytes, but for the tier: a call on the priority tier keeps it only when its x-agent-priority header is 0, a job on a P0 issue, and goes on the standard tier otherwise. A client that names itself P0 spends the faster tier; the relay's port is reached only through the model proxy. A session's prompt_cache_key, or its session-id header, keeps it on one account while that account has headroom. Missing identity is refused. An unreadable or signed-out account serves no request; another account is selected only from the explicit list.

Response headers supply each account's allowance windows, including model-specific families. Malformed named window data refuses that account; an empty update preserves its prior reading. Unavailable sign-ins or readings return 503; exhausted valid windows return 429. Tokens renew inside the credential holder under the account lock. A sign-out or replacement during a renewal wins, and a renewed sign-in is synchronized before use, mode 0600. Provider response bodies and store errors are absent from diagnostics.

--upstream defaults to https://chatgpt.com/backend-api/codex; --token-endpoint to https://auth.openai.com/oauth/token; --client-id selects the OAuth client. --max-connections is a positive count, 64 by default. Each request holds one bounded native body and reads at most the explicit account list; a stale account can make one bounded token renewal. The relay's port is private to the credential holder. Worker jobs use the worker's model proxy as their model exit and hold only its placeholder.

Tests #

The unit tests decode Codex's auth.json and rate-limit documents, and the cram test runs every command over a home with one account in each state, recorded fixtures only, never the live service; it asserts that no output carries a token.

License #

ISC. See LICENSE.md.