agentleFS
Fei2-LabsFei2-Labs/skill-genie/skills/claude-agent-sdk-auth/SKILL.md

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

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
  1. Very specific:

    --bare mode does NOT read CLAUDE_CODE_OAUTH_TOKEN: If a script uses claude --bare, authenticate with ANTHROPIC_API_KEY or an …

    line 46
  2. Distinctive:

    Do not log it; treat it as a credential (store mode 0600, inject via env only).

    line 47
  3. Distinctive:

    "Use the SDK itself as the LLM server": does not exist — see the top section.

    line 53

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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-json is newline-delimited JSON for real-time streaming; pair with --verbose).
  • SDK: TypeScript @anthropic-ai/claude-agent-sdk query() async generator; Python claude-agent-sdk query() / ClaudeSDKClient (long-running sessions).

Authentication precedence (what claude checks, first match wins)

  1. Cloud provider env (CLAUDE_CODE_USE_BEDROCK, …)
  2. ANTHROPIC_AUTH_TOKEN — bearer token, sent as Authorization: Bearer <token>; the intended slot for an LLM gateway / proxy.
  3. ANTHROPIC_API_KEY — Console API key (metered).
  4. apiKeyHelper — a script whose stdout is the credential (dynamic).
  5. CLAUDE_CODE_OAUTH_TOKEN — a long-lived subscription OAuth token (see setup-token).
  6. Subscription OAuth from /login — the interactive Claude Code login. Stored on macOS in the Keychain, on Linux in ~/.claude/.credentials.json (mode 0600) as claudeAiOauth: { 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 subscription

This 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-token to renew. Cannot establish Remote Control sessions.
  • --bare mode does NOT read CLAUDE_CODE_OAUTH_TOKEN. If a script uses claude --bare, authenticate with ANTHROPIC_API_KEY or an apiKeyHelper instead. (Plain claude -p and 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-token once → set CLAUDE_CODE_OAUTH_TOKEN on the remote. Simplest, no proxy, origin machine can be offline. Default choice.
  • You need an actual HTTP endpoint to point ANTHROPIC_BASE_URL at (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/messages to api.anthropic.com and attaches the subscription OAuth as Authorization: Bearer <accessToken> plus anthropic-beta: oauth-2025-04-20 (subscription OAuth requires that beta header on /v1/messages; it is NOT x-api-key). The remote then uses ANTHROPIC_AUTH_TOKEN=<a gate token> + ANTHROPIC_BASE_URL=<proxy>. This keeps the OAuth on one box but requires that box online. Prefer setup-token unless 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"

  1. Is X another machine / CI / a spawned agent that must work unattended? → claude setup-token → CLAUDE_CODE_OAUTH_TOKEN env. Done.
  2. Does X call claude --bare? → that flag ignores the OAuth token; use an API key or apiKeyHelper.
  3. Is a stale ANTHROPIC_API_KEY set in the env? → unset it, or it shadows the subscription.
  4. Need a real Anthropic-compatible HTTP endpoint? → build/point a proxy (Bearer + anthropic-beta: oauth-2025-04-20); the SDK is not that endpoint.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.