Claude multi-account web/sessionKey: per-account snapshots converge to first account's data after initial fetch
Summary
With Claude cookieSource = manual and two tokenAccounts entries (both sessionKey cookies, one per Anthropic account) and showAllTokenAccountsInMenu = true, the menu correctly shows distinct usage for each account on the first fetch round after a clean app launch, but subsequent refresh rounds collapse both accounts onto the data of the first/active account. Identity (email, plan) also degrades to the configured label and a default plan.
Similar in effect to #403 ("Claude --source cli all-accounts reports incorrect per-account usage"), but reproduces on the web/sessionKey path, not CLI.
Environment
- CodexBar 0.23 (build 58), macOS 26.4.1, Apple Silicon.
- Two
claudetoken accounts in~/.codexbar/config.json, both 131-charsk-ant-sid02-…sessionKeys from claude.ai (different Anthropic accounts: one Team, one Max). cookieSource: "manual",activeIndex: 0,showAllTokenAccountsInMenu = true.
Steps to reproduce
defaults write com.steipete.codexbar showAllTokenAccountsInMenu -bool true- Configure two Claude
tokenAccountswith sessionKeys for two distinct Anthropic accounts that have different real usage. - Set Claude
cookieSource: "manual". - Quit CodexBar; remove
~/Library/HTTPStorages/com.steipete.codexbar*for a clean baseline. - Launch CodexBar, open the menu within ~10s.
- Wait for the next refresh round (≥1 min) and reopen the menu.
Observed
- First fetch (clean state): both cards show distinct, correct data — work shows email A, Team-tier numbers; personal shows email B, Max-tier numbers.
- Subsequent refresh: both cards show the same numbers (those of the first/active account); the email is replaced by the
labelfor both; both are shown as "Max" plan. Switching the active tab in the UI does not bring back the per-account distinction.
Expected
Each tokenAccount should fetch independently and persist independent snapshots across refreshes; the second card should remain pinned to its own sessionKey's data.
Suspected area
Both Claude web requests go through URLSession.shared (Sources/CodexBarCore/Providers/Claude/ClaudeWeb/ClaudeWebAPIFetcher.swift). All sites set the Cookie: sessionKey=… header explicitly via setValue(forHTTPHeaderField:), but URLRequest.httpShouldHandleCookies is not set to false. With the default cookie-handling policy, the shared HTTPCookieStorage accumulates server-set cookies (e.g. __cf_bm, plus any auth/state cookies claude.ai returns) across requests, so the second account's request is sent with the first account's accumulated cookies in addition to the explicit sessionKey. This is consistent with the observed convergence on subsequent rounds (the storage is empty on first launch, populated thereafter).
A minimal mitigation worth trying: request.httpShouldHandleCookies = false for every request in ClaudeWebAPIFetcher.swift. A stronger fix would be a per-tokenAccount URLSession with its own ephemeral HTTPCookieStorage.
Workaround
Setting showAllTokenAccountsInMenu = false keeps usage correct because only the active account is fetched per round; users switch via "Switch Account…".
Related
- #403 — same convergence symptom on the CLI source.
- #567 (closed) — related identity issues in multi-account.
Update on root cause — earlier suspicion was wrong, real cause is linked Anthropic emails
After more digging on the same setup, the URLSession.shared cookie-jar hypothesis from the original report turned out to be wrong. The actual root cause in this configuration is linked Anthropic email aliases:
- Two emails (
work@…Team-org +personal@…Max-org) are linked to a single Anthropic user account. - A
sessionKeyextracted from claude.ai under either email is functionally the same Anthropic user session. GET /api/organizationsreturns the same list of organizations for either sessionKey:[Personal-org, Team-org].ClaudeWebAPIFetcher.organization(...)selectsorganizations.first(where: { $0.hasChatCapability }), which deterministically returns the Personal-org for this list.GET /api/organizations/{personal-uuid}/usagealways returns Personal-org usage, regardless of which "tokenAccount" was used.
So the visible symptom — "two distinct accounts collapse onto the data of the first/active one after the initial fetch round" — is not a race condition. It's that both sessionKeys are functionally equivalent for a linked-email user, and CodexBar's first-with-chat-capability heuristic always picks the same org. The first fetch round just briefly showed plausible-looking distinct data because identity (/api/account email + plan) was momentarily attributed per tokenAccount.label; subsequent rounds reveal that the underlying usage numbers were the same Personal-org numbers all along.
Confirmed by direct API probes:
curl -H "Cookie: sessionKey=<work-extracted>" /api/organizations/{personal_uuid}/usage → Personal usage
curl -H "Cookie: sessionKey=<personal-extracted>" /api/organizations/{personal_uuid}/usage → Personal usage (same)
curl -H "Authorization: Bearer <CLI sk-ant-oat>" /api/oauth/usage → Team usage (correct)
The OAuth path (ClaudeOAuthUsageFetcher → /api/oauth/usage) returns the correct Team-org data, because the OAuth token is bound to the org by its user:sessions:claude_code scope; the API doesn't need an org_id from the client.
Practical workaround for users in this situation
Use an OAuth tokenAccount (the sk-ant-oat... access token from ~/.claude/.credentials.json) for the org you want to monitor. The Web/sessionKey path can't reach the secondary org for a linked-email user.
Suggested fixes
- Document explicitly: linked Anthropic emails are a single user account; web sessionKeys cannot scope to the non-default org.
- Allow
tokenAccounts[].organizationId(ortargetOrganization) to be set per-account, so the web path can useorganizations.first(where: { $0.uuid == configured })instead of the chat-capability heuristic. This would unblock multi-org monitoring for linked-email users via web cookies. - Optionally, expose an org selector in the UI when
/api/organizationsreturns more than one org for a giventokenAccount.
The "URLSession.shared cookie-jar" suggestion in my original report can be ignored — clearing the HTTP storage and disabling cookie handling didn't change anything. Apologies for the noise.
Fixed on main in 23e4b69a.
The updated diagnosis in this issue is the right one: linked Anthropic emails can return the same session/org list, so a web sessionKey alone cannot imply which org CodexBar should pick. The fix is explicit org targeting for Claude web token accounts.
Config shape:
{
"label": "Team",
"token": "sk-ant-sid02-...",
"organizationId": "org-team-uuid"
}
When organizationId is present, the Claude web path uses that org for /api/organizations/{org}/usage and also selects the matching membership for identity/plan display. This is wired through both the app menu refresh path and the CLI token-account path.
Validated with:
swift test --filter ClaudeUsageTestsswift test --filter TokenAccountEnvironmentPrecedenceTestsswiftlint lint --strict --quiet ...env CODEXBAR_WIDGET_METADATA_TIMEOUT_SECONDS=5 ./Scripts/compile_and_run.sh