Claude Code CLI Setup
Create a Gate.AI API Key
- Open gate.ai → Dashboard → API Keys, create and copy a key starting with
sk-v1-… - Confirm your account has sufficient balance
Network connectivity check
Replace GATEAI_API_KEY with your key:
1export GATEAI_API_KEY="sk-v1-xxxxxxxxxxxxxxxx"Anthropic-compatible endpoint (Claude Code):
1curl -s -o /dev/null -w "%{http_code}" \2 -H "x-api-key: $GATEAI_API_KEY" \3 -H "content-type: application/json" \4 -H "anthropic-version: 2023-06-01" \5 -d '{"model":"anthropic/claude-sonnet-4.6","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}' \6 https://api.gate.ai/anthropic/v1/messages- Returns 200: gateway is reachable; proceed with install and configuration
- Returns 401: invalid or expired key — check the dashboard
- Connection timeout: check local network or DNS; do not use
https://api.gate.ai/v1
Install Claude Code CLI
macOS / Linux / WSL (recommended):
1curl -fsSL https://claude.ai/install.sh | bashOr with npm:
1npm install -g @anthropic-ai/claude-codeIf npm install fails
This is usually caused by registry.npmjs.org timeouts or instability — unrelated to the Gate.AI gateway. Try one of the following:
Option A: one-off install with a mirror (try first)
1npm install -g @openai/codex --registry=https://registry.npmmirror.com2npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.comOption B: set mirror globally
1# Use npmmirror (common in China)2npm config set registry https://registry.npmmirror.com34# Verify registry5npm config get registry67# Reinstall8npm install -g @openai/codex9npm install -g @anthropic-ai/claude-codeOption C: current shell session only
1export NPM_CONFIG_REGISTRY=https://registry.npmmirror.com2npm install -g @openai/codex| Symptom | Fix |
|---|---|
ETIMEDOUT / ECONNRESET | Switch mirror (Option A or B) and retry |
EACCES permission error | Configure npm global prefix: mkdir -p ~/.npm-global && npm config set prefix ~/.npm-global, and add export PATH=~/.npm-global/bin:$PATH to ~/.zshrc |
command not found: claude / codex | Installed but PATH not updated: restart the terminal, or confirm bin under npm config get prefix is on PATH |
Configure model
Replace all sk-v1-your-key placeholders with your real key. No need to repeat unless you change the key or model.
1mkdir -p ~/.claude23cat > ~/.claude/settings.json <<'EOF'4{5 "env": {6 "ANTHROPIC_BASE_URL": "https://api.gate.ai/anthropic",7 "ANTHROPIC_API_KEY": "sk-v1-your-key",8 "ANTHROPIC_MODEL": "anthropic/claude-sonnet-4.6"9 },10 "includeCoAuthoredBy": false11}12EOF1314# Clear old Anthropic / proxy sessions to avoid auth conflicts15claude /logout 2>/dev/null || true16unset ANTHROPIC_AUTH_TOKENStore credentials only in
settings.json. If~/.zshrcstill hasANTHROPIC_AUTH_TOKENor a duplicateANTHROPIC_API_KEY, comment or remove them.
Verify setup
In your project directory, start the CLI for AI-assisted coding in the terminal.
Run claude
1claudeFirst check: run /status and confirm Base URL is https://api.gate.ai/anthropic and auth token is ANTHROPIC_API_KEY.
Advanced configuration
Auth conflict
If you see Auth conflict: Both a token (ANTHROPIC_AUTH_TOKEN) and an API key (ANTHROPIC_API_KEY) are set:
| Scenario | Fix |
|---|---|
| Previously logged in via Anthropic OAuth / local proxy | Run claude /logout, then start claude again |
| Both token and key configured in shell and settings | Keep only ANTHROPIC_API_KEY; comment out ANTHROPIC_AUTH_TOKEN in ~/.zshrc |
Alternative configuration
Shell environment variables (do not duplicate settings.json):
1export ANTHROPIC_BASE_URL="https://api.gate.ai/anthropic"2export ANTHROPIC_API_KEY="sk-v1-xxxxxxxxxxxxxxxx"3export ANTHROPIC_MODEL="anthropic/claude-sonnet-4.6"Project-level settings (share structure; never commit real keys):
| Path | Purpose |
|---|---|
.claude/settings.json | Project-level, committable (no secrets) |
.claude/settings.local.json | Local project key; add to .gitignore |
Model environment variables
Add to the env block in settings.json:
| Variable | Purpose | Example |
|---|---|---|
ANTHROPIC_DEFAULT_SONNET_MODEL | Sonnet-tier tasks | anthropic/claude-sonnet-4.6 |
ANTHROPIC_DEFAULT_OPUS_MODEL | Opus-tier tasks | anthropic/claude-opus-4.6 |
ANTHROPIC_DEFAULT_HAIKU_MODEL | Haiku-tier tasks | anthropic/claude-haiku-4.5 |
CLAUDE_CODE_SUBAGENT_MODEL | Sub-agent tasks | anthropic/claude-sonnet-4.6 |
Full list: model catalog. If auto fails, use an explicit model ID.
Restore direct Anthropic (optional)
1env -u ANTHROPIC_BASE_URL -u ANTHROPIC_API_KEY claudeTroubleshooting
Common Base URL mistakes
1# ❌ Wrong (missing /anthropic prefix, 404)2https://api.gate.ai/v13https://api.gate.ai/v1/chat/completions45# ❌ Wrong (do not use full API path in Claude config)6https://api.gate.ai/anthropic/v1/messages7https://api.gate.ai/anthropic/messages89# ✅ Correct10https://api.gate.ai/anthropic/v111https://api.gate.ai/anthropic # Claude Code CLI12https://api.gate.ai/anthropic/v1/messages # curl key check (Anthropic)13https://api.gate.ai/openai/v1/responses # curl key check (Codex)/openai/v1/chat/completionsworks, but Codex must use the Responses API. On 404 for/responses, checkwire_api = "responses", not the URL./openai/v1/modelsdoes not validate keys (invalid keys may still return 200). Use the curl endpoints above to verify keys.
Quick reference
| Symptom | Type | Suggestion |
|---|---|---|
| Auth conflict warning | Auth conflict | Run claude /logout; keep only ANTHROPIC_API_KEY; remove or comment ANTHROPIC_AUTH_TOKEN |
| 401 / auth failure | Auth error | Check the key is correct and not expired |
| 404 on URL | Path error | Claude → https://api.gate.ai/anthropic; Codex → https://api.gate.ai/openai/v1. Do not use https://api.gate.ai/v1/... |
| Model not found | Model ID error | Use provider/model-name format (e.g. anthropic/claude-sonnet-4.6, openai/gpt-5.2); see the model catalog |
| Still connecting to official domain | Config not applied | Use user-level config: Claude → ~/.claude/settings.json; Codex → ~/.codex/config.toml (project .codex/config.toml cannot override gateway settings) |
| Shows offline | CLI behavior | Does not affect main chat; safe to ignore |
| 402 / 429 | Quota / rate limit | Top up or check key budget and rate limits |
| npm install fails | Network / permissions | See "If npm install fails" |