magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Routing group: a rule's use-effort is ignored for Anthropic requests without thinking (output_config.effort wins, or the budget is cut by max_tokens)

Open Dunqing opened this issue 2 weeks ago • 1 comments

magpie version

0.1.4623 (latest; same code in 0.1.4582)

Where

Gateway only

OS

macOS 27 (arm64)

Agent, provider and model

Anthropic Messages requests (curl; Claude Code's side requests have the same shape) → local magpie gateway → an auto routing group whose intent rules set use-effort:

4 intent "code review, …" → group/strong at xhigh        (served by Opus 5.5 over Anthropic Messages)
9 intent "an easy task, …" → opencode-go/glm-5.3-flash at low   (Chat protocol)

Steps to reproduce

Summary. A rule's use-effort is applied only when the request already has thinking.

  • A request with output_config.effort but no thinking goes out at the request's own effort.
  • A request with no reasoning fields at all goes out at whatever level the thinking budget reads back as after it is cut to max_tokens - 1.

magpie group rule help says use-effort is "the reasoning is asked for on the requests the rule sends it, whatever the agent asked", and the code comment where it is applied says "asked even of a request that asked for no reasoning".

Send three requests that differ only in their reasoning fields. Each uses max_tokens: 8192, one user message and a fresh x-session-id. Read the route right after each one:

SID=$(uuidgen | tr 'A-Z' 'a-z')
curl -sS http://127.0.0.1:3425/v1/messages \
  -H 'content-type: application/json' -H 'anthropic-version: 2023-06-01' \
  -H "x-api-key: $MAGPIE_KEY" -H "x-session-id: $SID" \
  -d '{"model":"group/smart-split","max_tokens":8192,"output_config":{"effort":"high"},
       "messages":[{"role":"user","content":"What is 2 + 2? Reply with just the number."}]}' >/dev/null
curl -sS "http://127.0.0.1:3425/v1/magpie/route?session=$SID&wait=0" -H "x-api-key: $MAGPIE_KEY" \
  | jq '.route | {asked_effort, model, effort, rule: (.rule | {n, useEffort}), tries: [.tries[] | {model, effort}]}'

Repeat with "thinking":{"type":"adaptive"},"output_config":{"effort":"high"}, and with no reasoning fields at all. For rule 4, use a one-line diff review prompt.

What happened, and what you expected

All requests returned 200 on their first try. The route effort, tries[0].effort and the effort column of magpie usage --csv agree in every case.

Reasoning fields sent asked_effort Rule 9 (low) sent Rule 4 (xhigh) sent
thinking: adaptive + output_config.effort: high high low ✅ xhigh ✅
none — low ✅ medium ❌
output_config.effort: high, no thinking — high ❌ high ❌

The third row is what Claude Code's side requests look like (title and similar calls carry the session's effort but no thinking). In three long Claude Code runs on 0.1.4582, 139 rule-9 GLM requests with an empty asked_effort went out at high instead of low, and 6 rule-4 requests went out at high instead of xhigh. Main-loop requests, which carry thinking, got the rule's level every time.

Expected. When a rule sets use-effort, the member is asked for that level (fitted to its levels) whether or not the request has thinking or output_config.effort.

Cause (source at v0.1.4623).

  1. The rule's level comes from ruleEffort and is applied at gateway.go#L4377-L4380 through withModelEffort → withFixedEffort.
  2. For an Anthropic request without thinking, withFixedEffort only adds thinking: {type: enabled, budget_tokens: min(budgetOf(level), max_tokens-1)}. It leaves an existing top-level output_config.effort (and the per-message output_config that turnEffort reads) as it was.
  3. Downstream, output_config.effort takes precedence over the budget:
  4. With no effort field at all, the level is read back from the budget (effortOfBudget): xhigh's 32000 becomes 8191 under max_tokens: 8192, which reads as medium.

A possible fix:

  • When withFixedEffort adds thinking to a request that had none, also set (or drop) output_config.effort, top-level and per-message, to the rule's level.
  • For adaptive models, write output_config.effort directly instead of relying on a budget that max_tokens can cut.

A separate design question: should a rule's use-effort turn reasoning on for side requests that asked for none? It does today; GLM's replies to them come back with a thinking block.

Related: #6621, the same output_config.effort-without-thinking shape but without rules involved. The code above is unchanged in v0.1.4582…v0.1.4623.

Error text or logs

No errors: every request returned 200. The evidence is the route, try and usage effort fields shown above.

Dunqing avatar Nov 21 '26 01:11 Dunqing

I have a fix ready on fork branch eisen0419:fix/7389-routing-group-use-effort-anthropic (e511b373, based on e5c2eb94):

  • Root cause: When a routing group rule sets use-effort on a native Anthropic /v1/messages request that omits thinking (or carries a different output_config.effort), the gateway did not inject the upstream model's thinking shape or rewrite top-level/per-message output_config.effort consistently, and budget-based requests could have budget_tokens clipped below the requested level without raising max_tokens.
  • Fix: Apply routing-rule / fixed-member / side / subagent effort overrides on native and translated Anthropic /v1/messages using the turn's wire snapshot (effortShapeModelAt on wiresOf):
    • Emit adaptive thinking + output_config.effort for adaptive-only Claudes and Gemini 3+ relays, budget + output_config.effort for outputEffortAt models/hosts (including DeepSeek's host), and budget-only (rewriting any client-supplied output_config.effort without adding a synthetic field) for budget Claudes and budget vendors, raising max_tokens past budgetOf(level) while honoring withMaxOutput / withReplyCap.
    • Under tight caps (0 < cap <= 1024), omit synthetic thinking/effort when the model is non-adaptive or requires thinking.type: "enabled", while preserving adaptive thinking when accepted; skip synthetic thinking beside forced tool_choice while still rewriting client-supplied output_config.effort; read per-message and unicode-escaped output_config.effort in withAdaptiveAsEnabled; record clipped translated budgets via effortOfBudget; update docs/subsystems/gateway-routing.md.
  • Verification: gofmt, targeted go test -race -count=1 ./internal/gateway (including rule_use_effort_anthropic_test.go and wire_test.go), go test -count=1 -run TestDocs ./internal/testenv, go test -count=1 ., and 3-OS go vet all pass.

eisen0419 avatar Nov 21 '26 20:11 eisen0419