只傳送兩者之一。Anthropic 對 Claude Haiku 4.5 記載了這項規則;開發者在 Claude Opus 4.6 與 Sonnet 4.6 上也遇到相同的 400 錯誤;而在 Claude Opus 4.7 及之後的版本,規則收緊為兩者都不能傳送。罪魁禍首通常是用戶端或框架填入了你從未設定的參數。
錯誤
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "`temperature` and `top_p` cannot both be specified for this model. Please use only one."
},
"request_id": "req_..."
}原因與解決方法一覽
| 原因 | 解決方法 |
|---|---|
| 你的程式碼或底層框架同時傳送了兩者 | 只傳送你真正要使用的其中一個,並刪除另一個鍵。 |
| 另一個是像 top_p: 1 這樣的「中性」預設值 | 據回報仍會同樣失敗。請移除該鍵,不要只將它正規化。 |
| 較舊的 LangChain JS 或 n8n 建置版本 | 升級至 @langchain/anthropic 1.3.24 或更新版本,以及 n8n 1.114.1 或更新版本。 |
| LiteLLM 會將兩者都傳遞下去 | 對該模型加入 additional_drop_params: ["top_p"]。 |
| 你已移轉至 Claude Opus 4.7 或更新版本、Sonnet 5 或 Fable 5 | 這些版本的規則更嚴格:移除兩者——任一者的任何非預設值都會被拒絕。 |
哪些 Claude 模型會強制執行這項規則(截至 2026 年 9 月)
Anthropic 的 Haiku 4.5 移轉指南:只使用 temperature 或 top_p,不要同時使用——兩者同時設定會在 Claude Haiku 4.5 上回傳 400,對從 Claude 3.x 遷移而來的程式碼而言是破壞性變更(https://platform.claude.com/docs/en/models/haiku-4-5/migration-guide)。Anthropic 的 API 發布說明在 2025 年 8 月 5 日 Claude Opus 4.1 發布時標示了相同規則;該模型目前已退役(https://platform.claude.com/docs/en/release-notes/api)。AWS 在 Bedrock 文件中也對 Claude Sonnet 4.5 與 Claude Haiku 4.5 說明了這項規則(https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages-request-response.html)。我們找不到 Anthropic 明確說明 4.6 模型適用此規則的頁面,但開發者回報 Claude Opus 4.6(OpenHands issue #12846)與 Claude Sonnet 4.6(opencommit issue #566)會出現這個確切的 400 錯誤。Claude Opus 4.7 及更新版本如下所述:在這些版本中,傳送其中一個就已經太多。
傳送其中一個——並刪除另一個鍵
除非有理由不這麼做,否則請保留 temperature:Anthropic 的 API 參考文件指出,top_p 僅建議用於進階使用情境(https://platform.claude.com/docs/en/api/messages)。請移除第二個欄位,而不是將它設為你認為中性的值——一名開發者回報,在 Claude Sonnet 4.5 上,top_p 為 1.0 且 temperature 為 0.3 時仍然失敗(n8n issue #18304)。
from openai import OpenAI
client = OpenAI(base_url="https://api.kunavo.com/v1", api_key="sk-kn-...")
def one_sampling_knob(params: dict) -> dict:
"""Where the rule applies, send temperature or top_p, not both. Keep temperature."""
if params.get("temperature") is not None:
params.pop("top_p", None)
return params
sampling = one_sampling_knob({"temperature": 0.3, "top_p": 0.9}) # {"temperature": 0.3}
resp = client.chat.completions.create(
model="claude-haiku-4-5",
max_tokens=512,
messages=[{"role": "user", "content": "Classify this support ticket: ..."}],
**sampling,
)
print(resp.choices[0].message.content)在加入第二個參數的用戶端中修正
LangChain JS:ChatAnthropic 0.3.x 會在你設定的 top_p 旁一併傳送預設 temperature(issue #9258;PR #9362 已於 2025 年 11 月在 0.3 分支修正),而 @langchain/anthropic 於 2026 年 3 月 17 日發布的 1.3.24 版本包含 PR #10420,停止加入任何你未設定的取樣欄位(https://github.com/langchain-ai/langchainjs/pull/10420)。n8n:Anthropic Chat Model 節點會傳送 LangChain 的預設值(topP -1、temperature 0.7);已在 2025 年 10 月 2 日發布的 n8n 1.114.1 中修正(https://github.com/n8n-io/n8n/pull/20286)。LiteLLM:依照每次呼叫或每個模型,使用 additional_drop_params 指定要移除的參數(https://docs.litellm.ai/docs/completion/drop_params)——但其 Anthropic 直通路由例外;該回報以直通請求不會被修改為由結案(https://github.com/BerriAI/litellm/issues/15097)。Amazon Bedrock:在 Converse 的 inferenceConfig 中設定 temperature 或 topP,或在 InvokeModel body 中設定 temperature 或 top_p——絕不可同時設定兩者(https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html)。
model_list:
- model_name: claude-haiku-4-5
litellm_params:
model: openai/claude-haiku-4-5 # Kunavo speaks the OpenAI chat shape
api_base: https://api.kunavo.com/v1
api_key: sk-kn-...
additional_drop_params: ["top_p"] # keep temperature, never send top_p在 Claude Opus 4.7 及後續版本中,兩者都不要傳送
較新的模型更進一步。在 Claude Opus 4.7 及後續模型上,將 temperature、top_p 或 top_k 設為任何非預設值都會回傳 400(https://platform.claude.com/docs/en/models/opus-5-5/migration-guide);Claude Sonnet 5 也相同(https://platform.claude.com/docs/en/models/sonnet-5/migration-guide),且此規則延伸至 Claude Fable 5(https://platform.claude.com/docs/en/models/fable-5/migration-guide)和 Fable 5.1(https://platform.claude.com/docs/en/models/fable-5-1/migration-guide)。錯誤訊息有所變更——開發者會看到「`temperature` is deprecated for this model.」——而 Anthropic 的 API 參考文件現在將這三個參數全部標記為已棄用:對於 Claude Opus 4.6 之後發布的模型,為了向後相容,僅接受 temperature 為 1.0 或 top_p 為 0.99 以上,並且無論 top_k 設為何值都會拒絕(https://platform.claude.com/docs/en/api/messages)。Anthropic Python SDK 已在 2026 年 8 月 20 日發布的 v1.0 中,從 Messages 方法移除這些參數(https://platform.claude.com/docs/en/release-notes/api)。
API 版本 2026-10-21:任何 temperature、top_p 或 top_k 都會回傳 400
Anthropic 於 2026 年 10 月 21 日發布 API 版本 2026-10-21,下表中的 SDK 版本預設就會送出這個版本。在該版本下,只要請求帶有 temperature、top_p 或 top_k,無論哪個模型都會回傳 400 錯誤,即使是參數的預設值也一樣。另有兩種請求形式在該版本下同樣會回傳 400 錯誤:直接放在 assistant 訊息中的 document 區塊,以及 user 訊息中帶有非空 citations 陣列的 text 區塊(https://platform.claude.com/docs/en/api/migration-2026-10-21, 2026 年 10 月 22 日讀取)。新版 SDK 已完全沒有這三個參數:在我們 2026 年 10 月 22 日的測試中,Python SDK 2.0.0 的 client.messages.create(temperature=0.2) 在送出任何請求之前就拋出了 TypeError。
Python anthropic 2.0.0
TypeScript @anthropic-ai/sdk 1.0.0
Go anthropic-sdk-go/v2 2.0.0
Java com.anthropic:anthropic-java 3.0.0
C# Anthropic 13.0.0
PHP anthropic-ai/sdk 1.0.0
Ruby anthropic 2.0.0
CLI ant 2.0.0
Earlier versions send anthropic-version: 2023-06-01.移除這三個參數,或讓該請求繼續使用 2023-06-01
移除它們是 Anthropic 給出的做法;在 Claude Opus 4.7 及後續模型上不會有任何變化,因為這些模型本來就只以預設值取樣。在仍接受其他值的模型上(例如 Claude Sonnet 4.6),原本調低 temperature 的請求會失去這個效果,也沒有其他設定可以取代。如果某個請求仰賴非預設值,就讓它繼續使用 API 版本 2023-06-01:Python 和 TypeScript SDK 允許你自行設定 anthropic-version 標頭,而該參數則必須放在請求 body 中送出,因為 SDK 方法已不再接受它。
import anthropic
# The 2.x SDK sends anthropic-version: 2026-10-21; this client keeps 2023-06-01.
client = anthropic.Anthropic(default_headers={"anthropic-version": "2023-06-01"})
msg = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=512,
messages=[{"role": "user", "content": "Classify this support ticket: ..."}],
extra_body={"temperature": 0.2}, # 2.x has no temperature argument
)
print(msg.content[0].text)
# TypeScript 1.x: new Anthropic({ defaultHeaders: { "anthropic-version": "2023-06-01" } })經 Kunavo 時,送往上游的請求仍使用 2023-06-01
Kunavo 的 /v1/messages 不論請求帶的是哪個 anthropic-version 都會接收,並以 2023-06-01 呼叫上游,因此這類請求會依本頁前面說明的舊規則判定。我們在 2026 年 10 月 22 日用 Python SDK 2.0.0 和 2.1.0、TypeScript SDK 1.0.0 和 1.1.0 測試過:它們送出的是 2026-10-21 與 Bearer 形式的金鑰,Kunavo 接受了每一個請求。在 Anthropic 移除取樣參數的 Claude 模型上,Kunavo 會在轉送前刪除 temperature、top_p 與 top_k,並在 X-Kunavo-Dropped-Params 回應標頭中列出刪除了哪些;在其他模型上則原樣轉送。assistant 訊息中的 document 區塊與 user 訊息中的 citations 會依你送出的內容傳往上游。chat completions 參考文件依我們的目錄列出會刪除這些參數的模型,而對任何單一請求,這個標頭都會直接告訴你。新版 SDK 如何傳送金鑰,請見 ANTHROPIC_AUTH_TOKEN 與 ANTHROPIC_API_KEY 的比較指南。
如果你透過 Kunavo 呼叫
對於 Anthropic 已移除取樣功能的 10 個 claude-* 模型——claude-fable-5-2, claude-fable-5-1, claude-fable-5, claude-opus-5-5, claude-opus-5, claude-opus-4-8, claude-opus-4-7, claude-sonnet-5-5, claude-sonnet-5 和 claude-haiku-5-5——無論使用 /v1/chat/completions 或 /v1/messages,Kunavo 都會在請求離開閘道前移除 temperature、top_p 和 top_k。這 10 個模型不會出現此錯誤,但你傳送的值不會生效:請求送往上游時,效果等同於未提供這些參數。使用 claude-opus-4-6、claude-sonnet-4-6 和 claude-haiku-4-5 時,Kunavo 不會移除任何參數:聊天轉譯器會將任何數值型的 temperature、top_p 或 top_k 原樣複製到 Claude 請求中,而 /v1/messages 則會照你送出的內容轉送要求,因此請傳送 temperature 或 top_p,不要兩者都傳送。如果上游拒絕同時提供這些參數,上游的 400 錯誤會連同原始訊息文字傳回給你,不會改用其他管道重試,且記錄費用為零。另一個較新模型會拒絕的請求參數,請參閱 max_tokens 與 max_completion_tokens 指南。
常見問題
我應該在 Claude 上使用 temperature 還是 top_p?
除非有特定理由,否則使用 temperature。Anthropic 的 API 參考文件僅建議進階使用情境使用 top_p;在 Claude Opus 4.7 及後續版本上,兩者都不能設為非預設值。
top_p = 1 算是未設定嗎?
不要指望如此。一名開發者回報,在 Claude Sonnet 4.5 上,top_p 為 1.0 且同時設定 temperature 時仍然收到相同的 400(n8n issue #18304)。請刪除該欄位,而不是將它設為看似中性的值。
為什麼我從未設定 top_p,n8n 或 LangChain 卻會傳送它?
兩者先前都會傳遞預設值。n8n 的 Anthropic Chat Model 節點在 1.114.1 之前會傳送 LangChain 的預設值(topP -1、temperature 0.7),而 LangChain JS 0.3.x 中的 ChatAnthropic 會在你設定的 top_p 旁傳送預設 temperature。請升級至 n8n 1.114.1 或後續版本,以及 @langchain/anthropic 1.3.24 或後續版本。
「`temperature` is deprecated for this model」是什麼意思?
這是 Claude Opus 4.7 及後續版本、Sonnet 5,以及 Fable 5 和 5.1 上更嚴格的規則:這些模型會直接拒絕非預設取樣值。請移除這些參數。透過 Kunavo 時,我們提供的模型會移除這些參數,因此呼叫可以順利通過。
在 Kunavo 上,被拒絕的請求會產生費用嗎?
不會。上游拒絕的請求會以零成本記錄。
升級到 Anthropic Python SDK 2.0 後,為什麼 temperature 會出錯?
因為 Python SDK 2.0.0 和 TypeScript SDK 1.0.0 送出的是 API 版本 2026-10-21,在該版本下,temperature、top_p 和 top_k 在任何模型上都會回傳 400,而且 Python SDK 已完全不接受這些參數。請移除它們;或對需要非預設值的請求自行設定 anthropic-version 標頭,讓它們繼續使用 2023-06-01。