Repository navigation
feat: openrouter format for claude request - #1235
Conversation
WalkthroughThis update enhances the Changes
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
relay/channel/claude/relay-claude.go(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
relay/channel/claude/relay-claude.go (3)
dto/openai_request.go (1)
Reasoning(623-626)relay/channel/openrouter/dto.go (1)
RequestReasoning(3-9)dto/claude.go (1)
Thinking(179-182)
| if textRequest.Reasoning != nil { | ||
| var reasoning openrouter.RequestReasoning | ||
| if err := json.Unmarshal(textRequest.Reasoning, &reasoning); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| budgetTokens := reasoning.MaxTokens | ||
| if budgetTokens > 0 { | ||
| claudeRequest.Thinking = &dto.Thinking{ | ||
| Type: "enabled", | ||
| BudgetTokens: budgetTokens, | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Reasoning handling ignores effort, exclude, and mutual-exclusion rules
Good to see the OpenRouter reasoning field being parsed, but a couple of gaps remain:
- The OpenRouter spec states that exactly one of
effortormax_tokensshould be set. We silently accept invalid payloads (effort+max_tokensor none) instead of validating and returning a 400. effort(low/medium/high) andexcludeare discarded. Consumers expecting Claude’s extended‐thinking behaviour driven byeffort, or expecting the reasoning content to be suppressed whenexclude=true, will get surprising results.- This block runs after the “-thinking” adapter logic, so a
reasoning.max_tokensfield will overwrite the budget calculated earlier, whereas aneffortfield will not. The precedence feels accidental – consider parsingReasoningbefore the adapter section and using its values when calculating the budget.
Minimal validation + support for effort/exclude:
+// validate and map openrouter reasoning
+if textRequest.Reasoning != nil {
+ var r openrouter.RequestReasoning
+ if err := json.Unmarshal(textRequest.Reasoning, &r); err != nil {
+ return nil, err
+ }
+ // spec: exactly one of effort/max_tokens
+ if (r.Effort != "" && r.MaxTokens > 0) || (r.Effort == "" && r.MaxTokens == 0) {
+ return nil, fmt.Errorf("invalid reasoning payload: set either effort or max_tokens")
+ }
+
+ // derive budget
+ budget := r.MaxTokens
+ if budget == 0 { // use effort mapping if provided
+ switch strings.ToLower(r.Effort) {
+ case "low":
+ budget = 1024
+ case "medium":
+ budget = 4096
+ case "high":
+ budget = 8192
+ }
+ }
+
+ claudeRequest.Thinking = &dto.Thinking{
+ Type: "enabled",
+ BudgetTokens: budget,
+ }
+ if r.Exclude {
+ // implementation-specific: propagate a flag or strip reasoning later
+ }
+}This keeps behaviour predictable and aligned with OpenRouter’s contract.
🏁 Script executed:
#!/bin/bash
# Locate the relay-claude.go file
fd relay-claude.go
# Show context around the Reasoning handling (lines ~120–140)
rg -n 'textRequest\.Reasoning' -A5 -B5 relay/channel/claude/relay-claude.go
# Find all earlier occurrences of Thinking setup (to see adapter logic)
rg -n 'claudeRequest\.Thinking' -C3 relay/channel/claude/relay-claude.goLength of output: 1146
Validate and Apply Reasoning Fields Before Adapter Logic
The current code at lines 112–117 and 126–137
- Unconditionally applies a “-thinking” adapter budget first (
claudeRequest.Thinkingat line 114) - Parses
textRequest.Reasoningonly afterward, ignoresEffortandExclude, and only usesMaxTokensif > 0 - Allows invalid combos (both or neither of
effort/max_tokens) without error
This leads to unpredictable precedence and silent drops of effort/exclude. To align with the OpenRouter spec:
- Move
Reasoningparsing & validation before the adapter logic - Enforce exactly one of
r.Effortorr.MaxTokens(else return 400) - Map
effort→ token budget (low=1024,medium=4096,high=8192) whenMaxTokens==0 - Honor
r.Excludeby propagating a flag or suppressing reasoning output - Only if
textRequest.Reasoning==nilfall back to the adapter‐computed budget
Example diff:
- // BudgetTokens 为 max_tokens 的 80%
- claudeRequest.Thinking = &dto.Thinking{
- Type: "enabled",
- BudgetTokens: int(float64(claudeRequest.MaxTokens) * model_setting.GetClaudeSettings().ThinkingAdapterBudgetTokensPercentage),
- }
+ // 1) parse + validate OpenRouter reasoning
+ if textRequest.Reasoning != nil {
+ var r openrouter.RequestReasoning
+ if err := json.Unmarshal(textRequest.Reasoning, &r); err != nil {
+ return nil, err
+ }
+ // spec: exactly one of effort or max_tokens
+ if (r.Effort != "" && r.MaxTokens > 0) || (r.Effort == "" && r.MaxTokens == 0) {
+ return nil, fmt.Errorf("invalid reasoning payload: set either effort or max_tokens")
+ }
+
+ // derive budget from max_tokens or effort
+ budget := r.MaxTokens
+ if budget == 0 {
+ switch strings.ToLower(r.Effort) {
+ case "low":
+ budget = 1024
+ case "medium":
+ budget = 4096
+ case "high":
+ budget = 8192
+ }
+ }
+
+ claudeRequest.Thinking = &dto.Thinking{
+ Type: "enabled",
+ BudgetTokens: budget,
+ }
+ if r.Exclude {
+ // TODO: implement exclusion behavior
+ }
+ } else {
+ // 2) fall back to adapter‐calculated budget
+ claudeRequest.Thinking = &dto.Thinking{
+ Type: "enabled",
+ BudgetTokens: int(float64(claudeRequest.MaxTokens) *
+ model_setting.GetClaudeSettings().ThinkingAdapterBudgetTokensPercentage),
+ }
+ }This ensures mutual‐exclusion, full support for effort/exclude, and consistent precedence.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if textRequest.Reasoning != nil { | |
| var reasoning openrouter.RequestReasoning | |
| if err := json.Unmarshal(textRequest.Reasoning, &reasoning); err != nil { | |
| return nil, err | |
| } | |
| budgetTokens := reasoning.MaxTokens | |
| if budgetTokens > 0 { | |
| claudeRequest.Thinking = &dto.Thinking{ | |
| Type: "enabled", | |
| BudgetTokens: budgetTokens, | |
| } | |
| } | |
| } | |
| // 1) parse + validate OpenRouter reasoning | |
| if textRequest.Reasoning != nil { | |
| var r openrouter.RequestReasoning | |
| if err := json.Unmarshal(textRequest.Reasoning, &r); err != nil { | |
| return nil, err | |
| } | |
| // spec: exactly one of effort or max_tokens | |
| if (r.Effort != "" && r.MaxTokens > 0) || (r.Effort == "" && r.MaxTokens == 0) { | |
| return nil, fmt.Errorf("invalid reasoning payload: set either effort or max_tokens") | |
| } | |
| // derive budget from max_tokens or effort | |
| budget := r.MaxTokens | |
| if budget == 0 { | |
| switch strings.ToLower(r.Effort) { | |
| case "low": | |
| budget = 1024 | |
| case "medium": | |
| budget = 4096 | |
| case "high": | |
| budget = 8192 | |
| } | |
| } | |
| claudeRequest.Thinking = &dto.Thinking{ | |
| Type: "enabled", | |
| BudgetTokens: budget, | |
| } | |
| if r.Exclude { | |
| // TODO: implement exclusion behavior | |
| } | |
| } else { | |
| // 2) fall back to adapter‐calculated budget (80% of MaxTokens) | |
| claudeRequest.Thinking = &dto.Thinking{ | |
| Type: "enabled", | |
| BudgetTokens: int(float64(claudeRequest.MaxTokens) * | |
| model_setting.GetClaudeSettings().ThinkingAdapterBudgetTokensPercentage), | |
| } | |
| } |
🤖 Prompt for AI Agents
In relay/channel/claude/relay-claude.go around lines 112 to 139, the current
handling of textRequest.Reasoning parses and applies MaxTokens after the
"-thinking" adapter logic, ignores Effort and Exclude fields, and does not
validate that exactly one of Effort or MaxTokens is set. To fix this, move the
parsing and validation of Reasoning before the adapter logic, enforce that
exactly one of Effort or MaxTokens is provided (return a 400 error if not), map
Effort values (low=1024, medium=4096, high=8192) to token budgets when MaxTokens
is zero, handle the Exclude flag appropriately (e.g., suppress reasoning output
or propagate a flag), and only use the adapter-computed budget if Reasoning is
nil. This will ensure correct precedence, validation, and full support for the
Reasoning fields per the OpenRouter spec.
feat: openrouter format for claude request
feat: openrouter format for claude request
feat: openrouter format for claude request
之前的 PR: #1112
#983 实现了 openrouter 的 reasoning 字段透传和
/messages -> openrouter,没有实现/chat/completions -> claude这个 pr 支持 openrouter 格式请求
"reasoning": {"max_tokens": 16000},转换成 ClaudeRequestSummary by CodeRabbit