Claude Code — Third-Party LLM Provider Connection
Connect OpenAI-compatible LLM APIs (Alibaba Qwen, DashScope, any /v1/chat/completions endpoint) to Claude Code, which only speaks the Anthropic Messages protocol.
When to Use
- User wants to use a non-Anthropic model (Qwen, GLM, DeepSeek, etc.) inside Claude Code
- User has an OpenAI-compatible API key/endpoint and wants Claude Code as the coding agent
- Claude Code says "Not logged in" or "Invalid API key" when pointed at a non-Anthropic endpoint
Core Problem
Claude Code only speaks the Anthropic Messages API format (/v1/messages with content blocks, tool_use, etc.). Setting OPENAI_API_KEY/OPENAI_BASE_URL does NOT work — Claude Code ignores them and demands Anthropic auth. Setting ANTHROPIC_BASE_URL to an OpenAI-compatible endpoint fails because the request/response formats differ.
Solution: Local Translation Proxy
A lightweight Python proxy on localhost translates:
- Inbound: Anthropic Messages API (
POST /v1/messages) from Claude Code - Outbound: OpenAI Chat Completions API (
POST /v1/chat/completions) to the provider
Setup Steps
- Write the proxy — use
templates/proxy.pyfrom this skill (copy to~/claude-qwen-proxy/proxy.py) - Start the proxy:
export OPENAI_API_KEY="<provider-api-key>" export OPENAI_BASE_URL="<provider-base-url>" # e.g. https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1 python ~/claude-qwen-proxy/proxy.py # Listens on http://127.0.0.1:8082 - Run Claude Code through the proxy:
export ANTHROPIC_API_KEY="sk-proxy-placeholder" # ANY non-empty string works export ANTHROPIC_BASE_URL="http://127.0.0.1:8082" claude --bare --model qwen3.8-max-preview -p "Say hello" --max-turns 1 - Verify — should get a text response. If "Invalid API key" → ANTHROPIC_API_KEY not set. If "model not found" → check provider's
/v1/modelsendpoint for exact model IDs.
Windows Convenience Scripts
Create start-proxy.bat (sets OPENAI_* env vars, runs proxy.py) and claude-qwen.bat (sets ANTHROPIC_* env vars, runs claude). See templates/ directory.
Pitfalls
- ANTHROPIC_API_KEY must be set — even to a dummy value. Without it, Claude Code enters OAuth login flow and ignores ANTHROPIC_BASE_URL.
--bareflag is important — skips OAuth/keychain reads, plugin sync, and CLAUDE.md discovery. Without it, Claude Code may try to validate the key against Anthropic's servers.- litellm proxy is an alternative but often fails on Windows —
pip install litellmhits metadata generation errors. The custom proxy (stdlib-only, no deps) is more reliable. - Model names must match the provider's
/v1/modelslist exactly — query it withcurl -H "Authorization: Bearer $KEY" $BASE_URL/models. - Streaming translation is imperfect — tool_use blocks in streaming mode require stateful reassembly. The template proxy handles basic text streaming; complex multi-tool agentic loops may hit edge cases.
- Proxy must stay running — it's a foreground process. Use
start-proxy.batin a minimized window or run under a process manager. - Claude Code features that depend on Anthropic-specific APIs won't work — e.g., extended thinking blocks, prompt caching headers,
/usagecommand. Basic coding (read/write/bash) works fine.
Verification Checklist
# 1. Provider endpoint reachable?
curl -s "$OPENAI_BASE_URL/models" -H "Authorization: Bearer $OPENAI_API_KEY" | head -5
# 2. Proxy running?
curl -s http://127.0.0.1:8082/ # → {"status": "ok", "proxy": "anthropic-to-openai"}
# 3. Claude Code connected?
ANTHROPIC_API_KEY=sk-placeholder ANTHROPIC_BASE_URL=http://127.0.0.1:8082 \
claude --bare --model <model-id> -p "Say hello" --max-turns 1
Known Working Providers
| Provider | Base URL | Models confirmed |
|---|---|---|
| Alibaba Coding Plan | https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1 |
qwen3.8-max-preview, glm-5.2, deepseek-v4-pro |
| DashScope (general) | https://dashscope.aliyuncs.com/compatible-mode/v1 |
qwen-* series |
Gap Note
The bundled claude-code skill (v2.2.1) does NOT cover third-party provider connection. Its auth section only documents Anthropic OAuth, ANTHROPIC_API_KEY for Anthropic, and Bedrock/Vertex/Foundry. If that skill is ever updated, this content should be merged into it.