Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions docs/de/features/users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,15 @@ Obergrenzen greifen nach den [Overrides virtueller Modelle](/de/features/virtual
bei jedem Versuch, auch bei Failover-Versuchen, und begrenzen daher auch einen
Effort oder ein `max_tokens`, das ein virtuelles Modell erzwingt oder als
Standard setzt. Sie gelten für Chat Completions, die Responses API und die
Anthropic Messages API; ein Messages-Request innerhalb der Obergrenzen wird
weiterhin nativ an Anthropic weitergeleitet, einer darüber nimmt den
übersetzten Pfad, damit er gesenkt werden kann. Ein Request, den nur Anthropic
lesen kann (etwa mit Server-Tool-Verlauf), hat keinen übersetzten Pfad; bei ihm
werden `max_tokens`, das Thinking-Budget und `output_config.effort` stattdessen
im weitergeleiteten Body gesenkt. Unter einer Obergrenze `none` wird sein
Anthropic Messages API. Obergrenzen nehmen einen Messages-Request nicht von
der nativen Weiterleitung an Anthropic: Würde er ohne sie nativ weitergeleitet
und liegt er über den Obergrenzen, werden `max_tokens`, das Thinking-Budget
und `output_config.effort` im weitergeleiteten Body gesenkt; seine übrigen
Felder und seine `anthropic-beta`-Header bleiben erhalten. Ein Request, der
den Response-Cache, Failover, einen erzwungenen Reasoning-Effort oder
Request-Parameter oder eine Guardrail oder ein Plugin nutzt, das ihn
umschreibt, nimmt wie bisher den übersetzten Pfad und wird dort gesenkt. Unter
einer Obergrenze `none` wird bei einem nativ weitergeleiteten Request das
Thinking mit `thinking: {"type": "disabled"}` ausgeschaltet; ein Modell, das
immer denkt (Fable 5.x, Mythos 5.x, Opus 5.5), erhält stattdessen `low`, wie auf
dem übersetzten Pfad. Bekannte Einschränkung: Sonnet 5.5 lehnt `disabled` ab,
Expand Down
15 changes: 9 additions & 6 deletions docs/features/users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,15 @@ and the capped `max_tokens` bounds what it spends. Claude Opus 4.6 and Sonnet
Ceilings apply after [virtual model overrides](/features/virtual-models#force-a-reasoning-effort),
on every attempt, including failover legs, so they also bound an effort or a
`max_tokens` that a virtual model forces or defaults. They apply to Chat
Completions, the Responses API, and the Anthropic Messages API; a Messages
request within the ceilings is still forwarded natively to Anthropic, and one
above them takes the translated path so it can be lowered. A request only
Anthropic can read (server-tool history, for example) has no translated path,
so its `max_tokens`, thinking budget, and `output_config.effort` are lowered in
the forwarded body instead. Under a `none` ceiling its thinking is turned off
Completions, the Responses API, and the Anthropic Messages API. Ceilings do
not take a Messages request off native forwarding to Anthropic: where it
would be forwarded natively without them, one above the ceilings has its
`max_tokens`, thinking budget, and `output_config.effort` lowered in the
forwarded body, and keeps its other members and its `anthropic-beta` headers.
A request that uses the response cache, failover, a forced reasoning effort or
request parameters, or a guardrail or plugin that rewrites it takes the
translated path as before, and is lowered there. Under a `none` ceiling a
natively forwarded request's thinking is turned off
with `thinking: {"type": "disabled"}`; a model that always thinks (Fable 5.x,
Mythos 5.x, Opus 5.5) gets `low` instead, as on the translated path. Known
limit: Sonnet 5.5 rejects `disabled`, so such a request to it fails with
Expand Down
16 changes: 9 additions & 7 deletions docs/pl/features/users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ Claude Opus 4.6 i Sonnet 4.6 przyjmują `max`, ale nie `xhigh`, więc przy limic
Limity działają po [nadpisaniach modeli wirtualnych](/pl/features/virtual-models#wymuszanie-reasoning-effort),
przy każdej próbie, także przy próbach failover, więc ograniczają też effort
lub `max_tokens`, które model wirtualny wymusza albo ustawia domyślnie. Dotyczą
Chat Completions, Responses API i Anthropic Messages API; request Messages
mieszczący się w limitach jest nadal przekazywany natywnie do Anthropic, a
przekraczający je idzie ścieżką tłumaczoną, żeby można go było obniżyć.
Request, który potrafi odczytać tylko Anthropic (np. z historią narzędzi
serwerowych), nie ma ścieżki tłumaczonej, więc jego `max_tokens`, budżet
thinking i `output_config.effort` są obniżane w przekazywanym body. Przy
limicie `none` jego thinking jest wyłączany przez
Chat Completions, Responses API i Anthropic Messages API. Limity nie
wyłączają natywnego przekazywania requestu Messages do Anthropic: jeśli bez
nich byłby przekazany natywnie, a przekracza limity, jego `max_tokens`, budżet
thinking i `output_config.effort` są obniżane w przekazywanym body, a pozostałe
pola i nagłówki `anthropic-beta` zostają zachowane. Request, który korzysta z
cache odpowiedzi, failover, wymuszonego reasoning effort lub parametrów
requestu albo z guardraila lub pluginu, który go przepisuje, idzie jak dotąd
ścieżką tłumaczoną i tam jest obniżany. Przy limicie `none` thinking requestu
przekazywanego natywnie jest wyłączany przez
`thinking: {"type": "disabled"}`; model, który zawsze myśli (Fable 5.x,
Mythos 5.x, Opus 5.5), dostaje zamiast tego `low`, tak jak na ścieżce
tłumaczonej. Znane ograniczenie: Sonnet 5.5 odrzuca `disabled`, więc taki
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/features/users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ users:

关闭推理的控制项(`false`、零预算、`{"type": "disabled"}`)以及不超过上限的级别会保留。刻度无法定位的值(例如 token 预算或 `true`)视为超过上限:网关会移除客户端的所有推理控制项,改为发送上限作为推理强度。同时关闭推理的请求(例如 `enable_thinking: false` 与 `thinking_budget` 并存)不会因此被开启推理,重复 `type` 且只有最后一次为 `disabled` 的控制对象也是如此:这些控制项同样会被移除,取而代之的是请求在别处指定且不超过上限的最高推理强度,若没有其他推理强度则为 `none`。Messages 的 `budget_tokens` 若高于网关自身为上限级别发送的预算(`low` 为 5000,`medium` 为 10000,`high` 及以上为 20000),会被降到该预算。“从不添加推理强度”只有一个例外:未指定 `output_config.effort` 的 Messages 自适应 thinking(`thinking: {"type": "adaptive"}`)。此时 thinking 已经开启,Anthropic 按 `high` 运行它,因此在更低的上限下会把该上限作为它的推理强度发送。适配器自行填入的值(Anthropic 的默认 `max_tokens` 及其在 thinking 预算之上预留的空间、OpenCode Zen 的默认推理强度)同样保持在上限之内:thinking 预算会缩小到能放进受限的 `max_tokens`,连最小预算都放不下时则关闭 thinking。自适应 thinking(Claude Opus 4.6、Sonnet 4.6 及更新的模型)没有预算:它保持开启,其消耗由受限的 `max_tokens` 约束。Claude Opus 4.6 和 Sonnet 4.6 接受 `max` 但不接受 `xhigh`,因此在 `xhigh` 上限下会向它们发送 `high`。

上限在[虚拟模型覆盖](/zh/features/virtual-models#强制推理强度)之后生效,作用于每一次尝试,包括故障转移尝试,因此也会约束虚拟模型强制或默认设置的推理强度或 `max_tokens`。它们适用于 Chat Completions、Responses API 和 Anthropic Messages API;在上限之内的 Messages 请求仍会原生转发给 Anthropic,超过上限的请求会改走转换路径以便降低。只有 Anthropic 能读取的请求(例如带有服务器工具历史)没有转换路径,因此会直接在转发的请求体中降低其 `max_tokens`、thinking 预算和 `output_config.effort`。在 `none` 上限下,它的 thinking 会通过 `thinking: {"type": "disabled"}` 关闭;始终思考的模型(Fable 5.x、Mythos 5.x、Opus 5.5)则改为收到 `low`,与转换路径一致。已知限制:Sonnet 5.5 会拒绝 `disabled`,因此发往该模型的这类请求会以 Anthropic 的 `400` 失败。原生转发的请求体若重复其中某个成员或以不同大小写书写,到达 Anthropic 时只保留一个:最低的 `max_tokens`,以及最后一个 `thinking` 和 `output_config`。网关无法降到上限之内的请求体会以 `400` 拒绝,绝不会超过上限转发。嵌入和媒体端点不会被改写。[响应缓存](/zh/features/cache)的条目与生效的上限绑定,因此在设置或修改上限之前缓存的回答不会在新上限下返回。
上限在[虚拟模型覆盖](/zh/features/virtual-models#强制推理强度)之后生效,作用于每一次尝试,包括故障转移尝试,因此也会约束虚拟模型强制或默认设置的推理强度或 `max_tokens`。它们适用于 Chat Completions、Responses API 和 Anthropic Messages API。上限不会让 Messages 请求离开对 Anthropic 的原生转发:原本会被原生转发的请求若超过上限,会直接在转发的请求体中降低其 `max_tokens`、thinking 预算和 `output_config.effort`,其余字段和 `anthropic-beta` 请求头会被保留。使用响应缓存、故障转移、强制推理强度或请求参数,或者会被护栏或插件改写的请求,仍像以前一样走转换路径,并在那里被降低。在 `none` 上限下,原生转发的请求的 thinking 会通过 `thinking: {"type": "disabled"}` 关闭;始终思考的模型(Fable 5.x、Mythos 5.x、Opus 5.5)则改为收到 `low`,与转换路径一致。已知限制:Sonnet 5.5 会拒绝 `disabled`,因此发往该模型的这类请求会以 Anthropic 的 `400` 失败。原生转发的请求体若重复其中某个成员或以不同大小写书写,到达 Anthropic 时只保留一个:最低的 `max_tokens`,以及最后一个 `thinking` 和 `output_config`。网关无法降到上限之内的请求体会以 `400` 拒绝,绝不会超过上限转发。嵌入和媒体端点不会被改写。[响应缓存](/zh/features/cache)的条目与生效的上限绑定,因此在设置或修改上限之前缓存的回答不会在新上限下返回。

<Warning>
批处理、Realtime 会话(`/v1/realtime`)和 `/p/` 下的提供商直通**不受**上限约束:它们的请求体会原样发往上游。受限的密钥如果还能使用它们,就可以绕过该策略,因此请为受限的密钥禁止这些端点。[ChatGPT](/zh/providers/chatgpt) 提供商不接受输出 token 上限,因此 `max_output_tokens` 不会在其模型上强制执行。token 上限约束的是每一个回答:要求多个候选回答(`n`)的 Chat Completions 请求,得到的输出可以达到上限的相应倍数。如有需要,请用模型的 [`params`](/zh/features/virtual-models#设置请求参数) 强制设置 `n`。
Expand Down
6 changes: 0 additions & 6 deletions internal/gateway/request_ceilings.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ func ResponsesRequestWithCeilings(req *core.ResponsesRequest, ceilings requestpa
return requestparams.CapResponsesTokens(req, ceilings)
}

// ChatRequestExceedsCeilings reports whether the ceilings would change req.
func ChatRequestExceedsCeilings(req *core.ChatRequest, ceilings requestparams.Ceilings) bool {
capped, err := ChatRequestWithCeilings(req, ceilings)
return err != nil || capped != req
}

// effortWithinCeiling reports whether the efforts a request carries, in any
// spelling, have to be replaced, and the effort that replaces them all. One
// above ceiling is replaced by the ceiling, and so is one the common scale
Expand Down
1 change: 0 additions & 1 deletion internal/gateway/request_ceilings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func TestChatRequestWithCeilingsReasoningEffort(t *testing.T) {
capped, err := ChatRequestWithCeilings(req, requestparams.Ceilings{ReasoningEffort: tt.ceiling})
require.NoError(t, err)

assert.Equal(t, !tt.unchanged, ChatRequestExceedsCeilings(req, requestparams.Ceilings{ReasoningEffort: tt.ceiling}))
if tt.unchanged {
assert.Same(t, req, capped)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/server/messages_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (s *translatedInferenceService) Messages(c *echo.Context) error {
// the translated pipeline can actually carry the request: content it cannot
// represent (server-tool history, …) still belongs upstream verbatim.
unsignedThinking := translateErr == nil && anthropicapi.HasUnsignedThinking(decoded)
if s.canForwardMessagesNatively(ctx, workflow, req, decoded, unsignedThinking, translateErr != nil) {
if s.canForwardMessagesNatively(ctx, workflow, unsignedThinking) {
return s.dispatchMessagesNative(c, prepared, decoded, workflow)
}
if translateErr != nil {
Expand Down
25 changes: 7 additions & 18 deletions internal/server/messages_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/enterpilot/gomodel/ext"
"github.com/enterpilot/gomodel/internal/anthropicapi"
"github.com/enterpilot/gomodel/internal/core"
"github.com/enterpilot/gomodel/internal/gateway"
"github.com/enterpilot/gomodel/internal/requestlog"
"github.com/enterpilot/gomodel/internal/streaming"
)
Expand All @@ -35,10 +34,7 @@ const anthropicProviderType = "anthropic"
// requests using guardrails patching, response caching, failover, or a forced
// reasoning effort stay on the translated pipeline. So does a request replaying an unsigned thinking
// block, which Anthropic rejects and only the translated pipeline can drop.
// req is the client's request as translated, before any override, and
// decoded the request as sent. nativeOnly marks a request the translated
// pipeline cannot carry.
func (s *translatedInferenceService) canForwardMessagesNatively(ctx context.Context, workflow *core.Workflow, req *core.ChatRequest, decoded *anthropicapi.MessagesRequest, unsignedThinking, nativeOnly bool) bool {
func (s *translatedInferenceService) canForwardMessagesNatively(ctx context.Context, workflow *core.Workflow, unsignedThinking bool) bool {
if workflow == nil || strings.TrimSpace(workflow.ProviderType) != anthropicProviderType {
return false
}
Expand All @@ -58,19 +54,12 @@ func (s *translatedInferenceService) canForwardMessagesNatively(ctx context.Cont
return false
}
// A forced reasoning effort or request parameters rewrite the request,
// which the verbatim body cannot carry. Ceilings rewrite only a request
// that exceeds them: one already within them is forwarded as it is. The
// translation keeps only a coarse level of the thinking config (a budget's
// band, "medium" for adaptive thinking that Anthropic runs at "high"), so
// the config itself is checked too, as the forwarded body would carry it;
// the translated pipeline then sends the ceiling level. A request only
// the native path can carry stays on it above the ceilings too:
// dispatchMessagesNative lowers its body to them.
overrides := s.inference().RequestOverrides(ctx, workflow)
if overrides.Forces() {
return false
}
if !nativeOnly && (gateway.ChatRequestExceedsCeilings(req, overrides.Ceilings) || messagesExceedCeilings(decoded, nativeMessagesCeilings(workflow, overrides.Ceilings), modelAlwaysThinks(workflow))) {
// which the verbatim body cannot carry. Ceilings do not take a request off
// the native path: dispatchMessagesNative lowers max_tokens, the thinking
// config and output_config.effort where they stand in the body, so one
// above them keeps its anthropic-beta headers and every member the
// translation does not model (metadata, context_management).
if s.inference().RequestOverrides(ctx, workflow).Forces() {
return false
}
_, ok := s.provider.(core.RoutablePassthrough)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/plugin_phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestCanForwardMessagesNatively_DisabledByPostResponsePlugins(t *testing.T)
chains := phaseChains(t, map[string]string{"response": "warn"}, guardrails.StepReference{Ref: "phase", Phase: pluginapi.KindResponse, Step: 1})
svc := &translatedInferenceService{provider: &capturingProvider{}, pluginChains: staticChainsResolver{chains: chains}}
workflow := &core.Workflow{ProviderType: anthropicProviderType}
require.False(t, svc.canForwardMessagesNatively(context.Background(), workflow, &core.ChatRequest{}, nil, false, false))
require.False(t, svc.canForwardMessagesNatively(context.Background(), workflow, false))

svc.pluginChains = staticChainsResolver{chains: &plugins.Chains{}}
require.False(t, svc.hasPostResponsePlugins(context.Background()))
Expand Down
5 changes: 2 additions & 3 deletions internal/server/user_path_ceilings_upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ func TestCeilingHoldsThroughAnthropicThinkingRoom(t *testing.T) {
}
}

// Native Messages forwarding sends the client's body verbatim, so an explicit
// thinking budget above the ceiling level's own budget must send the request
// through the translated pipeline, which sends that level's budget.
// An explicit thinking budget above the ceiling level's own budget is lowered
// to that level's budget in the body native Messages forwarding sends.
func TestCeilingBoundsNativeMessagesThinkingBudget(t *testing.T) {
tests := []struct {
name string
Expand Down
11 changes: 4 additions & 7 deletions internal/server/user_path_native_ceilings_upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -145,14 +144,12 @@ func TestCeilingLowersAPerMessageEffortOfANativeMessagesBody(t *testing.T) {
assert.Equal(t, `{"model":"claude-opus-5",`+body("low"), string(f.upstreams["anthropic"].Last(t).Body))
})

t.Run("a request the translated pipeline lowers carries no per-message effort", func(t *testing.T) {
t.Run("with a top-level effort above the ceiling in a body the pipeline could carry", func(t *testing.T) {
f := newUpstreamFixture(t, cappedRows, nil)
rec := f.post(t, "/low", "/v1/messages", `{"model":"anthropic/claude-opus-5","max_tokens":512,"output_config":{"effort":"max"},`+effortTurns("max")+`}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())

sent := string(f.upstreams["anthropic"].Last(t).Body)
assert.Equal(t, 1, strings.Count(sent, `"effort"`), sent)
assert.Equal(t, "low", object(t, f.sent(t, "anthropic"), "output_config")["effort"])
assert.Equal(t, `{"model":"claude-opus-5","max_tokens":512,"output_config":{"effort":"low"},`+effortTurns("low")+`}`, string(f.upstreams["anthropic"].Last(t).Body))
})
}

Expand All @@ -167,11 +164,11 @@ func TestXHighCeilingSendsHighToAModelWithoutThatLevel(t *testing.T) {
want string
}{
{name: "chat completions", path: "/v1/chat/completions", body: `{"model":"anthropic/claude-opus-4-6","reasoning_effort":"max",` + hiMessages + `}`, want: "high"},
{name: "messages, lowered by the translated pipeline", path: "/v1/messages", body: `{"model":"anthropic/claude-opus-4-6","max_tokens":512,"output_config":{"effort":"max"},` + hiMessages + `}`, want: "high"},
{name: "messages, lowered in the forwarded body", path: "/v1/messages", body: `{"model":"anthropic/claude-opus-4-6","max_tokens":512,"output_config":{"effort":"max"},` + hiMessages + `}`, want: "high"},
{name: "messages, lowered in a native-only body", path: "/v1/messages", body: `{"model":"anthropic/claude-opus-4-6","max_tokens":512,"output_config":{"effort":"max"},` + serverToolHistory + `}`, want: "high"},
{name: "messages within the ceiling", path: "/v1/messages", body: `{"model":"anthropic/claude-opus-4-6","max_tokens":512,"output_config":{"effort":"high"},` + hiMessages + `}`, want: "high"},
{name: "sonnet 4.6, chat completions", path: "/v1/chat/completions", body: `{"model":"anthropic/claude-sonnet-4-6","reasoning_effort":"max",` + hiMessages + `}`, want: "high"},
{name: "sonnet 4.6, messages lowered by the translated pipeline", path: "/v1/messages", body: `{"model":"anthropic/claude-sonnet-4-6","max_tokens":512,"output_config":{"effort":"max"},` + hiMessages + `}`, want: "high"},
{name: "sonnet 4.6, messages lowered in the forwarded body", path: "/v1/messages", body: `{"model":"anthropic/claude-sonnet-4-6","max_tokens":512,"output_config":{"effort":"max"},` + hiMessages + `}`, want: "high"},
{name: "sonnet 4.6, messages lowered in a native-only body", path: "/v1/messages", body: `{"model":"anthropic/claude-sonnet-4-6","max_tokens":512,"output_config":{"effort":"max"},` + serverToolHistory + `}`, want: "high"},
{name: "a model with the level gets it", path: "/v1/chat/completions", body: `{"model":"anthropic/claude-opus-5","reasoning_effort":"max",` + hiMessages + `}`, want: "xhigh"},
{name: "a model with the level gets it in a native-only body", path: "/v1/messages", body: `{"model":"anthropic/claude-opus-5","max_tokens":512,"output_config":{"effort":"max"},` + serverToolHistory + `}`, want: "xhigh"},
Expand Down
Loading