Skip to content

Track add-on credits (Claude extra usage/prepaid, Codex credits) and harden usage pipeline - #6

Open
ninedter wants to merge 14 commits into
mainfrom
claude/llm-tracker-credit-tracking-6c9819
Open

ninedter wants to merge 14 commits into
mainfrom
claude/llm-tracker-credit-tracking-6c9819

Conversation

@ninedter

Copy link
Copy Markdown
Owner

What

Tracks how much add-on credit is left on both subscriptions and shows it beside the quota bars.

  • Claude card: an "Add-on credits" section with:
    • the claude.ai prepaid balance (requires a saved session key, because only claude.ai exposes it)
    • this month's extra usage: spent / monthly cap / remaining / %, or "no monthly cap", or "Extra usage is off"
    • an Out of credits pill
  • OpenAI card: the Codex credit balance from wham/usage → credits ("N credits", "Unlimited", "Credits available"), plus a Depleted pill when the workspace's credits run out or the overage limit is reached.
  • Monitor strip: one compact credit figure per provider row (hidden on phones).

Verified against the live ChatGPT response; it rendered 60,740.28 credits. The Claude field shapes (cents, currency) come from the open-source Codex CLI and several community trackers. The parser treats every field as optional and accepts amounts sent as numeric strings.

Hardening found along the way

  • Parsing: moved into pure, tested modules (claude-parse.ts, openai-parse.ts). Clients only fetch now. The credit endpoints are best-effort and can never fail the quota card.
  • Live payload bug: rate_limit_reached_type is an object in the live payload, so the old code would have thrown. Fixed and tested.
  • Claude with only Claude Code's OAuth token now works. Before, the routes required a session key. An expired Keychain token now reports AUTH_EXPIRED ("run claude to refresh"), not "not configured".
  • Shared usage service: usage-service.ts gives typed results: MISSING_CREDENTIALS 401, AUTH_EXPIRED 401, FETCH_ERROR 502. The routes and /api/health are thin wrappers, and health entries are now {configured, connected, error?, code?}.
  • Card behaviour on errors: cards are gated on "configured", not "upstream succeeded". On error they show the real message beside the last good data, and the badge reads "Error". They keep polling: a retry is scheduled even for missing credentials, backoff is capped at the 60 s poll, and only one retry is pending per key.
  • Refresh button: now bypasses the 30 s server cache with ?fresh=1.
  • Fetching:
    • 10 s timeouts on every upstream fetch.
    • User-Agent overrides via CLAUDE_CODE_USER_AGENT and CLAUDE_WEB_USER_AGENT.
    • The Keychain probe is async execFile (no shell), shares one in-flight call, and honours expiresAt.
  • Credentials: the decrypted credential file is memoized in-process.
  • OpenAI per-feature limits: the secondary window is no longer dropped.
  • Layout: page headers wrap at phone width.
  • CI: adds tsc --noEmit, the edge tests and npm run build. The test:edge script was fixed because it failed on Node 22.
  • Dependencies: Next and eslint-config-next 16.1.6 → 16.4.2; @types/node ^22.

Verification

  • npx vitest run: 295 passed. tsc --noEmit and lint are clean, npm run test:edge 84/84, and npm run build succeeds.
  • Checked in the browser on the dev server: dashboard, Monitor strip and Settings at desktop width and 375 px. The dashboard and Monitor have no horizontal overflow.

Reviewer notes

  • The upstream endpoints are undocumented, the same ones the official apps use.
  • Follow-ups split into separate sessions:
    • retire the Electron tooling and bump Vitest
    • Settings overflow at 375 px
    • warn when a saved session key has expired but OAuth still works
    • the Monitor strip keeping stale numbers after credentials are deleted
    • confirming the Claude credit shapes against a live response
  • Design and plan: docs/superpowers/specs/2026-10-20-addon-credit-tracking-design.md and docs/superpowers/plans/2026-10-20-addon-credit-tracking.md.

🤖 Generated with Claude Code

ninedter and others added 13 commits October 20, 2026 13:13
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move usage parsing out of ClaudeClient into a pure claude-parse module and
add extra-usage / prepaid-balance parsing (cents -> major units). Legacy
session/weekly fields now apply only when the new utilization is absent, so
a real 0% is no longer overridden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…points

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…resh

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cards render a shared "Add-on credits" section (Claude prepaid balance and
extra usage, OpenAI credit balance) and the Monitor strip shows a one-line
credit summary. Usage hooks no longer gate on /api/health: the routes report
MISSING_CREDENTIALS themselves, which drives the setup placeholder and skips
SWR retries. Refresh bypasses the server cache via ?fresh=1, and Settings
distinguishes configured-but-failing from not configured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rage flag

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also fixes the test:edge script (node --test with a directory path fails on Node 22).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ssing

The Keychain probe now distinguishes absent from expired
(readClaudeCodeOAuthState). With no session key, an expired token yields
401 AUTH_EXPIRED ("Claude Code sign-in expired. Run `claude` ..."), so
health stays configured and the card keeps its last data. An expired
result caches for 60 s instead of 5 min, and a cached valid token never
outlives its own expiresAt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SWR suppresses refreshInterval while an error is cached, so onErrorRetry
now drives polling: MISSING_CREDENTIALS rechecks every usage interval,
other errors use SWR's backoff capped at that interval (usageRetryDelay).
Cards no longer flash the skeleton during error retries, drop the
"Connected" badge while erroring, and show the setup placeholder on
MISSING_CREDENTIALS even over stale data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude parser accepts finite numeric strings for monthly_limit,
used_credits, utilization and prepaid amount. OpenAI "N credits" only
shows when hasCredits is true (unlimited still wins). Settings explains
when the session key is optional; README clarifies the "Out of credits"
pill, documents the expired Claude Code sign-in, and bumps the test count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SWR starts a new retry chain for each failed non-deduped request, so a
focus revalidation or Refresh during an outage stacked another never-ending
<=60 s chain. scheduleRetry() cancels any pending retry for the key before
scheduling the next. README: expired sign-in is picked up "within a minute
or two"; test count 295.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ConnectionBadge only knew connected:boolean, so a card showing a real
error (e.g. "Claude Code sign-in expired") still read "Not configured".
It now takes a status of connected | error | unconfigured, and
ProviderCard derives it from connected and error.

Page headers on the dashboard, monitor and settings pages used a
non-wrapping flex row, so the title block and nav buttons overflowed
at 375px. They now wrap, matching the analytics header.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-20T06:31:02.314115Z b0e4d03 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

next build typechecks every .ts in the context, and
src/app/api/__tests__/edge-contract.test.ts imports edge/, which
.dockerignore already excludes — so docker build failed with TS2307.
Tests never run in the image, so exclude them too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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