Add /codex:usage command to show rate limits and usage#152
Add /codex:usage command to show rate limits and usage#152sabiut wants to merge 4 commits intoopenai:mainfrom
Conversation
Adds a new `/codex:usage` slash command that displays Codex plan type, rate limits, and credit balance without leaving Claude Code. Reads ~/.codex/auth.json and calls the OpenAI usage API directly since the Codex CLI does not yet expose a `usage` subcommand.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: abd9131eef
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const authPath = resolveCodexAuthPath(); | ||
| if (!fs.existsSync(authPath)) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
Resolve Codex auth from configured credential storage
fetchCodexUsage treats a missing auth.json file as unauthenticated, but Codex logins are not guaranteed to live at that path (e.g., keychain-backed auth in default auto mode, or relocated state via CODEX_HOME). In those setups users are genuinely logged in, yet /codex:usage will always return the “auth.json not found” error and never reach the API call. Please load auth from Codex’s configured credential store (or obtain a token via Codex itself) instead of hard-failing on file absence.
Useful? React with 👍 / 👎.
Respect CODEX_HOME env var for auth.json path. When no token file is found, check codex login status to distinguish between keychain-backed auth (with a helpful fallback message) and genuinely unauthenticated users.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 37e13f1eb2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
plugins/codex/scripts/lib/render.mjs
Outdated
| } | ||
|
|
||
| const data = report.data ?? {}; | ||
| const planType = report.planType ?? data.plan_type ?? "unknown"; |
There was a problem hiding this comment.
Prefer usage API plan over decoded ID token
renderUsageReport currently prioritizes report.planType over data.plan_type, but report.planType is populated from the local auth.json JWT claim in fetchCodexUsage. If local auth state is stale (for example after a plan change), /codex:usage will print an outdated plan even when the API response already includes the current plan. Use data.plan_type first when it exists, and fall back to the decoded token only when the API omits plan info.
Useful? React with 👍 / 👎.
The usage API response reflects the current plan; the local JWT may be stale after a plan change. Swap the priority so data.plan_type wins when present and the decoded token is only a fallback.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4320482d6e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
plugins/codex/scripts/lib/render.mjs
Outdated
| } else if (credits.has_credits && credits.balance != null) { | ||
| lines.push(`Credits: $${credits.balance.toFixed(2)} remaining`); |
There was a problem hiding this comment.
Coerce credit balance before calling toFixed
renderUsageReport assumes credits.balance is a number, but the value comes from JSON and may be serialized as a string; in that case credits.balance.toFixed(2) throws and /codex:usage fails instead of rendering usage data. Please normalize with Number(...) (and validate it is finite) before formatting so the command remains resilient to API serialization differences.
Useful? React with 👍 / 👎.
The API may serialize balance as a string. Normalize with Number() and validate with isFinite before calling toFixed to avoid a runtime error.
Summary
/codex:usageslash command that displays Codex plan type, rate limits (primary/weekly), code review limits, and credit balance without leaving Claude Code~/.codex/auth.jsonand callsGET /v1/codex/usagedirectly, since the Codex CLI does not yet expose ausagesubcommand--jsonflag for structured outputCloses #102
Changes
plugins/codex/commands/usage.mdplugins/codex/scripts/lib/codex.mjsfetchCodexUsage()— reads auth, calls usage API, decodes plan type from JWTplugins/codex/scripts/lib/render.mjsrenderUsageReport()— formats limits, credits, and plan infoplugins/codex/scripts/codex-companion.mjshandleUsagehandler +usagecase in switch + updatedprintUsage()tests/commands.test.mjstests/render.test.mjsExample output
Test plan
/codex:usagewith a valid Codex login/codex:usage --jsonfor structured output/codex:usagewithout auth to verify error message