This tutorial provides a detailed introduction to various methods of configuring the Claude API in OpenClaw to help you get started quickly.
Introduction
OpenClaw is a powerful AI agent framework that supports integrating multiple large language models. Claude is a high-performance AI model developed by Anthropic, showing excellent performance in code generation, reasoning, and analysis.
Preparation
Before getting started, you need to:
- Install OpenClaw:
npm install -g openclaw - Have the appropriate API access permissions
Method 1: Using Defapi (Recommended)
If you want to save costs, Defapi is recommended!
Defapi is an AI model aggregation platform. All Claude models are priced at half the official rate. Defapi supports the OpenAI-compatible v1/chat/completions interface, making configuration simple and convenient.
Advantages
- Half-price: Claude Sonnet 4.5 costs only $1.5/M input, $7.5/M output
- Strong compatibility: Perfectly compatible with OpenAI interface formats
- Stable and reliable: Fast access within the country
Supported Claude Models
- Claude Opus 4.5 / 4.6
- Claude Sonnet 4.5
- Claude Haiku 4.5
Configuration Steps
-
Visit Defapi, register an account, and get an API Key
-
Configure in OpenClaw:
{
env: {
OPENAI_API_KEY: "ä½ çš„Defapi-Key",
OPENAI_BASE_URL: "https://api.defapi.org/v1",
},
agents: {
defaults: {
model: { primary: "openai/claude-sonnet-4.5" },
},
},
}
- Verify the configuration:
# Send a message directly to test (recommended)
openclaw agent --message "Hello"
# Or check model status
openclaw models status
After successful verification, you can converse with Claude in the following ways:
# Send a message directly
openclaw agent --message "ä½ å¥½"
# Or use the terminal UI
openclaw tui
# Or start the Gateway and converse via other channels
openclaw gateway
Now you can enjoy Claude's powerful capabilities at half price!
Method 2: Official Anthropic API
This is the most direct integration method, suitable for users who have an API Key or a Claude subscription.
Method 2.1: API Key Authentication
If you have an Anthropic API Key (pay-as-you-go), configuration is very simple:
openclaw onboard --anthropic-api-key "$ANTHROPIC_API_KEY"
Or add it in the configuration file:
{
env: { ANTHROPIC_API_KEY: "sk-ant-..." },
agents: { defaults: { model: { primary: "anthropic/claude-opus-4-6" } } },
}
Verify:
openclaw agent --message "Hello"
Method 2.2: Setup-Token Authentication
If you have a Claude Max/Pro subscription, you can use a setup-token:
# Generate in the Claude Code CLI
claude setup-token
# Configure in OpenClaw
openclaw models auth setup-token --provider anthropic
Tip: Prompt Caching
The Anthropic API supports prompt caching, which can improve performance and reduce costs:
{
agents: {
defaults: {
models: {
"anthropic/claude-opus-4-6": {
params: { cacheRetention: "long" },
},
},
},
},
}
Method 3: OpenRouter
OpenRouter provides a unified API interface and can access various models, including the Claude series.
openclaw onboard --auth-choice apiKey --token-provider openrouter --token "$OPENROUTER_API_KEY"
Configuration:
{
env: { OPENROUTER_API_KEY: "sk-or-..." },
agents: {
defaults: {
model: { primary: "openrouter/anthropic/claude-sonnet-4-5" },
},
},
}
Verify:
openclaw agent --message "Hello"
Method 4: Claude Max API Proxy
If you have a Claude Max subscription but need an OpenAI-formatted interface, you can use the community tool claude-max-api-proxy:
npm install -g claude-max-api-proxy
claude-max-api
Configure OpenClaw:
{
env: {
OPENAI_API_KEY: "not-needed",
OPENAI_BASE_URL: "http://localhost:3456/v1",
},
agents: {
defaults: {
model: { primary: "openai/claude-opus-4" },
},
},
}
Verify:
openclaw agent --message "Hello"
Method 5: Custom Compatible APIs
For other platforms that provide compatible APIs, OpenClaw also supports custom configuration.
v1/chat/completions Format
{
env: {
CUSTOM_API_KEY: "sk-...",
},
agents: {
defaults: {
model: { primary: "custom/claude-model" },
},
},
models: {
mode: "merge",
providers: {
"custom": {
baseUrl: "https://api.example.com/v1",
apiKey: "${CUSTOM_API_KEY}",
api: "openai-completions",
models: [
{ id: "claude-model", name: "Claude Model", contextWindow: 200000 },
],
},
},
},
}
v1/messages Format (Anthropic-compatible)
{
env: {
CUSTOM_API_KEY: "sk-...",
},
agents: {
defaults: {
model: { primary: "custom/claude-opus-4" },
},
},
models: {
mode: "merge",
providers: {
"custom": {
baseUrl: "https://api.example.com/v1",
apiKey: "${CUSTOM_API_KEY}",
api: "anthropic-messages",
models: [
{ id: "claude-opus-4", name: "Claude Opus 4", contextWindow: 200000 },
],
},
},
},
}
Common Models Reference
| Model | Configuration Reference |
|---|---|
| Claude Opus 4.6 | anthropic/claude-opus-4-6 |
| Claude Sonnet 4.6 | anthropic/claude-sonnet-4-6 |
| Claude Haiku 4.6 | anthropic/claude-haiku-4-6 |
Troubleshooting
Issue: 401 error / Token expired
- Rerun
claude setup-tokenand reconfigure
Issue: "No API key found for provider anthropic"
- Authentication is isolated per agent; confirm the target agent has authentication configured
Issue: Connection failed
- Check network connectivity
- Confirm the API Key is correct
- Verify the baseUrl configuration is correct