標題卡寫著 Kimi K3.1 Pro 在 Claude Code 與 Codex 中,副標是設定、靜默失敗,以及三個統計標籤分別寫著 Messages API、Responses API 和 1M 上下文
Guides & Insights

在 Claude Code 與 Codex 中執行 Kimi K3.1 Pro:設定、無聲失敗,以及一行切換

作者

Rowan Sterling

發佈日期

最新模型 · 20查看全部模型 →
基準測試:Artificial Analysis · 每日更新
返回全部文章

Kimi K3.1 Pro 支援三種協定:OpenAI Chat Completions、OpenAI Responses 和 Anthropic Messages。這正是它能直接接入 Claude Code 和 Codex 而無需轉譯代理的全部原因——每個工具本來就使用這三種協定之一,而 Moonshot 原生提供該協定。設定上,一種情況是幾個環境變數,另一種情況是五行 TOML 區塊。真正讓人耗掉一個下午的不是設定,而是失敗模式。Claude Code 針對不同工作使用四種不同的模型層級,任何未設定的層級都會無聲失敗,而不是明顯報錯。

本指南依據 Moonshot 自家的整合文件撰寫,並於 2026 年 10 月 24 日查核,同時納入文件中隱而未提的注意事項。若將相同工具指向路由閘道而非直接指向 Moonshot 時,某個數值會有所不同,文中會特別註明。

為什麼不需要代理

Claude Code talks to Anthropic's Messages API; Codex talks to the Responses API. Moonshot exposes both. Its Anthropic-compatible surface lives at https://api.moonshot.ai/anthropic and its Responses API at https://api.moonshot.ai/v1, and the model id is kimi-k3.1-pro on both. Nothing in either client needs to know it is not talking to the vendor it was built for.

在進入設定之前,有個後果值得先說明:模型一定會進行推理。沒有思考開關,深度是由頂層的 reasoning_effort 欄位設定,可接受 low、high 和 max,預設為 max。取樣參數是固定的——temperature 1.0、top-p 0.95、n 為 1——傳入其他值會回傳錯誤。如果你先前的設定調整過 temperature,那項調整不會在切換後保留下來。

Claude Code:環境區塊

Setup card headed One model, three protocols with four rows: Claude Code uses the Anthropic Messages API, Codex uses the OpenAI Responses API, the model id is kimi-k3.1-pro, and the key variable is ANTHROPIC_AUTH_TOKEN

Claude Code 會從 env 的 ~/.claude/settings.json 區塊讀取設定。寫在那裡的變數會覆寫你在 shell 中匯出的同名變數,這既是功能也是陷阱:如果你的終端機裡有過期的匯出,而檔案裡寫的是別的東西,它也救不了你。

• ANTHROPIC_BASE_URL — the Kimi endpoint root. Set it to https://api.moonshot.ai/anthropic. If you leave it unset, requests go to Anthropic and fail authentication.

• ANTHROPIC_AUTH_TOKEN — 你的 Kimi API 金鑰。這是 Claude Code 實際用來作為 bearer token 的變數;若改將 API 金鑰放在 ANTHROPIC_API_KEY,這正是設定看似正確卻回傳 401 最常見的原因。

• ANTHROPIC_MODEL — 主要對話模型:kimi-k3.1-pro。

• ANTHROPIC_DEFAULT_OPUS_MODEL、ANTHROPIC_DEFAULT_SONNET_MODEL、ANTHROPIC_DEFAULT_HAIKU_MODEL、ANTHROPIC_DEFAULT_FABLE_MODEL——每個層級各一個。至少設定前三個,並將 HAIKU 指向便宜又快速的模型,而不是旗艦模型;Moonshot 自己的範例會將該層級對應到其程式碼模型。

• CLAUDE_CODE_SUBAGENT_MODEL — 子代理所使用的設定。未設定時,子代理的工作會降級或失敗。

• CLAUDE_CODE_AUTO_COMPACT_WINDOW — 設為 1000000,與上下文視窗相符。太小會過早壓縮並丟棄上下文;太大則產生上下文長度錯誤。

• CLAUDE_CODE_EFFORT_LEVEL — Moonshot 的指南將其設為 max,這也是該模型的預設值。調低它會降低複雜任務的品質,而在工作階段中途變更它則會使前綴快取命中失效。

四個會無聲失敗的層級

這正是值得預先規劃的失敗情況。Claude Code 會針對主要對話、背景摘要、子代理與小型工具呼叫使用不同模型。只設定 ANTHROPIC_MODEL,其他路徑並不會報錯——它們會默默地不產生任何有用作用,或以某些方式退回,讓工具感覺像壞了,卻不說明原因。Moonshot 的參考表明確指出,每個未設定的層級都會導致對應任務失敗,而症狀看起來像是代理變慢或混亂,而不是設定錯誤。

實用的檢查清單是:在一次編輯中設定全部四個層級變數以及子代理變數,重新啟動 Claude Code,然後在工作階段中確認子代理確實有在執行。如果它卡住了,第一個該檢查的是層級,而不是模型。

Codex:一個 TOML 區塊

Codex reads ~/.codex/config.toml (on Windows, %USERPROFILE%\.codex\config.toml). The connection is defined by four values in a provider block: the model id kimi-k3.1-pro, a provider named kimi, base_url pointing at https://api.moonshot.ai/v1, the environment-variable name that holds the key, and — the setting that makes the direct connection work — wire_api = "responses". Set model_context_window to 1048576 as well; without it Codex falls back to default model metadata, which degrades behaviour on long inputs.

金鑰本身應放在環境中,而不是檔案裡。Codex 會從 env_key 指定的變數讀取它,因此請讓 config.toml 不含任何機密,並在你的 shell 設定檔或系統環境中設定該變數。

在提交錯誤報告前,有兩個限制需要知道。即使模型支援影片,Codex CLI 也不接受影片作為輸入——這是 Codex 輸入層的限制,因此影片相關工作必須直接透過 API 進行。此外,Codex 本身的上下文管理仍然適用:一百萬個 token 的視窗並不代表每個工作階段都應該用到這麼大。

改為將這兩個工具都指向同一個閘道

Moonshot's Kimi platform documentation page titled Use Kimi in Claude Code, describing the Anthropic-compatible Messages API integration, with install commands for macOS, Linux, PowerShell and npmOrcaRouter's Kimi K3.1 Pro model page showing the identifier kimi/kimi-k3.1-pro, the three supported endpoints for chat completions, responses and messages, a 1M-token context window, 943K maximum output and $2.00 input and $10.00 output per million tokens

只要將 Moonshot 的基礎 URL 替換為閘道的基礎 URL,上述一切都能保持不變地運作;當同一個程式碼庫也會呼叫其他廠商的模型時,這會很有用。/v1/chat/completions、/v1/responses 與 /v1/messages——並以 ID 發布該模型:kimi/kimi-k3.1-pro。

For Claude Code, that means ANTHROPIC_BASE_URL set to the gateway root (https://api.orcarouter.ai) with the provider-prefixed model id in the four tier variables. For Codex, the same TOML block with the base URL pointing at https://api.orcarouter.ai/v1 and the model id kimi/kimi-k3.1-pro. The key variable stays the same shape; it just holds a gateway key instead of a vendor key.

換掉之後,你得到的是目錄中每一個模型各一組憑證,以及一套能在不同供應商之間移動請求、而不必更動應用程式碼的路由策略——這正是讓備援方案得以成立的原因,當新模型在發布當週的端點遭到限流時尤其如此。你要放棄的,是供應商自家的主控台,以及任何供應商專屬的測試版介面。價格則按供應商的定價原樣轉嫁:Kimi K3.1 Pro 每百萬輸入 token 為 2.00 美元、每百萬輸出 token 為 10.00 美元,不額外加價。

五分鐘的驗證

• 在動到用戶端之前,先直接呼叫端點:對 Messages 或 Chat Completions 路由發出帶有單一 token 請求的 POST,應該會回傳 JSON;而 401 表示金鑰錯誤,或屬於另一個平台。

• 在 Claude Code 中,提出一個會迫使系統啟動子代理的問題,例如跨多個檔案進行搜尋,並觀察它是否完成。這是測試層級配置最快的方法。

• 在 Codex 中,於小型儲存庫上啟動工作階段,並確認模型回報的是 1M 視窗;若它回報較小的預設值,表示 model_context_window 並未生效。

• 在工作階段開始時,刻意設定一次投入程度:max 用於困難的除錯,high 用於一般編輯,low 用於擷取與格式化。預設值是 max,而在這個模型上,那會是成本高昂的選擇。