跳到內容

翻譯代理

翻譯代理是 Claudex 的核心。它位於 Claude Code 和你的 AI 提供商之間,透明地在 Anthropic Messages API 和 OpenAI Chat Completions API(或 Responses API)之間進行轉換。

Claude Code → Anthropic Messages API request
└── Claudex Proxy (127.0.0.1:13456)
├── DirectAnthropic provider → forward with headers
├── OpenAICompatible provider
│ ├── Translate request: Anthropic → OpenAI Chat Completions
│ ├── Apply query_params, strip_params, custom_headers
│ ├── Forward to provider
│ └── Translate response: OpenAI → Anthropic
└── OpenAIResponses provider
├── Translate request: Anthropic → OpenAI Responses API
├── Forward to provider
└── Translate response: Responses → Anthropic

Claudex 使用 ProviderAdapter trait 處理不同提供商 API 之間的差異。共實作了三種 adapter:

Adapter翻譯使用者
DirectAnthropic無(直通)Anthropic、MiniMax、Vertex AI
ChatCompletions完整 Anthropic 與 OpenAI 雙向翻譯Grok、OpenAI、DeepSeek、Kimi、GLM、OpenRouter、Groq、Mistral、Together AI、Perplexity、Cerebras、Azure OpenAI、GitHub Copilot、GitLab Duo、Ollama、vLLM、LM Studio
ResponsesAnthropic 與 OpenAI Responses API 雙向翻譯ChatGPT/Codex 訂閱
AnthropicOpenAI
system 欄位messages 陣列中的系統訊息
messages[].content 區塊(text、image、tool_use、tool_result)messages[].content + tool_calls
tools 陣列(JSON Schema,含 input_schematools 陣列(function 格式,含 parameters
tool_choiceautoany{name}tool_choiceautorequired{function: {name}}
max_tokensmax_tokens(若有設定 profile 的 max_tokens 則受其上限)
temperaturetop_p直接映射(若 strip_params 匹配則移除)
OpenAIAnthropic
choices[0].message.contentcontent 區塊(type: text)
choices[0].message.tool_callscontent 區塊(type: tool_use)
finish_reason: stopstop_reason: end_turn
finish_reason: tool_callsstop_reason: tool_use
usage.prompt_tokens / completion_tokensusage.input_tokens / output_tokens

Claude Code 可能產生超過 64 字元的工具名稱(例如 mcp__server-name__very-long-tool-name-that-exceeds-the-limit)。OpenAI 和許多提供商有 64 字元限制。

Claudex 自動:

  1. 在送出的請求中截斷超過 64 字元的名稱
  2. 建立截斷名稱到原始名稱的映射表
  3. 在提供商回應中還原原始名稱

此雙向轉換完全透明。

Claudex 完整支援 SSE(Server-Sent Events)串流,即時將 OpenAI 串流區塊翻譯為 Anthropic 串流事件:

OpenAI SSEAnthropic SSE
第一個區塊message_start + content_block_start
choices[0].delta.contentcontent_block_delta(text_delta)
choices[0].delta.tool_callscontent_block_delta(input_json_delta)
出現 finish_reasoncontent_block_stop + message_delta + message_stop

串流翻譯器維護一個狀態機,以正確處理工具呼叫累積和內容區塊邊界。

Azure OpenAI 使用不同的認證和 URL 結構:

  • 認證:使用 api-key 標頭取代 Authorization: Bearer
  • URL 格式https://{resource}.openai.azure.com/openai/deployments/{deployment}
  • API 版本:透過 query_params 必填

Claudex 透過檢查 base_url 是否包含 openai.azure.com 自動偵測 Azure,並據此調整認證方式。

提供商類型Base URL
AnthropicDirectAnthropichttps://api.anthropic.com
MiniMaxDirectAnthropichttps://api.minimax.io/anthropic
Google Vertex AIDirectAnthropichttps://REGION-aiplatform.googleapis.com/v1/projects/...
OpenRouterOpenAICompatiblehttps://openrouter.ai/api/v1
Grok (xAI)OpenAICompatiblehttps://api.x.ai/v1
OpenAIOpenAICompatiblehttps://api.openai.com/v1
DeepSeekOpenAICompatiblehttps://api.deepseek.com
Kimi/MoonshotOpenAICompatiblehttps://api.moonshot.ai/v1
GLM (Zhipu)OpenAICompatiblehttps://api.z.ai/api/paas/v4
GroqOpenAICompatiblehttps://api.groq.com/openai/v1
Mistral AIOpenAICompatiblehttps://api.mistral.ai/v1
Together AIOpenAICompatiblehttps://api.together.xyz/v1
PerplexityOpenAICompatiblehttps://api.perplexity.ai
CerebrasOpenAICompatiblehttps://api.cerebras.ai/v1
Azure OpenAIOpenAICompatiblehttps://{resource}.openai.azure.com/...
GitHub CopilotOpenAICompatiblehttps://api.githubcopilot.com
GitLab DuoOpenAICompatiblehttps://gitlab.com/api/v4/ai/llm/proxy
OllamaOpenAICompatiblehttp://localhost:11434/v1
vLLMOpenAICompatiblehttp://localhost:8000/v1
LM StudioOpenAICompatiblehttp://localhost:1234/v1
ChatGPT/Codex 訂閱OpenAIResponseshttps://chatgpt.com/backend-api/codex

代理暴露一個 /v1/models 端點,列出所有已啟用的 profile。每個項目包含自訂欄位:

  • x-claudex-profile:profile 名稱
  • x-claudex-provider:提供商類型(anthropicopenai-compatibleopenai-responses

Claude Code 查詢此端點以探索可用模型。

Terminal window
# 以背景程式啟動代理
claudex proxy start -d
# 檢查代理狀態
claudex proxy status
# 停止代理背景程式
claudex proxy stop
# 在自訂連接埠啟動
claudex proxy start -p 8080

執行 claudex run <profile> 時,若代理尚未運行,會自動在背景啟動。