如何讓 Claude Code、Cursor 和 Cline 以一個 OpenAI-compatible key 使用 GPT、Claude、Gemini 和 DeepSeek
Use Claude Code, Cursor and Cline with one OpenAI-compatible key (GPT, Claude, Gemini, DeepSeek)
文章示範如何讓 Claude Code、Cursor、Cline 及 Python/Node 的 OpenAI SDK 共用 OpenAI-compatible gateway 的 key,切換模型時只需更改模型名稱。
Disclosure: I work on 666 AI, the gateway used in the examples below. The setup pattern works with any OpenAI-compatible gateway.
If you switch between models a lot, you end up with several API keys, several bills, and a different config in every tool. Gateways fix that: one key, one balance, and the tool only needs two fields changed — the base URL and the key.
Below are working configs for the most common tools.
1. The two values you need
-
Base URL (OpenAI format):
https://YOUR-ENDPOINT/v1 -
Base URL (Anthropic format, for Claude Code):
https://YOUR-ENDPOINT— note: no/v1 -
Key:
sk-...from your gateway console
Quick check that it works:
curl https://YOUR-ENDPOINT/v1/chat/completions \
-H "Authorization: Bearer $KEY" \
-H "Content-Type: application/json" \
-d '{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}]}'
A JSON response means you're done.
2. Claude Code
Claude Code speaks the Anthropic protocol, so point it at the Anthropic-format address:
# ~/.zshrc or ~/.bashrc
export ANTHROPIC_BASE_URL="https://YOUR-ENDPOINT"
export ANTHROPIC_AUTH_TOKEN="YOUR_KEY"
Reopen the terminal and run claude. The most common mistake is adding /v1 here — don't.
3. Cursor
- Settings → Models → OpenAI API Key
- Turn on Override OpenAI Base URL
- Paste
https://YOUR-ENDPOINT/v1and your key, click Verify - Add the model names you want to use (e.g.
claude-sonnet-5-5,gemini-3.5-flash)
4. Cline / Roo Code
- API Provider: OpenAI Compatible
- Base URL:
https://YOUR-ENDPOINT/v1 - API Key: your key
- Model ID: any model from the catalog
5. Python / Node (official SDKs, unchanged)
from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY", base_url="https://YOUR-ENDPOINT/v1")
resp = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[{"role": "user", "content": "Explain this regex: ^\\d{3}-\\d{4}$"}],
)
print(resp.choices[0].message.content)
Switching models is now a one-word change (claude-sonnet-5-5 → gemini-3.5-flash → deepseek-v4-flash), with no new SDK and no new key.
Things worth knowing
- Streaming, tool calling and vision work the same as the official APIs, as long as the model supports them.
- Prompt caching: put the unchanging part (system prompt, project files) first and the varying part last. The order decides the cache hit rate, and cached tokens are billed much cheaper.
- Set a spend cap per key, and use one key per project. If a key leaks, you lose one project's budget, not everything.
Why bother
A common split is Claude for refactors, DeepSeek for cheap bulk tasks and Gemini for long documents. With all three behind one key, the editor config never changes. Only the model name does.
If you want to try the gateway in these examples, signup is free and there's a daily free chat allowance: 666gogogo.com. Full docs: Documentation.
來源:DEV Community · dev.to