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.

runner: Qwen Code's own max_tokens never reaches the provider, so a self-hosted model with a small window answers (#609)

Yue Sudc96024b3fe9
diff --git a/runner/server.py b/runner/server.py
index d1f4cc27..c6680a7d 100644
--- a/runner/server.py
+++ b/runner/server.py
@@ -7263,6 +7263,17 @@ def _hermes_prepare_env(provider: str | None, auth: Auth, cwd: str, env: dict,
QWEN_PROVIDERS = {"anthropic", "openai", "azure", "openai-api", "tokenrouter"}
+# The output cap Qwen Code puts on every request on its own initiative, the kimi precedent: nobody
+# configured it, and it is the CLI's figure for the model NAME, not for the model the endpoint serves.
+# 0.26.3 sent max_tokens: 32768 for qwen3-4b (captured between the relay and a real vLLM, 2026-10-28);
+# the report (#578) has 64000 for qwen3.5-9b. The image's 0.22.2 does the same on every call: 64000 for
+# most ids, 16384 for gpt-6.1-sol, 32000 for an id it does not know, never max_completion_tokens (measured
+# at a stub, 2026-10-28). A self-hosted model's window is the server's choice, and
+# vLLM refuses the whole request when the cap does not fit it: "max_tokens=64000 cannot be greater than
+# max_model_len=max_total_tokens=8192" there, "you requested 49350 tokens (16582 in the messages, 32768
+# in the completion)" here. Without the field the provider's own default applies, on vLLM the room left
+# in the window, as on every base that sends none. No max_completion_tokens was seen.
+_QWEN_DROP_FIELDS = ("max_tokens",)
def _qwen_settings(home: pathlib.Path, mcp_servers: list[dict] | None) -> None:
@@ -7314,8 +7325,9 @@ def _build_qwen(provider: str, auth: Auth, model: str, prompt: str, cwd: str, en
# array of parts, which aggregators 400 with 'content must be a string' — measured live
# against LLMTR; Azure's gpt-5.x deployments 400 on max_tokens), and the real key never
# enters the CLI's environment — which matters more here than anywhere, because qwen
- # takes its credential ONLY via env.
- relay_base, relay_tok = _hermes_relay_route(auth.base_url, auth.api_key)
+ # takes its credential ONLY via env. The route also takes off the output cap the CLI
+ # invents (_QWEN_DROP_FIELDS).
+ relay_base, relay_tok = _hermes_relay_route(auth.base_url, auth.api_key, drop_fields=_QWEN_DROP_FIELDS)
auth = auth.model_copy(update={"base_url": relay_base, "api_key": relay_tok})
home = pathlib.Path(cwd) / ".harness" / "home"
home.mkdir(parents=True, exist_ok=True)
diff --git a/runner/tests/test_qwen_build.py b/runner/tests/test_qwen_build.py
index 625e6d59..fb184811 100644
--- a/runner/tests/test_qwen_build.py
+++ b/runner/tests/test_qwen_build.py
@@ -81,3 +81,81 @@ def test_normalizer_is_the_claude_passthrough():
def test_instruction_file_is_qwen_md():
assert _agent_doc_path("/ws", "qwen").name == "QWEN.md"
+
+
+# A request in the shape Qwen Code 0.26.3 sent a real vLLM (captured 2026-10-28, #578): its own cap on
+# every call, with the stream and its usage asked for.
+def _captured(cap: int = 32768) -> dict:
+ return {"model": "qwen3-4b", "max_tokens": cap, "stream": False,
+ "stream_options": {"include_usage": True},
+ "messages": [{"role": "system", "content": "You are Qwen Code."},
+ {"role": "user", "content": "Create hello.txt"}],
+ "tools": [{"type": "function", "function": {"name": "write_file", "parameters": {"type": "object"}}}]}
+
+
+def test_the_cap_the_cli_invents_never_reaches_the_provider():
+ """0.26.3 puts max_tokens on every request, 32768 for qwen3-4b and 64000 for qwen3.5-9b (#578), a
+ figure for the model's name, not for what the endpoint serves. The route is born dropping it, and
+ every other field goes as the CLI wrote it."""
+ from server import _HERMES_RELAY, _drop_top_level_field
+ _, _, env = _argv()
+ flags = _HERMES_RELAY["routes"][env["OPENAI_API_KEY"]][2]
+ assert flags["drop_fields"] == ("max_tokens",)
+ body = json.dumps(_captured(64000)).encode()
+ for f in flags["drop_fields"]:
+ body = _drop_top_level_field(body, f)
+ want = _captured()
+ del want["max_tokens"]
+ assert json.loads(body) == want
+
+
+def test_a_model_whose_window_is_smaller_than_the_cap_answers_through_the_route():
+ """The report's setup through the real relay: a vLLM model served with max_model_len=8192 refuses
+ any request whose cap does not fit ("max_tokens=64000 cannot be greater than max_model_len=
+ max_total_tokens=8192"). Through the route _build_qwen registers, the call reaches the provider
+ without the cap and is answered; the refusal is what the provider gave before this route dropped it."""
+ import threading
+ import urllib.request
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+
+ got: list[dict] = []
+ refusal = (b'{"object":"error","message":"max_tokens=64000cannot be greater than max_model_len='
+ b'max_total_tokens=8192. Please request fewer output tokens.","type":"BadRequestError",'
+ b'"param":"max_tokens","code":400}')
+
+ class VLLM(BaseHTTPRequestHandler):
+ def do_POST(self):
+ body = json.loads(self.rfile.read(int(self.headers["content-length"])))
+ got.append(body)
+ if body.get("max_tokens", 0) > 8192:
+ out, code = refusal, 400
+ else:
+ out, code = json.dumps({"object": "chat.completion", "model": body["model"], "choices": [
+ {"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "DONE"}}]}).encode(), 200
+ self.send_response(code)
+ self.send_header("content-type", "application/json")
+ self.send_header("content-length", str(len(out)))
+ self.end_headers()
+ self.wfile.write(out)
+
+ def log_message(self, *a):
+ pass
+
+ up = ThreadingHTTPServer(("127.0.0.1", 0), VLLM)
+ threading.Thread(target=up.serve_forever, daemon=True).start()
+ try:
+ env: dict = {}
+ _build_qwen("openai-api", Auth(api_key="sk-local", base_url=f"http://127.0.0.1:{up.server_address[1]}/v1"),
+ "qwen3.5-9b", "hi", tempfile.mkdtemp(), env)
+ req = urllib.request.Request(env["OPENAI_BASE_URL"] + "/chat/completions", method="POST",
+ data=json.dumps(_captured(64000)).encode(),
+ headers={"authorization": f"Bearer {env['OPENAI_API_KEY']}",
+ "content-type": "application/json"})
+ answer = json.loads(urllib.request.urlopen(req, timeout=10).read())
+ assert answer["choices"][0]["message"]["content"] == "DONE"
+ assert len(got) == 1 and "max_tokens" not in got[0] # answered on the first call, without the cap
+ want = _captured()
+ del want["max_tokens"]
+ assert got[0] == want # and nothing else changed on the way
+ finally:
+ up.shutdown()