Published on

Using Claude Code and Agent SDK with Alternative Providers

Authors

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

ProviderBase URLKey VariableNotes
Anthropic(default)ANTHROPIC_API_KEYNo configuration needed
Z.AIhttps://api.z.ai/api/anthropicANTHROPIC_AUTH_TOKENHigher token limits, GLM-4.7 access
OpenRouterhttps://openrouter.ai/apiANTHROPIC_AUTH_TOKEN400+ 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.

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"
TierModel IDProvider
Sonnetz-ai/glm-4.7Z.AI
Sonnetopenai/gpt-5.2OpenAI
Sonnetmistralai/devstral-2512Mistral
Opusopenai/gpt-5.2-proOpenAI
Haikuz-ai/glm-4.5-airZ.AI
Haikuxiaomi/mimo-v2-flash:freeXiaomi

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

VariablePurposeRequired For
ANTHROPIC_BASE_URLAPI endpoint URLZ.AI, OpenRouter
ANTHROPIC_AUTH_TOKENAuthentication tokenZ.AI, OpenRouter
ANTHROPIC_API_KEYAnthropic API key (set empty for alternatives)OpenRouter
ANTHROPIC_DEFAULT_SONNET_MODELOverride Sonnet tier modelModel customization
ANTHROPIC_DEFAULT_OPUS_MODELOverride Opus tier modelModel customization
ANTHROPIC_DEFAULT_HAIKU_MODELOverride Haiku tier modelModel customization
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFICDisable telemetryRecommended for all

Verification

After configuration, verify your setup:

  1. Run /status inside Claude Code to check the connection
  2. Check OpenRouter Activity Dashboard to see requests (if using OpenRouter)
  3. Monitor your provider's usage dashboard for billing accuracy

Troubleshooting

IssueSolution
Auth errorsEnsure ANTHROPIC_API_KEY="" is explicitly empty (not unset)
Requests still hitting AnthropicRun /logout first if previously logged in
Model not foundCheck model ID on OpenRouter models
Tool use failingVerify your chosen model supports tool use

Important Considerations

  1. 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.

  2. Billing: You're billed through your chosen provider. Usage appears in their respective dashboards, not Anthropic's.

  3. Privacy: OpenRouter doesn't log prompts by default. Check provider privacy policies for specifics.

  4. 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.