Skip to content

feat: add Anthropic (Claude Code) quota provider#30

Merged
slkiser merged 2 commits intoslkiser:mainfrom
diogoleitao:feature/anthropic-claude-quota
Mar 25, 2026
Merged

feat: add Anthropic (Claude Code) quota provider#30
slkiser merged 2 commits intoslkiser:mainfrom
diogoleitao:feature/anthropic-claude-quota

Conversation

@diogoleitao
Copy link
Copy Markdown
Contributor

Refs #29

Summary

  • Adds src/lib/anthropic.ts: credential loading, token refresh via claude CLI, and quota fetch from GET https://api.anthropic.com/api/oauth/usage
  • Adds src/providers/anthropic.ts: normalizes 5-hour and 7-day windows into QuotaToastEntry for both classic and grouped toast styles
  • Updates src/providers/registry.ts: registers anthropicProvider first in the list
  • Updates tests/quota-provider-boundary.test.ts: adds anthropic to the registry snapshot
  • Adds tests/lib.anthropic.test.ts: 23 tests covering credential resolution, isNearExpiry, parseUsageResponse, and queryAnthropicQuota (fetch success, 401/403, network error, bad JSON, unexpected shape)
  • Adds tests/providers.anthropic.test.ts: 9 tests covering classic/grouped toast entries, worst-window selection, error mapping, matchesCurrentModel, and isAvailable
  • Updates README.md: adds Anthropic to the provider list, setup table, and provider-specific notes with attribution

Behaviour

  • Provider id: anthropic
  • Auto-detected when OpenCode is configured with the anthropic provider
  • Credential resolution order: ~/.claude/.credentials.json → macOS Keychain (Claude Code-credentials) → CLAUDE_CODE_OAUTH_TOKEN env var
  • Token near expiry (within 5 min): invokes claude CLI for silent refresh — same pattern as opencode-claude-auth
  • Classic style: shows the worse of the two windows
  • Grouped style: shows both Claude 5h and Claude 7d entries
  • Graceful errors on missing credentials, 401/403, network failure

Checks

  • npm run typecheck
  • npm test ✓ (272 tests pass)
  • npm run build
  • Tested with OpenCode 1.3.2

Attribution

Credential loading and API endpoint approach based on prior art from claude-lens (Astro-Han) and ClaudeBar (tddworks).

diogoleitao and others added 2 commits March 25, 2026 14:22
Adds a new 'anthropic' provider that fetches the 5-hour and 7-day
rate-limit windows from the Anthropic OAuth usage API using Claude Code
credentials (file, macOS Keychain, or env var). Token refresh is
delegated to the claude CLI when near expiry.

Credential loading approach based on prior art from claude-lens and
ClaudeBar. Refs slkiser#29.
@slkiser
Copy link
Copy Markdown
Owner

slkiser commented Mar 25, 2026

@diogoleitao I made some changes, could you test locally, since I don't have an anthropic subscription?

I took out the MacOS keychain only because I remember seeing some future feature like this being added to Opencode on a dev branch.

Thanks!

@slkiser slkiser merged commit 4c1d3f7 into slkiser:main Mar 25, 2026
3 checks passed
@diogoleitao diogoleitao deleted the feature/anthropic-claude-quota branch March 25, 2026 18:12
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.

2 participants