Skip to content

Ask Claude Code for the account and usage instead of reading its login - #7077

Merged
ymichael merged 1 commit into
mainfrom
bb/3607-claude-usage-check-ignores-claude-config-di-thr_4yj34xabd9
Nov 6, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/3607-claude-usage-check-ignores-claude-config-di-thr_4yj34xabd9

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The Claude Code plugin's health and usage checks read Claude's login themselves: ~/.claude/.credentials.json, ~/.claude.json and the default Claude Code-credentials Keychain item. Usage then called api.anthropic.com/api/oauth/usage with that token. When CLAUDE_CONFIG_DIR is set, Claude Code signs in from a different profile, so bb reported a different account than its sessions use. A stale token in the default profile could show as expired for an account that was signed in and working (#3607).

Fixing the paths would mean copying Claude Code's private rules for its folder, Keychain name and account file. The installed 2.1.317 binary showed several edge cases those rules miss: a legacy .config.json, CLAUDE_SECURESTORAGE_CONFIG_DIR, relative paths and an empty variable. Separately, fork resolved a relative CLAUDE_CONFIG_DIR against the bridge process's directory instead of the session's cwd, which is where Claude Code reads it.

What changed

Everything is inside plugins/provider-claude-code. There is no daemon protocol change, no plugin SDK change and no dependency change: main is already on @anthropic-ai/claude-agent-sdk 0.3.318 (#7133).

  • initialization-probe.ts: the probe that model and command listing already use now has a second entry point, probeClaudeUsage. It starts Claude Code with no prompt and sends the SDK usage request with skipBehaviors: true, which skips Claude's scan of local transcripts.
  • Health reads the account Claude Code reports at startup (initializationResult().account). It is signed out only when Claude reports tokenSource: "none" with no API key and a first-party provider. The plan label comes from Claude's own plan name, for example "Claude Ultra" → Ultra.
  • Usage reads the limits Claude Code returns and keeps the existing window normalization. It reports unauthenticated when Claude has no usable login or plan limits don't apply, which matches today's result for API-key logins.
  • Removed: the Keychain and .credentials.json reads, the direct oauth/usage call, and its claude-code/2.1.0 User-Agent. This covers the Claude rows of Use regular Claude and Codex interfaces for status and usage, and identify bb as its own client #6688's table.
  • Kept: accountKey still reads the account UUID from the profile's .claude.json, at $CLAUDE_CONFIG_DIR or ~ (Claude Code's rule). Claude Code doesn't report the UUID anywhere else, and anthropic:account:<uuid> has to match the Account Pooler's key so the same account dedupes.
  • Fork: claudeConfigDirectory resolves CLAUDE_CONFIG_DIR against the session's cwd and treats an empty value as unset. Fork keeps this local rule rather than asking claude auth status, because that would break users who run only the SDK's bundled binary.

Behaviour changes:

  • expired is no longer reported. Claude Code refreshes access tokens itself, and the old check called a token expired once its timestamp passed, even with a valid refresh token. A login Claude Code can't use now shows as unauthenticated, and the sign-in action is the same.
  • Health and usage each start a short Claude process: about 0.3 s for health and 0.5 s for usage on this machine, against roughly 0.3 s for the old direct fetch. The server already caches usage for 10 s.

How you verified

  • Live, on macOS with Claude Code 2.1.317, calling the real getClaudeProviderHealth and getClaudeProviderUsage:

    Profile main this PR
    default (signed in) ready / ok, Ultra ready / ok, Ultra, same accountKey
    CLAUDE_CONFIG_DIR = empty profile — unauthenticated / unauthenticated
    CLAUDE_CONFIG_DIR = profile with an invalid token ready / ok for the default account unauthenticated / unauthenticated

    The last row is Claude usage check ignores CLAUDE_CONFIG_DIR, reporting expired for a signed-in account #3607's mismatch: main reports the default profile's account while Claude Code itself has no usable login in that profile.

  • New provider-maintenance.account.test.ts: usage reports the account and plan Claude Code returns, and keys it from $CLAUDE_CONFIG_DIR/.claude.json. Usage is signed out for a login Claude can't use and for logins without plan limits. Health is ready or signed out from the same account. It fails on main.

  • New claudeConfigDirectory cases in session-fork.test.ts: a relative path resolves against the session cwd, and unset or empty means ~/.claude.

  • pnpm exec turbo run typecheck --filter=bb-plugin-provider-claude-code passes, and oxlint is clean. Targeted vitest (provider-maintenance*, session-fork, model-list, provider-commands) passes 26 of 26. CI runs the full suites.

Fixes #3607
Part of #6688

🤖 Generated with Claude Code

AGENT GENERATED

Health and usage read ~/.claude/.credentials.json, ~/.claude.json and the
default Keychain item, then called Anthropic's OAuth usage endpoint with
that token. With CLAUDE_CONFIG_DIR set, Claude Code signs in from a
different profile, so bb reported the wrong account, or expired for one
that was signed in.

Both now start Claude Code through the Agent SDK with no prompt, as model
listing already does, and read the account it reports. Usage comes from
the SDK usage request with skipBehaviors, so it skips the local
transcript scan. Only the account UUID behind accountKey is still read
from the profile's .claude.json, because Claude Code reports it nowhere
else.

Fork resolved a relative CLAUDE_CONFIG_DIR against the bridge process
instead of the session's cwd, where Claude Code reads it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/3607-claude-usage-check-ignores-claude-config-di-thr_4yj34xabd9 branch from 867c68f to 5ff80e9 Compare November 6, 2026 05:05
@ymichael ymichael changed the title Read the Claude profile CLAUDE_CONFIG_DIR selects for usage, health and fork Ask Claude Code for the account and usage instead of reading its login Nov 6, 2026
@ymichael
ymichael merged commit f51e6b0 into main Nov 6, 2026
36 checks passed
@ymichael
ymichael deleted the bb/3607-claude-usage-check-ignores-claude-config-di-thr_4yj34xabd9 branch November 6, 2026 05:17
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.

Claude usage check ignores CLAUDE_CONFIG_DIR, reporting expired for a signed-in account

1 participant