claude-agent-sdk-auth
Skill12 starsChanged 49 days agoMITView on GitHub
Run Claude Code / the Claude Agent SDK headless or programmatically, and authenticate it with the user's Claude Pro/Max SUBSCRIPTION (no API key) — including on a REMOTE server or in CI. Use when: running `claude -p` / the Agent SDK (`@anthropic-ai/claude-agent-sdk`, `claude-agent-sdk`) non-interactively; making a remote/CI machine or a spawned agent use the local subscription instead of a metered API key; deciding between API key, subscription OAuth, `CLAUDE_CODE_OAUTH_TOKEN`, and a proxy; or wiring `CLAUDE_CODE_OAUTH_TOKEN` / `ANTHROPIC_AUTH_TOKEN` / `apiKeyHelper`. Covers `claude setup-token`, the auth precedence order, headless entry points, and the key fact that the Agent SDK is NOT an HTTP server.
- Reads credentials
In this repository37 other files it gives its agents.
- 3mf-print-editorSkill
- aegis-protocolSkill
- ai-address-parser-debugSkill
- ai-address-parserSkill
- ai-csuiteSkill
- browser-driverSkill
- chatgpt-web-delegationSkill
- chinese-humanizerSkill
- close-loopSkill
- de-ai-englishSkill
- dev-inboxSkill
- dokploy-deploySkill
- dokploy-upgradeSkill
- ecommerce-golive-checkSkill
- fork-upstream-syncSkill
- foxcode-openclawSkill
- git-cryptSkill
- git-worktree-decisionSkill
- handoff-receiverSkill
- image-prompt-engineerSkill
- init-rulesSkill
- landing-launch-checkSkill
- linuxdo-applicationSkill
- npm-publishSkill
- omnidebug-autopilotSkill
- psychology-masterSkill
- refactor-agents-mdSkill
- research-to-wechatSkill
- right-sized-solutionSkill
- rtk-token-saverSkill
- session-handoffSkill
- shared-memorySkill
- stalwart-dokploy-resend-relaySkill
- typed-decisions-around-llmsSkill
- web-compliance-builderSkill
- wechat-compliance-checkSkill
- wordpress-vps-installSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 46
--baremode does NOT readCLAUDE_CODE_OAUTH_TOKEN: If a script uses claude --bare, authenticate with ANTHROPIC_API_KEY or an … - Distinctive: line 47
Do not log it; treat it as a credential (store mode 0600, inject via env only).
- Distinctive: line 53
"Use the SDK itself as the LLM server": does not exist — see the top section.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Claude Agent SDK / Claude Code — headless auth with a subscription
Goal: run Claude Code (or the Claude Agent SDK) non-interactively, authenticated by the user's Claude Pro/Max subscription rather than a metered Anthropic API key — locally, in CI, on a remote 24/7 server, or inside a spawned agent. Verify any binding against the docs in the frontmatter references before writing code; do not guess flags.
The one fact that kills the wrong design first
The Claude Agent SDK / Claude Code is NOT an HTTP server. It is a client/agent runtime: the SDK spawns a local claude CLI subprocess over stdin/stdout, and that subprocess calls api.anthropic.com directly. It does not listen on a network port. So you cannot point another process's ANTHROPIC_BASE_URL at "the SDK" — there is nothing serving an Anthropic-API-compatible endpoint. If you need an HTTP endpoint, you build your own wrapper around query() (your code listens, calls the SDK internally), or you use a token (below) instead. Source: Agent SDK Hosting docs.
Headless / programmatic entry points
- CLI:
claude -p "<prompt>"(a.k.a.--print) → non-interactive, result to stdout. - Output:
--output-format text|json|stream-json(stream-jsonis newline-delimited JSON for real-time streaming; pair with--verbose). - SDK: TypeScript
@anthropic-ai/claude-agent-sdkquery()async generator; Pythonclaude-agent-sdkquery()/ClaudeSDKClient(long-running sessions).
Authentication precedence (what claude checks, first match wins)
- Cloud provider env (
CLAUDE_CODE_USE_BEDROCK, …) ANTHROPIC_AUTH_TOKEN— bearer token, sent asAuthorization: Bearer <token>; the intended slot for an LLM gateway / proxy.ANTHROPIC_API_KEY— Console API key (metered).apiKeyHelper— a script whose stdout is the credential (dynamic).CLAUDE_CODE_OAUTH_TOKEN— a long-lived subscription OAuth token (seesetup-token).- Subscription OAuth from
/login— the interactive Claude Code login. Stored on macOS in the Keychain, on Linux in~/.claude/.credentials.json(mode 0600) asclaudeAiOauth: { accessToken, refreshToken, expiresAt, scopes, subscriptionType }. Auto-refreshed by Claude Code.
A stale ANTHROPIC_API_KEY in the environment shadows the subscription (slot 3 beats slot 5/6) — unset it if you mean to use the subscription. Likewise an empty ANTHROPIC_API_KEY="" still wins its slot.
THE primitive: claude setup-token (use a subscription on a remote / in CI)
# On a machine where you're logged into Claude Code (interactive, opens a browser):
claude setup-token
# → prints a ~1-year, INFERENCE-ONLY OAuth token bound to your subscription.
# It is NOT saved anywhere; copy it.Then anywhere else (remote server, CI, a spawned agent), with no dependency on the original machine being online:
export CLAUDE_CODE_OAUTH_TOKEN=<token>
claude -p "do the task" # or drive the Agent SDK — uses the subscriptionThis is the supported, documented way to "borrow a subscription" off-box. It is genuinely independent of the origin machine (unlike any Mac-side relay), so it works for 24/7 / unattended agents.
Caveats:
- Token is inference-only and lasts ~1 year — re-run
setup-tokento renew. Cannot establish Remote Control sessions. --baremode does NOT readCLAUDE_CODE_OAUTH_TOKEN. If a script usesclaude --bare, authenticate withANTHROPIC_API_KEYor anapiKeyHelperinstead. (Plainclaude -pand the SDK read it fine.)- Do not log it; treat it as a credential (store mode 0600, inject via env only).
Decision guide
- Remote/CI agent should use the subscription, unattended:
claude setup-tokenonce → setCLAUDE_CODE_OAUTH_TOKENon the remote. Simplest, no proxy, origin machine can be offline. Default choice. - You need an actual HTTP endpoint to point
ANTHROPIC_BASE_URLat (e.g. a gateway that adds telemetry/routing, or a relay holding the subscription so the token never leaves one box): run a thin proxy that forwards/v1/messagestoapi.anthropic.comand attaches the subscription OAuth asAuthorization: Bearer <accessToken>plusanthropic-beta: oauth-2025-04-20(subscription OAuth requires that beta header on/v1/messages; it is NOTx-api-key). The remote then usesANTHROPIC_AUTH_TOKEN=<a gate token>+ANTHROPIC_BASE_URL=<proxy>. This keeps the OAuth on one box but requires that box online. Prefersetup-tokenunless you specifically need the gateway. - API-key / Console billing is fine: just set
ANTHROPIC_API_KEY— simplest, but metered, not the subscription. - "Use the SDK itself as the LLM server": does not exist — see the top section.
Reading the local subscription token directly (when not using setup-token)
~/.claude/.credentials.json → .claudeAiOauth.accessToken (+ refreshToken, expiresAt). Usable directly as a Bearer for short-lived calls, but you must implement refresh against the OAuth token endpoint when expiresAt passes — which is exactly the fragile part setup-token / Claude Code already handle. Prefer not to hand-roll refresh.
Quick checklist for "make X use the subscription headlessly"
- Is X another machine / CI / a spawned agent that must work unattended? →
claude setup-token→CLAUDE_CODE_OAUTH_TOKENenv. Done. - Does X call
claude --bare? → that flag ignores the OAuth token; use an API key orapiKeyHelper. - Is a stale
ANTHROPIC_API_KEYset in the env? → unset it, or it shadows the subscription. - Need a real Anthropic-compatible HTTP endpoint? → build/point a proxy (Bearer +
anthropic-beta: oauth-2025-04-20); the SDK is not that endpoint.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

