- Published on
Using Claude Code and Agent SDK with Alternative Providers
- Authors

- Name
- Avasdream
- @avasdream_
Claude Code isn't locked to Anthropic's direct API. By setting a few environment variables, you can route requests through alternative providers like Z.AI or OpenRouter—useful for accessing different models, managing costs, or leveraging provider-specific features.
Quick Reference
| Provider | Base URL | Key Variable | Notes |
|---|---|---|---|
| Anthropic | (default) | ANTHROPIC_API_KEY | No configuration needed |
| Z.AI | https://api.z.ai/api/anthropic | ANTHROPIC_AUTH_TOKEN | Higher token limits, GLM-4.7 access |
| OpenRouter | https://openrouter.ai/api | ANTHROPIC_AUTH_TOKEN | 400+ models, failover, budget controls |
Anthropic (Default)
No setup required. After logging in, Claude Code uses Anthropic's API directly:
claude
For API key authentication without interactive login:
export ANTHROPIC_API_KEY="sk-ant-..."
claude
Z.AI Configuration
Z.AI provides an Anthropic-compatible endpoint with higher token quotas and access to GLM-4.7 models.
Shell Function (Recommended)
Add to ~/.zshrc or ~/.bashrc:
zaiclaude() {
ANTHROPIC_BASE_URL=https://api.z.ai/api/anthropic \
ANTHROPIC_AUTH_TOKEN="YOUR_ZAI_TOKEN" \
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 \
claude "$@"
}
Usage:
zaiclaude
Fish Shell
Add to ~/.config/fish/config.fish:
function zaiclaude
set -lx ANTHROPIC_BASE_URL https://api.z.ai/api/anthropic
set -lx ANTHROPIC_AUTH_TOKEN "YOUR_ZAI_TOKEN"
set -lx CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC 1
claude $argv
end
Global Configuration
To make Z.AI the default for all Claude Code sessions:
export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="YOUR_ZAI_TOKEN"
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
OpenRouter Configuration
OpenRouter aggregates 400+ models and provides automatic failover between providers.
Shell Function
openrouterclaude() {
ANTHROPIC_BASE_URL="https://openrouter.ai/api" \
ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY" \
ANTHROPIC_API_KEY="" \
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 \
claude "$@"
}
Important: ANTHROPIC_API_KEY must be explicitly set to empty string. If unset (null), Claude Code may fall back to default authentication behavior.
Fish Shell
function openrouterclaude
set -lx ANTHROPIC_BASE_URL "https://openrouter.ai/api"
set -lx ANTHROPIC_AUTH_TOKEN "$OPENROUTER_API_KEY"
set -lx ANTHROPIC_API_KEY ""
set -lx CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC 1
claude $argv
end
Global Configuration
export OPENROUTER_API_KEY="sk-or-..."
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
Model Overrides
When using OpenRouter, you can override which models Claude Code uses for different tiers:
# Override the default "Sonnet" tier model
export ANTHROPIC_DEFAULT_SONNET_MODEL="z-ai/glm-4.7"
# Override the "Opus" tier model
export ANTHROPIC_DEFAULT_OPUS_MODEL="openai/gpt-5.2-pro"
# Override the "Haiku" tier model
export ANTHROPIC_DEFAULT_HAIKU_MODEL="z-ai/glm-4.5-air"
Popular Model Choices
| Tier | Model ID | Provider |
|---|---|---|
| Sonnet | z-ai/glm-4.7 | Z.AI |
| Sonnet | openai/gpt-5.2 | OpenAI |
| Sonnet | mistralai/devstral-2512 | Mistral |
| Opus | openai/gpt-5.2-pro | OpenAI |
| Haiku | z-ai/glm-4.5-air | Z.AI |
| Haiku | xiaomi/mimo-v2-flash:free | Xiaomi |
GLM-4.7 via OpenRouter
Dedicated function for using Z.AI's GLM-4.7 through OpenRouter:
glmclaude() {
ANTHROPIC_BASE_URL="https://openrouter.ai/api" \
ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY" \
ANTHROPIC_API_KEY="" \
ANTHROPIC_DEFAULT_SONNET_MODEL="z-ai/glm-4.7" \
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 \
claude "$@"
}
Anthropic Agent SDK
The Agent SDK uses Claude Code as its runtime, so the same environment variables apply.
TypeScript
npm install @anthropic-ai/claude-agent-sdk
import { query } from "@anthropic-ai/claude-agent-sdk";
// Set environment variables 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();
Python
pip install claude-agent-sdk
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
# Set environment variables 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())
The Agent SDK inherits all model override capabilities. Use ANTHROPIC_DEFAULT_SONNET_MODEL and similar variables to route agents to different models.
GitHub Actions
Use OpenRouter with the official Claude Code GitHub Action:
- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.OPENROUTER_API_KEY }}
env:
ANTHROPIC_BASE_URL: https://openrouter.ai/api
Store your OpenRouter API key as a GitHub secret named OPENROUTER_API_KEY.
Environment Variables Reference
| Variable | Purpose | Required For |
|---|---|---|
ANTHROPIC_BASE_URL | API endpoint URL | Z.AI, OpenRouter |
ANTHROPIC_AUTH_TOKEN | Authentication token | Z.AI, OpenRouter |
ANTHROPIC_API_KEY | Anthropic API key (set empty for alternatives) | OpenRouter |
ANTHROPIC_DEFAULT_SONNET_MODEL | Override Sonnet tier model | Model customization |
ANTHROPIC_DEFAULT_OPUS_MODEL | Override Opus tier model | Model customization |
ANTHROPIC_DEFAULT_HAIKU_MODEL | Override Haiku tier model | Model customization |
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC | Disable telemetry | Recommended for all |
Verification
After configuration, verify your setup:
- Run
/statusinside Claude Code to check the connection - Check OpenRouter Activity Dashboard to see requests (if using OpenRouter)
- Monitor your provider's usage dashboard for billing accuracy
Troubleshooting
| Issue | Solution |
|---|---|
| Auth errors | Ensure ANTHROPIC_API_KEY="" is explicitly empty (not unset) |
| Requests still hitting Anthropic | Run /logout first if previously logged in |
| Model not found | Check model ID on OpenRouter models |
| Tool use failing | Verify your chosen model supports tool use |
Important Considerations
Tool Use Support: Claude Code relies heavily on tool use (file reading, command execution, editing). When selecting alternative models, ensure they support tool use. Filter by this capability on the OpenRouter models page.
Billing: You're billed through your chosen provider. Usage appears in their respective dashboards, not Anthropic's.
Privacy: OpenRouter doesn't log prompts by default. Check provider privacy policies for specifics.
Performance: While many models work, Claude models typically provide the best experience for complex coding tasks due to their optimization for tool use and code generation.