Claude Code Proxy Connection Tutorial: Terminal Agent and Base URL Configuration

  • 发布时间
  • 语言en

Tutorial for connecting the official Anthropic terminal coding agent (Claude Code): installation, setting ANTHROPIC_BASE_URL and API keys, switching models, and troubleshooting.

1. What is Claude Code

Claude Code is Anthropic's official terminal coding agent (CLI), which allows you to read/write code, run commands, and execute tasks directly within your terminal. It natively supports environment variables like ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, making it easy to point to a proxy gateway and run using your own API keys and credits.

2. Installing Claude Code

Node.js is required (v18 or higher is officially recommended). Install it globally using:

npm install -g @anthropic-ai/claude-code

After installation, type claude in your terminal to start. Use /model or claude --model … to switch between models.

3. Configuring Environment Variables

Windows (PowerShell)

$env:ANTHROPIC_BASE_URL="https://www.relay-api.com"
$env:ANTHROPIC_API_KEY="sk-your-key"
$env:ANTHROPIC_MODEL="claude-sonnet-5"
claude

macOS / Linux (bash / zsh)

export ANTHROPIC_BASE_URL="https://www.relay-api.com"
export ANTHROPIC_API_KEY="sk-your-key"
export ANTHROPIC_MODEL="claude-sonnet-5"
claude

Note: The ANTHROPIC_API_KEY will be sent in the request header, and the ANTHROPIC_BASE_URL is used to override the default API endpoint to point to this site's Anthropic gateway.

4. API Address Reference

Model GroupBase URL (This Site)Protocol
Claude / Anthropichttps://www.relay-api.comAnthropic Messages
GPT / OpenAIhttps://www.relay-api.com/v1OpenAI Chat Completions
Geminihttps://www.relay-api.com/v1betaGoogle generateContent

5. Troubleshooting Common Issues

SymptomResolution
401 / 403Invalid key, disabled, or insufficient balance; verify the sk- prefix and top up your account.
404 / Model unavailableModel name mismatch with platform slug or model not enabled; verify the Model ID.
Unknown modelEnsure the Model ID is exact (case-sensitive); verify it in the Model Plaza.
Request timeoutIncrease timeout for long tasks; retry if there are network fluctuations.

Security Advice: Do not commit keys to public repositories; use .env files or environment variables. Generation tasks can consume significant tokens, so monitor your balance via the "Usage Logs" to track every call.

Base URL Note: This site accepts both /v1beta/models/... and /v1beta/v1beta/models/... (as well as /v1/... and /v1/v1/...), so it does not matter if your client appends /v1 or /v1beta automatically: you can set the Base URL to either the domain root or the full path with /v1beta. Please copy the Model ID from the "Model ID" section in the top right of the "Tokens" page in your console.