Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

fix(gateway): the broker strips by request shape, never the reasoning of an OpenAI-shape request (#75)

github-actions[bot]cae9199b4841
diff --git a/gateway/app.py b/gateway/app.py
index b96e8443..c991e484 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -2861,35 +2861,31 @@ _BROKER_HOP = ("host", "content-length", "connection", "keep-alive", "transfer-e
"authorization", "api-key", "x-api-key")
-# Extended thinking is off through the broker, and this is the one place that enforces it.
+# What the broker removes from a request, by request shape and by key, and nothing else:
#
-# The decision itself is not new — harness_runner already set MAX_THINKING_TOKENS=0 to stop
-# Claude Code sending thinking params, because opus-4.7/4.8 rejected `thinking.enabled` with a
-# 400 that leaked into the reply. That mechanism lived in the CLI's environment, so it only
-# covered one harness and only the shape the CLI used at the time. The CLI has since moved to
-# `output_config.effort`, which MAX_THINKING_TOKENS does not suppress, and haiku-4.5 rejects it
-# with "This model does not support the effort parameter" — the same class of failure, on a
-# different harness, through a different field.
-#
-# Every harness's inference traffic passes through this proxy, so enforcing it here covers
-# Claude Code, Hermes, Codex and anything added later, across whichever thinking API a CLI
-# happens to speak. The per-harness env hack is deleted rather than kept alongside: two
-# mechanisms for one behaviour is how the first one went stale unnoticed.
-# These travel together and must be removed together. `context_management`'s only strategy
-# today (clear_thinking_20251015) is defined in terms of thinking, so removing `thinking` while
-# leaving it produced a NEW 400 — "clear_thinking_20251015 strategy requires thinking to be
-# enabled or adaptive" — turning one broken model into a broken harness. Against this provider
-# context_management is rejected outright ("Extra inputs are not permitted") whether thinking is
-# present or not, so the coherent unit is: strip the whole group, leave a self-consistent request.
-_STRIP_REQUEST_FIELDS = ("thinking", "reasoning", "reasoning_effort", "context_management")
-
-
-def _strip_unsupported(body: bytes) -> bytes:
- """Remove thinking/effort controls from an inference request body.
-
- Returns the body unchanged if it is not JSON — the broker must stay a dumb pipe for
- anything it does not positively understand.
- """
+# * Anthropic-shape requests (the `messages` path): the extended-thinking controls. Measured 400s:
+# opus-4.7/4.8 reject Claude Code's `thinking.enabled` ("use output_config.effort"), haiku-4.5
+# rejects `output_config.effort` and `thinking.adaptive`; `context_management`'s only strategy
+# is defined in terms of thinking, so it goes with it. These are model-version rejections, the
+# same on the org's own key as on the platform's, so they are stripped for every provider that
+# carries the Anthropic shape (Anthropic, Bedrock, TokenRouter, Vercel, LLMTR).
+# * OpenAI-shape requests (`responses`, `responses/*`, `chat/completions`): nothing of the
+# thinking group. `reasoning` carries Codex's effort and, on `responses/compact`, the
+# `reasoning.context = all_turns` the compaction needs; TokenRouter and OpenRouter both take
+# `reasoning` and `reasoning_effort` as written (probed 2026-09-06). Stripping it here ran
+# every brokered Codex turn at default effort and broke compaction on an org's own OpenAI key
+# ("requires reasoning.context to be all_turns").
+# * On the platform's key, on either shape: the priced-tier selectors (OpenAI `service_tier`,
+# Anthropic fast mode's `speed`, an aggregator's `provider` preferences), because the platform
+# bills the standard tier. On the org's own key the tier is the org's own choice.
+_ANTHROPIC_THINKING_FIELDS = ("thinking", "context_management")
+_TIER_FIELDS = ("service_tier", "speed", "provider")
+
+
+def _strip_unsupported(body: bytes, provider: str = "", byok: bool = False, path: str = "") -> bytes:
+ """Remove what this request must not carry (see the rule above). Returns the body unchanged
+ if it is not JSON — the broker must stay a dumb pipe for anything it does not positively
+ understand."""
if not body:
return body
try:
@@ -2899,18 +2895,23 @@ def _strip_unsupported(body: bytes) -> bytes:
if not isinstance(doc, dict):
return body
changed = False
- for f in _STRIP_REQUEST_FIELDS:
+ anthropic_shape = (path or "").strip("/").startswith("messages")
+ fields: tuple[str, ...] = _ANTHROPIC_THINKING_FIELDS if anthropic_shape else ()
+ if not byok:
+ fields += _TIER_FIELDS
+ for f in fields:
if f in doc:
doc.pop(f)
changed = True
- # `output_config` carries more than effort; drop only that key, and the object with it
- # if nothing else remains, so a provider never sees an empty container it may reject.
- oc = doc.get("output_config")
- if isinstance(oc, dict) and "effort" in oc:
- oc.pop("effort")
- changed = True
- if not oc:
- doc.pop("output_config")
+ if anthropic_shape:
+ # `output_config` carries more than effort; drop only that key, and the object with it
+ # if nothing else remains, so a provider never sees an empty container it may reject.
+ oc = doc.get("output_config")
+ if isinstance(oc, dict) and "effort" in oc:
+ oc.pop("effort")
+ changed = True
+ if not oc:
+ doc.pop("output_config")
return json.dumps(doc).encode() if changed else body
@@ -3018,7 +3019,7 @@ async def llm_broker(path: str, request: Request):
else:
headers["authorization"] = f"Bearer {key}"
- body = _strip_unsupported(await request.body())
+ body = _strip_unsupported(await request.body(), provider=provider, byok=True, path=suffix)
rc = _relay_client()
req = rc.build_request(request.method, url, headers=headers, content=body or None)
up = await rc.send(req, stream=True)
diff --git a/gateway/tests/test_broker_strips_thinking.py b/gateway/tests/test_broker_strips_thinking.py
new file mode 100644
index 00000000..2d433c27
--- /dev/null
+++ b/gateway/tests/test_broker_strips_thinking.py
@@ -0,0 +1,76 @@
+"""What the broker strips, by request shape and by key.
+
+Anthropic-shape requests lose the extended-thinking controls on every provider and every key
+(measured model-version 400s: opus-4.7/4.8 reject `thinking.enabled`, haiku-4.5 rejects
+`output_config.effort` and `thinking.adaptive`). OpenAI-shape requests keep `reasoning` and
+`reasoning_effort` on every provider: TokenRouter and OpenRouter take them as written (probed
+2026-09-06), and Codex's compaction needs `reasoning.context`. Priced-tier selectors go only on
+the platform's key. Anything that is not JSON passes untouched.
+"""
+import json
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from app import _strip_unsupported # noqa: E402
+
+
+def _anth(**kw) -> bytes:
+ return json.dumps({"model": "claude-haiku-4.5", "max_tokens": 64,
+ "messages": [{"role": "user", "content": "hi"}], **kw}).encode()
+
+
+def _resp(**kw) -> bytes:
+ return json.dumps({"model": "gpt-5.5", "input": "hi", **kw}).encode()
+
+
+def test_anthropic_shape_loses_thinking_controls_on_every_key():
+ for byok in (False, True):
+ for provider in ("anthropic", "tokenrouter", "vercel", "bedrock"):
+ out = json.loads(_strip_unsupported(_anth(thinking={"type": "enabled", "budget_tokens": 1024},
+ context_management={"edits": []},
+ output_config={"effort": "high", "format": {"type": "json"}}),
+ provider=provider, byok=byok, path="messages"))
+ assert "thinking" not in out and "context_management" not in out, (provider, byok)
+ assert out["output_config"] == {"format": {"type": "json"}}
+ assert out["messages"] and out["max_tokens"] == 64
+
+
+def test_anthropic_shape_empty_output_config_goes_with_effort():
+ out = json.loads(_strip_unsupported(_anth(output_config={"effort": "low"}), provider="anthropic", byok=True, path="messages"))
+ assert "output_config" not in out
+
+
+def test_openai_shape_keeps_reasoning_on_every_provider():
+ body = _resp(reasoning={"effort": "high", "summary": "auto"})
+ for provider in ("openai", "azure", "azure-foundry", "tokenrouter", "openrouter", "vercel", "google"):
+ for byok in (False, True):
+ out = json.loads(_strip_unsupported(body, provider=provider, byok=byok, path="responses"))
+ assert out["reasoning"] == {"effort": "high", "summary": "auto"}, (provider, byok)
+ chat = json.dumps({"model": "gpt-5.4", "messages": [{"role": "user", "content": "hi"}], "reasoning_effort": "low"}).encode()
+ for provider in ("openai", "tokenrouter", "openrouter"):
+ assert json.loads(_strip_unsupported(chat, provider=provider, byok=False, path="chat/completions"))["reasoning_effort"] == "low"
+
+
+def test_codex_compaction_keeps_reasoning_context():
+ body = _resp(reasoning={"context": "all_turns"}, previous_response_id="resp_1")
+ out = _strip_unsupported(body, provider="openai", byok=True, path="responses/compact")
+ assert out == body # byte-identical: nothing to strip, nothing rewritten
+
+
+def test_tier_selectors_stripped_on_the_platform_key_only():
+ for path, body in (("responses", _resp(service_tier="priority")),
+ ("messages", _anth(speed="fast")),
+ ("chat/completions", json.dumps({"model": "m", "messages": [], "provider": {"order": ["openai"]}}).encode())):
+ platform = json.loads(_strip_unsupported(body, provider="openrouter", byok=False, path=path))
+ own = json.loads(_strip_unsupported(body, provider="openrouter", byok=True, path=path))
+ assert not ({"service_tier", "speed", "provider"} & set(platform)), path
+ assert {"service_tier", "speed", "provider"} & set(own), path
+
+
+def test_non_json_passes_untouched():
+ raw = b"not json at all"
+ assert _strip_unsupported(raw, provider="tokenrouter", byok=False, path="messages") is raw
+ assert _strip_unsupported(b"", provider="openai") == b""
+ arr = b"[1, 2]"
+ assert _strip_unsupported(arr, provider="openai", path="responses") is arr