feat(core): add OAuth support to the built-in Anthropic transformer#1408
Open
neatshell wants to merge 2 commits into
Open
feat(core): add OAuth support to the built-in Anthropic transformer#1408neatshell wants to merge 2 commits into
neatshell wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds first-class OAuth (Claude Code subscription) support to the built-in
Anthropictransformer in@CCR/core, so users with a Claude Code logincan route requests through CCR to
api.anthropic.comby toggling a singleoption on the built-in transformer:
What changed
packages/core/src/transformer/anthropic.transformer.tsOAuthoption on theAnthropictransformer.CCR_OAUTH_TOKENenv var~/.claude/.credentials.json(Linux / dev installs)Claude Code-credentials(viasecurity find-generic-password)api_key(fallback for explicit overrides)security/ re-reading the credentials file on every request.Authorization: Bearer <token>, dropsx-api-key, and appendsoauth-2025-04-20to theanthropic-betaheader while preserving anyexisting beta flags from the upstream request.
OAuth endpoint (
model,messages,system,max_tokens,metadata,stop_sequences,stream,temperature,top_p,top_k,tools,tool_choice,thinking) and strips non-typekeys from any nestedcache_controlobjects, which the OAuth endpoint rejects.packages/core/src/services/provider.tstransformer, instantiate a fresh one from its constructor using the
provider's options. This is what makes
["Anthropic", { "OAuth": true }]actually passOAuth: truethroughinstead of silently reusing the default-constructed singleton.
packages/core/src/api/routes.tsinstance over the global one when calling
auth(), so per-provideroptions (like
OAuth: true) are honored. Also forwards the currentrequest headers as
TransformerContextso the transformer can mergeanthropic-betacorrectly.packages/core/{package.json,vitest.config.ts}+packages/core/src/transformer/__tests__/anthropic.transformer.test.tsvitest+vite-tsconfig-pathsas dev deps and annpm test/npm run test:watchscript.rewriting, field/
cache_controlsanitization, and the non-OAuthUseBearer/ defaultx-api-keypaths.Compatibility
OAuth: truethe transformerbehaves exactly as before (
x-api-key, or Bearer whenUseBearer: true).fs/child_process).How to use it
Edit
~/.claude-code-router/config.jsonand add a provider that uses thebuilt-in
Anthropictransformer withOAuth: true:{ ..., "Providers": [ { "name": "AnthropicOAuth", "api_base_url": "https://api.anthropic.com/v1/messages", "api_key": "unused", "models": [ "claude-opus-4-7", "claude-sonnet-4-6", "claude-haiku-4-5-20251001" ], "transformer": { "use": [["Anthropic", { "OAuth": true }]] } } ], "Router": { "default": "AnthropicOAuth,claude-sonnet-4-6", "background": "AnthropicOAuth,claude-haiku-4-5-20251001", "think": "AnthropicOAuth,claude-opus-4-7", "longContext": "AnthropicOAuth,claude-sonnet-4-6", "webSearch": "AnthropicOAuth,claude-sonnet-4-6", "longContextThreshold": 60000 }, ... }Notes:
api_keyis required by the schema but ignored whenOAuth: trueanda token is found via env / credentials file / Keychain; set it to any
placeholder (e.g.
"unused").api_base_urlshould point at the real Anthropic endpoint(
https://api.anthropic.com/v1/messages).Claude Code-credentialsthat the Claude Code CLI creates on login.On Linux / WSL, it's read from
~/.claude/.credentials.json.CCR_OAUTH_TOKENbefore starting CCR.After editing the file, restart the router:
Test plan
pnpm --filter @CCR/core test— new vitest suite passes.ccr codeagainstclaude-haiku-4-5-20251001— non-streaming,streaming (SSE), tool use, and
cache_controlsanitization (requestwith
cache_control: { type: "ephemeral", foo: "bar" }and an unknowntop-level field returns 200; without the sanitizer the OAuth endpoint
400s on the extra field).
claude-sonnet-4-6.claude-opus-4-7.x-api-key) stillworks unchanged with the same build.