CodexBar icon indicating copy to clipboard operation
CodexBar copied to clipboard

Claude --source cli all-accounts reports incorrect per-account usage

Open 0xble opened this issue 9 months ago • 4 comments

Summary

codexbar usage --provider claude --source cli --all-accounts can return incorrect per-account usage in a multi-account Claude setup.

In my environment, CLI source reports identical exhausted values for multiple accounts, while OAuth source (and direct OAuth usage checks) show different usage per account as expected.

Environment

  • CodexBar CLI version: 2.1.47
  • OS: macOS (Apple Silicon)
  • Claude setup: multiple token accounts configured in CodexBar
  • Accounts intentionally have different real usage states

Expected behavior

--source cli should return usage scoped to each requested Claude account (or match OAuth source results for the same account).

Actual behavior

--source cli --all-accounts reports duplicated / incorrect usage across accounts.

Example symptom:

  • <ACCOUNT_A> truly exhausted weekly (100%)
  • <ACCOUNT_B> has remaining weekly quota (e.g., ~29% left / ~71% used)
  • --source cli reports both as exhausted

Reproduction steps

  1. Configure at least two Claude accounts in CodexBar tokenAccounts.
  2. Ensure accounts have different usage.
  3. Run:
codexbar usage --provider claude --source oauth --json --all-accounts --log-level error
codexbar usage --provider claude --source cli   --json --all-accounts --log-level error
  1. Compare outputs per account.

Example output (sanitized)

OAuth source (correct / distinct per account)

[
  {
    "account": "<ACCOUNT_A>",
    "source": "oauth",
    "usage": { "primary": { "usedPercent": 0 }, "secondary": { "usedPercent": 100 } }
  },
  {
    "account": "<ACCOUNT_B>",
    "source": "oauth",
    "usage": { "primary": { "usedPercent": 1 }, "secondary": { "usedPercent": 71 } }
  }
]

CLI source (incorrect / duplicated)

[
  {
    "account": "<ACCOUNT_A>",
    "source": "claude",
    "usage": { "primary": { "usedPercent": 0 }, "secondary": { "usedPercent": 100 } }
  },
  {
    "account": "<ACCOUNT_B>",
    "source": "claude",
    "usage": { "primary": { "usedPercent": 0 }, "secondary": { "usedPercent": 100 } }
  }
]

Additional diagnostics

I verified credentials are synchronized across:

  • CodexBar account config
  • Claude profile credential files
  • Shared secret export used by related tools

I also tested Claude CLI account scoping behavior:

  • CLAUDE_CODE_PROFILE=<PROFILE> claude -p ... did not reliably switch usage context in this setup.
  • CLAUDE_CONFIG_DIR=<PROFILE_CONFIG_DIR> claude -p ... did switch correctly.

This suggests CLI probing may not be binding the account context robustly per account.

Suspected root cause

In Claude source=cli probing, account context is not explicitly pinned per account (e.g., missing config-dir scoping), so usage can be read from the wrong active context and then attributed to multiple accounts.

Suggested fix

One of these approaches should resolve it:

  1. For Claude CLI probing, set explicit account config context per probe (for example via per-account config directory).
  2. Internally route Claude CLI-source usage to OAuth-source implementation when OAuth tokens are available.
  3. Add a regression test with two accounts returning intentionally different usage and assert distinct per-account output for --all-accounts.

Acceptance criteria

  • With at least two accounts having different usage, --source cli --all-accounts returns correct per-account values (not duplicated).
  • Results are consistent with --source oauth --all-accounts within normal freshness windows.

Impact

This can cause automated account selection to pick an exhausted account even when another account has available quota, creating avoidable failures/retries.

Current workaround

Use OAuth source for Claude usage checks:

codexbar usage --provider claude --source oauth ...

instead of:

codexbar usage --provider claude --source cli ...

Related

Possibly related to #215 (usage mismatch report), but this issue is specifically about multi-account --source cli behavior.

0xble avatar Feb 19 '26 15:02 0xble

Follow-up repro that may help narrow root cause:

I tested switching the Claude activeIndex in ~/.codexbar/config.json and observed the app-level snapshot for Claude flips entirely to whichever account is active.

Repro

  1. Set Claude activeIndex=0 in ~/.codexbar/config.json
  2. Restart CodexBar
  3. Inspect ~/Library/Group Containers/group.com.steipete.codexbar/widget-snapshot.json
  4. Note Claude entry values (example from my run):
    • secondary.usedPercent = 100
    • primary.usedPercent = 0
  5. Set Claude activeIndex=1
  6. Restart CodexBar
  7. Inspect same snapshot file again
  8. Claude entry now flips (example from my run):
    • secondary.usedPercent = 73
    • primary.usedPercent = 16

Then switching back to activeIndex=0 flips back to the first values.

Interpretation

This suggests the app snapshot/state for Claude may be sourced from the currently active account and then reused broadly, instead of maintaining independent per-account snapshot values for multi-account display/probing.

This aligns with the --source cli --all-accounts behavior in the issue body where values can appear duplicated across accounts.

Happy to run any debug build / extra probes if useful.

0xble avatar Feb 19 '26 15:02 0xble

I'm going to take a look at this on current main and see whether I can reproduce it cleanly and cut a small fix from there.

TheAngryPit avatar May 13 '26 09:05 TheAngryPit

I took a read-only pass through current main, and I think this is a real issue.

The likely problem seems to be that --source cli --all-accounts does iterate over Claude token accounts, but the CLI path does not appear to bind each fetch to a distinct Claude CLI account context. So multiple accounts can end up reading the same active CLI context and then get relabeled as separate per-account results.

From what I can see, there are probably two reasonable ways to fix it:

  1. A small / safe fix: make this path honest and deterministic by avoiding per-account CLI results when the account is not actually CLI-scoped (for example, falling back to OAuth for OAuth-backed Claude accounts, or failing explicitly for unsupported multi-account CLI cases), plus a regression test.

  2. A larger fix: add true per-account CLI scoping for Claude (for example via config-dir based context binding), then keep --source cli --all-accounts fully per-account.

My instinct would be to start with (1) as the smaller current-main fix unless there is a strong preference for going straight to (2). Happy to put together that smaller PR if that direction makes sense.

TheAngryPit avatar May 13 '26 09:05 TheAngryPit

Fixed on main in 29386fe7.

Proof:

  • Added regression coverage for Claude --all-accounts with explicit --source cli over OAuth and session token accounts.
  • Verified selected-account rerouting uses OAuth for bearer accounts and web/session cookie routing for session-key accounts, so ambient CLI usage is not relabeled.
  • Ran swift test and the focused token-account shard.

Closing as fixed for the next release.

steipete avatar May 14 '26 19:05 steipete