For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
  • Overview
    • Quickstart
    • Principles
    • Models
    • Stripe Projects
    • FAQ
    • Report Feedback
  • Models & Routing
    • Model Fallbacks
    • Provider Selection
    • Auto Exacto
    • Private Models
  • Features
    • Workspaces
    • Presets
    • Response Caching
    • Tool Calling
    • Structured Outputs
    • Message Transforms
    • Zero Completion Insurance
    • ZDR
    • App Attribution
    • Service Tiers
    • Sovereign AI
    • Router Metadata
    • Input & Output Logging
      • For Providers
      • Frameworks and Integrations Overview
      • Awesome OpenRouter
      • Effect AI SDK
      • Arize
      • LangChain
      • LiveKit
      • Langfuse
      • Mastra
      • OpenAI SDK
      • Anthropic Agent SDK
      • PydanticAI
      • Replit
      • TanStack AI
      • Vercel AI SDK
      • Xcode
      • Zapier
      • Infisical
LogoLogo
ModelsChatRankingsDocs
On this page
  • Configuration
  • TypeScript Example
  • Python Example
Community

Anthropic Agent SDK

Using OpenRouter with the Anthropic Agent SDK
Was this page helpful?
Previous

PydanticAI

Using OpenRouter with PydanticAI
Next
Built with

The Anthropic Agent SDK lets you build AI agents programmatically using Python or TypeScript. Since the Agent SDK uses Claude Code as its runtime, you can connect it to OpenRouter using the same environment variables.

Configuration

Set the following environment variables before running your agent:

$export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
$export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
$export ANTHROPIC_API_KEY="" # Important: Must be explicitly empty

TypeScript Example

Install the SDK:

$npm install @anthropic-ai/claude-agent-sdk

Create an agent that uses OpenRouter:

1import { query } from "@anthropic-ai/claude-agent-sdk";
2
3// Environment variables should be set before running:
4// ANTHROPIC_BASE_URL=https://openrouter.ai/api
5// ANTHROPIC_AUTH_TOKEN=your_openrouter_api_key
6// ANTHROPIC_API_KEY=""
7
8async function main() {
9 for await (const message of query({
10 prompt: "Find and fix the bug in auth.py",
11 options: {
12 allowedTools: ["Read", "Edit", "Bash"],
13 },
14 })) {
15 if (message.type === "assistant") {
16 console.log(message.message.content);
17 }
18 }
19}
20
21main();

Python Example

Install the SDK:

$pip install claude-agent-sdk

Create an agent that uses OpenRouter:

1import asyncio
2from claude_agent_sdk import query, ClaudeAgentOptions
3
4# Environment variables should be set before running:
5# ANTHROPIC_BASE_URL=https://openrouter.ai/api
6# ANTHROPIC_AUTH_TOKEN=your_openrouter_api_key
7# ANTHROPIC_API_KEY=""
8
9async def main():
10 async for message in query(
11 prompt="Find and fix the bug in auth.py",
12 options=ClaudeAgentOptions(
13 allowed_tools=["Read", "Edit", "Bash"]
14 )
15 ):
16 print(message)
17
18asyncio.run(main())

Tip: The Agent SDK inherits all the same model override capabilities as Claude Code. You can use ANTHROPIC_DEFAULT_SONNET_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL, and other environment variables to route your agent to different models on OpenRouter. See the Claude Code integration guide for more details.