Skip to content

Commit 036ba37

Browse files
committed
fix(relaykit): match Claude effort and Gemini 2.5 budgets to each model
RelayKit sent some Claude effort levels that the target model does not accept. A request with an effort but no thinking reached Opus 4.5 with max or xhigh. Sonnet 4.5, Haiku 4.5 and other models without effort support received an effort anyway. When a model-name suffix or modifier turned thinking off, the client's output_config.effort stayed in the request unchecked. Opus 5 and Haiku 5.5 then got disabled thinking at xhigh or max, and Sonnet 5.5 got between_tools at those levels. The API rejects these combinations with a 400. Known Claude models now get the nearest effort level they accept, and no effort when they accept none. Effort works without thinking, so a thinking-off request keeps the client's effort within those limits. On Opus 5, Haiku 5.5 and Sonnet 5.5 it is capped at high. Fable, Mythos and Mythos Preview are now marked as taking effort. These models cannot turn thinking off, so a thinking-off request runs them at low effort, as Opus 5.5 already does. The new reasoning.ClaudeEffort returns the effort a model accepts for a requested level. Native requests without a suffix or modifier still pass through unchanged, and models RelayKit does not know keep the effort they were sent. Claude Haiku 5.5 joins the capability table: adaptive thinking only, on by default, all five effort levels, and medium as the default effort. Converted requests no longer send it manual thinking with budget_tokens, which it rejects. They also drop temperature, top_p and top_k, which it accepts only at their defaults. Gemini 2.5 Pro takes thinking budgets up to 32768, but xhigh and max were rendered as 24576. They now get the model's largest budget: 32768 on 2.5 Pro, and 24576 on 2.5 Flash and Flash-Lite as before. high stays at 24576, as in Google's OpenAI-compatible mapping.
1 parent 0f38a14 commit 036ba37

4 files changed

Lines changed: 114 additions & 26 deletions

File tree

‎relaykit/relayconvert/internal/shared/claude/reasoning.go‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func ApplyReasoning(ctx context.Context, req *dto.ClaudeRequest, info convmeta.M
8181
intent.Effort = reasoning.EffortHigh
8282
}
8383
}
84+
requestEffort, err := reasoning.ParseEffort(req.GetEfforts())
85+
if err != nil {
86+
return err
87+
}
88+
if knownClaudeModel && intent.Mode == reasoning.ModeDisabled && (intent.Effort == "" || intent.Effort == reasoning.EffortNone) &&
89+
requestEffort != "" && requestEffort != reasoning.EffortNone {
90+
// Effort works without thinking, so the request's own effort is
91+
// rendered for the thinking-off state rather than left unchecked.
92+
intent.Effort = requestEffort
93+
}
8494
if req.MaxTokens == nil && intent.HasStrength() {
8595
// Adapter-provided defaults may be raised to accommodate an exact
8696
// cross-protocol budget. Values too small for manual thinking are
@@ -117,7 +127,14 @@ func ApplyReasoning(ctx context.Context, req *dto.ClaudeRequest, info convmeta.M
117127
if rendered.Thinking != nil {
118128
req.Thinking = rendered.Thinking
119129
}
120-
if rendered.OutputEffort != "" {
130+
outputEffort := rendered.OutputEffort
131+
// An effort the request still carries reaches a known model only at a
132+
// level it accepts, and is removed where the model takes none.
133+
rewriteRequestEffort := outputEffort == "" && knownClaudeModel && requestEffort != ""
134+
if rewriteRequestEffort {
135+
outputEffort = reasoning.ClaudeEffort(capabilityModel, requestEffort)
136+
}
137+
if outputEffort != "" || rewriteRequestEffort {
121138
outputConfig := make(map[string]any)
122139
if len(req.OutputConfig) > 0 {
123140
if kitutil.GetJsonType(req.OutputConfig) != "object" {
@@ -130,12 +147,20 @@ func ApplyReasoning(ctx context.Context, req *dto.ClaudeRequest, info convmeta.M
130147
outputConfig = make(map[string]any)
131148
}
132149
}
133-
outputConfig["effort"] = string(rendered.OutputEffort)
134-
encoded, err := kitutil.Marshal(outputConfig)
135-
if err != nil {
136-
return fmt.Errorf("failed to marshal Claude output_config: %w", err)
150+
if outputEffort != "" {
151+
outputConfig["effort"] = string(outputEffort)
152+
} else {
153+
delete(outputConfig, "effort")
154+
}
155+
if len(outputConfig) == 0 {
156+
req.OutputConfig = nil
157+
} else {
158+
encoded, err := kitutil.Marshal(outputConfig)
159+
if err != nil {
160+
return fmt.Errorf("failed to marshal Claude output_config: %w", err)
161+
}
162+
req.OutputConfig = encoded
137163
}
138-
req.OutputConfig = encoded
139164
}
140165
if rendered.ClearSampling {
141166
if req.Temperature != nil || req.TopP != nil || req.TopK != nil {

‎relaykit/relayconvert/reasoning/claude.go‎

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ type claudeCapabilities struct {
3333
supportsXHigh bool
3434
supportsMax bool
3535
strictSampling bool
36+
// offEffortAtMostHigh marks models that accept effort high or below only
37+
// while thinking is off (disabled or between_tools).
38+
offEffortAtMostHigh bool
3639
// defaultEffort is the effort the model runs at when a request sets none:
37-
// medium on Claude Opus 5.5 and high on every other model
40+
// medium on Claude Opus 5.5 and Haiku 5.5 and high on every other model
3841
// (https://platform.claude.com/docs/en/build-with-claude/effort).
3942
defaultEffort Effort
4043
}
@@ -50,24 +53,27 @@ func claudeCapabilitiesFor(model string) claudeCapabilities {
5053
capabilities.supportsManual = false
5154
capabilities.defaultThinking = true
5255
capabilities.supportsDisable = false
56+
capabilities.supportsEffort = true
5357
capabilities.supportsXHigh = true
5458
capabilities.supportsMax = true
5559
capabilities.strictSampling = true
5660
case strings.HasPrefix(model, "claude-mythos-preview"):
5761
capabilities.adaptive = true
5862
capabilities.defaultThinking = true
5963
capabilities.supportsDisable = false
64+
capabilities.supportsEffort = true
6065
capabilities.supportsMax = true
6166
capabilities.strictSampling = true
6267
case strings.HasPrefix(model, "claude-opus-5-5"),
6368
strings.HasPrefix(model, "claude-sonnet-5-5"):
6469
// Opus 5.5 cannot turn thinking off at all; Sonnet 5.5 turns it off
65-
// with between_tools.
70+
// with between_tools, which takes effort high or below only.
6671
capabilities.adaptive = true
6772
capabilities.supportsManual = false
6873
capabilities.defaultThinking = true
6974
capabilities.supportsDisable = false
7075
capabilities.disableAsBetweenTools = strings.HasPrefix(model, "claude-sonnet-5-5")
76+
capabilities.offEffortAtMostHigh = capabilities.disableAsBetweenTools
7177
if strings.HasPrefix(model, "claude-opus-5-5") {
7278
capabilities.defaultEffort = EffortMedium
7379
}
@@ -77,13 +83,19 @@ func claudeCapabilitiesFor(model string) claudeCapabilities {
7783
capabilities.strictSampling = true
7884
case strings.HasPrefix(model, "claude-opus-5"),
7985
strings.HasPrefix(model, "claude-sonnet-5"),
86+
strings.HasPrefix(model, "claude-haiku-5"),
8087
strings.HasPrefix(model, "claude-opus-4-8"),
8188
strings.HasPrefix(model, "claude-opus-4-7"):
8289
capabilities.adaptive = true
8390
capabilities.supportsManual = false
84-
if strings.HasPrefix(model, "claude-opus-5") || strings.HasPrefix(model, "claude-sonnet-5") {
91+
if strings.HasPrefix(model, "claude-opus-5") || strings.HasPrefix(model, "claude-sonnet-5") || strings.HasPrefix(model, "claude-haiku-5") {
8592
capabilities.defaultThinking = true
8693
}
94+
// Opus 5 and Haiku 5.5 reject disabled thinking with xhigh or max.
95+
capabilities.offEffortAtMostHigh = strings.HasPrefix(model, "claude-opus-5") || strings.HasPrefix(model, "claude-haiku-5")
96+
if strings.HasPrefix(model, "claude-haiku-5") {
97+
capabilities.defaultEffort = EffortMedium
98+
}
8799
capabilities.supportsEffort = true
88100
capabilities.supportsXHigh = true
89101
capabilities.supportsMax = true
@@ -120,12 +132,6 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
120132
diagnostics = append(diagnostics, normalizeDiagnostics...)
121133
}
122134
capabilities := claudeCapabilitiesFor(model)
123-
if disabledWithEffort {
124-
diagnostics = append(diagnostics, claudeReasoningDiagnostic(
125-
"claude_disabled_effort_ignored",
126-
fmt.Sprintf("model %q cannot apply effort %q while thinking is disabled; the effort was ignored", model, intent.Effort),
127-
))
128-
}
129135
if !intent.HasStrength() {
130136
if intent.IncludeThoughts != nil && capabilities.adaptive && capabilities.defaultThinking {
131137
thinking := &dto.Thinking{Type: "adaptive"}
@@ -147,15 +153,27 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
147153
}
148154

149155
if intent.Mode == ModeDisabled || intent.Effort == EffortNone {
156+
// Effort works without thinking, so a requested level is sent at one
157+
// the model accepts while thinking is off.
158+
outputEffort := Effort("")
159+
if disabledWithEffort && (capabilities.supportsDisable || capabilities.disableAsBetweenTools) {
160+
outputEffort = ClaudeEffort(model, intent.Effort)
161+
if capabilities.offEffortAtMostHigh && (outputEffort == EffortXHigh || outputEffort == EffortMax) {
162+
outputEffort = EffortHigh
163+
}
164+
diagnostics = append(diagnostics, claudeEffortDiagnostics(model, intent.Effort, outputEffort)...)
165+
}
150166
if capabilities.disableAsBetweenTools {
151167
// between_tools takes no other thinking field and runs at the
152-
// default effort, which is within its "high or below" limit.
168+
// default effort or the requested one, both within its "high or
169+
// below" limit.
153170
diagnostics = append(diagnostics, claudeReasoningDiagnostic(
154171
"claude_thinking_disable_as_between_tools",
155172
fmt.Sprintf("model %q rejects disabled thinking; using between_tools, its lowest thinking setting", model),
156173
))
157174
return ClaudeRender{
158175
Thinking: &dto.Thinking{Type: "between_tools"},
176+
OutputEffort: outputEffort,
159177
EffectiveEffort: EffortNone,
160178
ClearSampling: capabilities.strictSampling,
161179
Diagnostics: diagnostics,
@@ -175,7 +193,6 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
175193
thinking.Display = "omitted"
176194
}
177195
}
178-
outputEffort := Effort("")
179196
effectiveEffort := capabilities.defaultEffort
180197
if capabilities.supportsEffort {
181198
outputEffort = EffortLow
@@ -193,6 +210,7 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
193210
}
194211
return ClaudeRender{
195212
Thinking: &dto.Thinking{Type: "disabled"},
213+
OutputEffort: outputEffort,
196214
EffectiveEffort: EffortNone,
197215
ClearSampling: capabilities.strictSampling,
198216
Diagnostics: diagnostics,
@@ -268,7 +286,18 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
268286
}
269287
}
270288
if intent.Mode == ModeUnset {
271-
return ClaudeRender{OutputEffort: intent.Effort, EffectiveEffort: intent.Effort, Diagnostics: diagnostics}, nil
289+
// Known models get the effort they accept. They think only when asked,
290+
// so a removed effort leaves none.
291+
effort := intent.Effort
292+
if isKnownClaudeModel(model) {
293+
effort = ClaudeEffort(model, intent.Effort)
294+
diagnostics = append(diagnostics, claudeEffortDiagnostics(model, intent.Effort, effort)...)
295+
}
296+
effectiveEffort := effort
297+
if effectiveEffort == "" {
298+
effectiveEffort = EffortNone
299+
}
300+
return ClaudeRender{OutputEffort: effort, EffectiveEffort: effectiveEffort, Diagnostics: diagnostics}, nil
272301
}
273302
var raisedMaxTokens *uint
274303
if maxTokens == nil || *maxTokens <= 1024 {
@@ -326,9 +355,8 @@ func RenderClaude(model string, intent Intent, maxTokens *uint, adapterBudgetPer
326355
} else if effectiveEffort == "" {
327356
effectiveEffort = EffortFromBudget(budget)
328357
}
329-
outputEffort := Effort("")
330-
if capabilities.supportsEffort && intent.Effort != "" {
331-
outputEffort = normalizeClaudeEffort(intent.Effort, capabilities)
358+
outputEffort := ClaudeEffort(model, intent.Effort)
359+
if outputEffort != "" {
332360
effectiveEffort = outputEffort
333361
}
334362
thinking := &dto.Thinking{Type: "enabled", BudgetTokens: &budget}
@@ -359,6 +387,25 @@ func claudeReasoningDiagnostic(code string, message string) types.ConversionDiag
359387
}
360388
}
361389

390+
// claudeEffortDiagnostics reports a requested effort that is sent at another
391+
// level or removed because the model takes none.
392+
func claudeEffortDiagnostics(model string, requested Effort, sent Effort) []types.ConversionDiagnostic {
393+
switch {
394+
case requested == "" || requested == EffortNone || requested == sent:
395+
return nil
396+
case sent == "":
397+
return []types.ConversionDiagnostic{claudeReasoningDiagnostic(
398+
"claude_effort_unsupported",
399+
fmt.Sprintf("model %q does not support effort %q; the effort was removed", model, requested),
400+
)}
401+
default:
402+
return []types.ConversionDiagnostic{claudeReasoningDiagnostic(
403+
"claude_effort_adjusted",
404+
fmt.Sprintf("model %q does not support effort %q with the selected thinking mode; using %q", model, requested, sent),
405+
)}
406+
}
407+
}
408+
362409
// ClaudeUsesManualThinking reports whether an exact numeric budget is rendered
363410
// as legacy extended thinking rather than being reduced to adaptive effort.
364411
func ClaudeUsesManualThinking(model string, intent Intent) bool {
@@ -376,6 +423,17 @@ func ClaudeDefaultEffort(model string) Effort {
376423
return claudeCapabilitiesFor(model).defaultEffort
377424
}
378425

426+
// ClaudeEffort returns the effort level the model accepts in place of the
427+
// requested one. It returns empty when no effort is requested or the model
428+
// takes none, which includes models missing from the capability table.
429+
func ClaudeEffort(model string, effort Effort) Effort {
430+
capabilities := claudeCapabilitiesFor(model)
431+
if !capabilities.supportsEffort || effort == "" || effort == EffortNone {
432+
return ""
433+
}
434+
return normalizeClaudeEffort(effort, capabilities)
435+
}
436+
379437
// ResolveClaudeDefault makes the Claude model's default thinking explicit for
380438
// a non-Claude target: no intent becomes adaptive thinking on models that
381439
// think by default, and thinking without a strength takes the model's

‎relaykit/relayconvert/reasoning/gemini.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func RenderGemini(model string, intent Intent, maxOutputTokens *uint, adapterBud
145145
}
146146
hasBudget = true
147147
} else if intent.Effort != "" {
148-
budget = gemini25BudgetForEffort(intent.Effort)
148+
budget = gemini25BudgetForEffort(intent.Effort, capabilities.maxBudget)
149149
hasBudget = true
150150
} else if intent.Mode != ModeUnset && maxOutputTokens != nil && *maxOutputTokens > 0 {
151151
if uint64(*maxOutputTokens) > uint64(math.MaxInt) {
@@ -295,7 +295,7 @@ func NormalizeGeminiThinkingConfig(model string, generation *dto.GeminiChatGener
295295

296296
if capabilities.kind == geminiThinkingBudget {
297297
if config.ThinkingLevel != "" {
298-
budget := gemini25BudgetForEffort(intent.Effort)
298+
budget := gemini25BudgetForEffort(intent.Effort, capabilities.maxBudget)
299299
diagnostics = append(diagnostics, geminiReasoningDiagnostic(
300300
"gemini_level_to_budget",
301301
fmt.Sprintf("model %q uses thinkingBudget; thinkingLevel %q was converted to budget %d", model, config.ThinkingLevel, budget),
@@ -476,14 +476,19 @@ func EquivalentGeminiStrength(model string, left Intent, right Intent) (bool, er
476476
return leftConfig.ThinkingBudget == nil || *leftConfig.ThinkingBudget == *rightConfig.ThinkingBudget, nil
477477
}
478478

479-
func gemini25BudgetForEffort(effort Effort) int {
479+
// gemini25BudgetForEffort follows Google's OpenAI-compatible mapping, where
480+
// high is 24576; xhigh and max, which that mapping lacks, take the model's
481+
// largest budget.
482+
func gemini25BudgetForEffort(effort Effort, maxBudget int) int {
480483
switch effort {
481484
case EffortMinimal, EffortLow:
482485
return 1024
483486
case EffortMedium:
484487
return 8192
485-
case EffortHigh, EffortXHigh, EffortMax:
488+
case EffortHigh:
486489
return 24576
490+
case EffortXHigh, EffortMax:
491+
return maxBudget
487492
default:
488493
return 0
489494
}

‎relaykit/relayconvert/reasoning/suffix.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func isKnownClaudeModel(modelName string) bool {
161161
}
162162
knownPrefixes := []string{
163163
"claude-fable-5", "claude-mythos-5", "claude-mythos-preview",
164-
"claude-opus-5", "claude-sonnet-5", "claude-opus-4-8",
164+
"claude-opus-5", "claude-sonnet-5", "claude-haiku-5", "claude-opus-4-8",
165165
"claude-opus-4-7", "claude-opus-4-6", "claude-sonnet-4-6",
166166
"claude-opus-4-5", "claude-sonnet-4-5", "claude-haiku-4-5",
167167
"claude-opus-4-1", "claude-opus-4-", "claude-sonnet-4-",

0 commit comments

Comments
 (0)