Skip to main content
Route Claude Code through Anthropic’s Direct API via Portkey. Get full observability, cost tracking, and budget controls. Also supports Max plan and Anthropic OAuth. See Claude Code overview for why platform teams use Portkey.
Important: Always use the latest version of Claude Code. Older versions may not work with Portkey’s gateway.

Quick Start

1

Create a Virtual Key

Go to Virtual KeysCreate → Select Anthropic.Enter your Anthropic API key and save. Copy the virtual key ID.
2

Get Your Portkey API Key

Go to API Keys → Generate a new key.
3

Configure Claude Code

Edit ~/.claude/settings.json (user-level) or .claude/settings.json (project-level):
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1",
    "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY",
    "ANTHROPIC_MODEL": "claude-sonnet-4-20250514"
  }
}
Replace:
  • YOUR_PORTKEY_API_KEY with your Portkey API key
  • YOUR_VIRTUAL_KEY with your virtual key ID
That’s it! All Claude Code requests now route through Anthropic via Portkey. Monitor usage in the Portkey Dashboard.

Max Plan / Anthropic OAuth (Enterprise)

For Max plan or Anthropic OAuth users:
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1",
    "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic"
  }
}
Cost tracking: With Max plan, costs show as $0 in Portkey since Anthropic bills directly. Portkey still tracks request counts, latency, tokens, and traces.

Using a Config

For advanced features like fallbacks, caching, or retries, create a Portkey Config:
1

Create a Config

Go to ConfigsCreate Config:
{
  "virtual_key": "YOUR_VIRTUAL_KEY",
  "cache": { "mode": "simple" },
  "retry": { "attempts": 3, "on_status_codes": [429, 500, 502, 503] }
}
Save and copy the Config ID.
2

Update Claude Code Settings

Add the config to your settings:
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1",
    "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-config: YOUR_CONFIG_ID",
    "ANTHROPIC_MODEL": "claude-sonnet-4-20250514"
  }
}

Adding Metadata

Track Claude Code usage by team, project, or user:
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1",
    "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-provider: anthropic\nx-portkey-virtual-key: YOUR_VIRTUAL_KEY\nx-portkey-metadata: {\"_user\": \"john.doe\", \"team\": \"engineering\"}",
    "ANTHROPIC_MODEL": "claude-sonnet-4-20250514"
  }
}

Advanced Configuration

Fallbacks

Route to backup providers when Anthropic fails. Create a config with fallback targets:
{
  "strategy": { "mode": "fallback" },
  "targets": [
    { "virtual_key": "anthropic-virtual-key" },
    { "virtual_key": "bedrock-virtual-key" },
    { "virtual_key": "vertex-virtual-key" }
  ]
}

Load Balancing

Distribute requests across multiple API keys:
{
  "strategy": { "mode": "loadbalance" },
  "targets": [
    { "virtual_key": "anthropic-key-1", "weight": 0.5 },
    { "virtual_key": "anthropic-key-2", "weight": 0.5 }
  ]
}

Caching

Reduce costs and latency for repeated queries:
{
  "virtual_key": "YOUR_VIRTUAL_KEY",
  "cache": { "mode": "simple" }
}

Retries

Automatically retry failed requests:
{
  "virtual_key": "YOUR_VIRTUAL_KEY",
  "retry": { "attempts": 3, "on_status_codes": [429, 500, 502, 503] }
}

Budget Limits

Set spending controls on your virtual key:
  1. Go to Virtual Keys → Select your Anthropic virtual key
  2. Click EditBudget & Limits
  3. Configure:
    • Cost limit: Maximum spend (e.g., $500/month)
    • Rate limit: Requests per minute/hour
Budget limits prevent runaway costs from agentic coding sessions.

Troubleshooting

Claude Code Version Issues

Cause: Older versions of Claude Code may not be compatible. Fix: Update to the latest version:
claude update
Or reinstall:
curl -fsSL https://claude.ai/install.sh | bash

Complete Example

Full configuration with all features enabled:
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.portkey.ai/v1",
    "ANTHROPIC_CUSTOM_HEADERS": "x-portkey-api-key: YOUR_PORTKEY_API_KEY\nx-portkey-config: YOUR_CONFIG_ID\nx-portkey-metadata: {\"_user\": \"john.doe\", \"project\": \"backend-api\"}",
    "ANTHROPIC_MODEL": "claude-sonnet-4-20250514"
  }
}
With Portkey Config:
{
  "strategy": { "mode": "fallback" },
  "targets": [
    { "virtual_key": "anthropic-virtual-key" },
    { "virtual_key": "bedrock-virtual-key" }
  ],
  "cache": { "mode": "simple" },
  "retry": { "attempts": 3, "on_status_codes": [429, 500, 502, 503] }
}

Next Steps

Last modified on February 2, 2026