Skip to content

Add /codex:usage command to show rate limits and usage#152

Open
sabiut wants to merge 4 commits intoopenai:mainfrom
sabiut:feature/codex-usage-command
Open

Add /codex:usage command to show rate limits and usage#152
sabiut wants to merge 4 commits intoopenai:mainfrom
sabiut:feature/codex-usage-command

Conversation

@sabiut
Copy link
Copy Markdown

@sabiut sabiut commented Apr 5, 2026

Summary

  • Adds a new /codex:usage slash command that displays Codex plan type, rate limits (primary/weekly), code review limits, and credit balance without leaving Claude Code
  • Reads ~/.codex/auth.json and calls GET /v1/codex/usage directly, since the Codex CLI does not yet expose a usage subcommand
  • Supports --json flag for structured output

Closes #102

Changes

File Description
plugins/codex/commands/usage.md New slash command definition
plugins/codex/scripts/lib/codex.mjs fetchCodexUsage() — reads auth, calls usage API, decodes plan type from JWT
plugins/codex/scripts/lib/render.mjs renderUsageReport() — formats limits, credits, and plan info
plugins/codex/scripts/codex-companion.mjs handleUsage handler + usage case in switch + updated printUsage()
tests/commands.test.mjs Updated command list, added usage command test
tests/render.test.mjs 5 new tests covering error state, rate limits, credits, unlimited credits, plan type fallback

Example output

# Codex Usage

Plan: plus

Limits:
- Primary limit: 73% left (resets Apr 1, 18:22)
- Weekly limit: 54% left (resets Apr 5, 09:15)
- Code review limit: 91% left (resets Apr 7, 14:30)

Test plan

  • All 70 existing tests pass
  • Manual test: run /codex:usage with a valid Codex login
  • Manual test: run /codex:usage --json for structured output
  • Manual test: run /codex:usage without auth to verify error message

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.
@sabiut sabiut requested a review from a team April 5, 2026 03:36
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +963 to +966
const authPath = resolveCodexAuthPath();
if (!fs.existsSync(authPath)) {
return null;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

}

const data = report.data ?? {};
const planType = report.planType ?? data.plan_type ?? "unknown";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +527 to +528
} else if (credits.has_credits && credits.balance != null) {
lines.push(`Credits: $${credits.balance.toFixed(2)} remaining`);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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.
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.

Add /codex:usage command to show rate limits and usage

1 participant