翻篇收录说明
翻篇网页快照

Anthropic Agent SDK Integration - OpenRouter SDK Support

https://openrouter.ai/docs/guides/community/anthropic-agent-sdk

最近抓取:2026-12-04 13:23:48(北京时间)

最近内容变化:2026-12-01 20:54:38(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Using OpenRouter with the Anthropic Agent SDK

正文摘录

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: import { query } from "@anthropic-ai/claude-agent-sdk"; // Environment variables should be set before running: // ANTHROPIC_BASE_URL=https://openrouter.ai/api // ANTHROPIC_AUTH_TOKEN=your_openrouter_api_key // ANTHROPIC_API_KEY="" async function main() { for await (const message of query({ prompt: "Find and fix the bug in auth.py", options: { allowedTools: ["Read", "Edit", "Bash"], }, })) { if (message.type === "assistant") { console.log(message.message.content); } } } main(); See all 21 lines Python Example Install the SDK: pip install claude-agent-sdk Create an agent that uses OpenRouter: import asyncio from claude_agent_sdk import query, ClaudeAgentOptions # Environment variables should be set before running: # ANTHROPIC_BASE_URL=https://openrouter.ai/api # ANTHROPIC_AUTH_TOKEN=your_openrouter_api_key # ANTHROPIC_API_KEY="" async def main(): async for message in query( prompt="Find and fix the bug in auth.py", options=ClaudeAgentOptions( allowed_tools=["Read", "Edit", "Bash"] ) ): print(message) asyncio.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.Was this page helpful?YesNo