← ClaudeAtlas

sdk-via-openrouterlisted

Use when ANTHROPIC_API_KEY is absent and OPENROUTER_API_KEY is present. Point the Anthropic SDK at openrouter.ai/api/v1 with a provider-prefixed model id; no other code changes.
MrBinnacle/skills · ★ 0 · AI & Automation · score 65
Install: claude install-skill MrBinnacle/skills
# Anthropic SDK via OpenRouter (env-var fallback) ## Problem The Anthropic Python SDK constructor `anthropic.Anthropic()` resolves `ANTHROPIC_API_KEY` from the environment by default. When that env var is absent (e.g., on Claude Code subscription auth, on machines where the operator only has OpenRouter credentials, on CI where only an OpenRouter token is provisioned), the constructor raises before any call goes out. The naive workaround — rewriting the integration to use the OpenAI SDK against OpenRouter's chat completions endpoint — abandons the Anthropic Messages API contract (tool_use schema, system blocks, the content-block response shape, the `stop_reason` taxonomy, the cache control markers, etc.). For any non-trivial Anthropic integration, the rewrite cost is high. There is a much smaller move: keep the Anthropic SDK; just point it at OpenRouter's Anthropic-compatible endpoint via the `base_url` override. ## Solution Construct the Anthropic SDK with both `api_key` and `base_url` overrides: ```python import anthropic client = anthropic.Anthropic( api_key=os.environ["OPENROUTER_API_KEY"], base_url="https://openrouter.ai/api/v1", ) ``` Use a **provider-prefixed model id** with a DOT separator inside the model name (OpenRouter's slug convention, NOT Anthropic-direct's dash convention): ```python response = client.messages.create( model="anthropic/claude-sonnet-4.6", # dot before 6, not dash max_tokens=8192, system=SYSTEM_PROMPT, tools=[.