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: a resumed Claude Code turn after a model switch names the model that answered it (#452)

github-actions[bot]b0dbc6bd564f
diff --git a/runner/server.py b/runner/server.py
index ff24773b..a5d7eaac 100644
--- a/runner/server.py
+++ b/runner/server.py
@@ -1713,6 +1713,16 @@ def _claude_passthrough(obj: dict, state: dict) -> list[dict]:
if obj2 is None:
return []
obj = obj2
+ if obj.get("type") == "assistant" and not obj.get("parent_tool_use_id"):
+ # The model that wrote this turn's answer is named on the answer itself. The result's
+ # modelUsage is the SESSION's running total: after a model switch on a resumed session it
+ # still credits the previous model with every earlier turn's tokens, so the most output
+ # tokens named the old model on the first turn served by the new one (claude-haiku-4.5,
+ # 111 tokens from two turns, over claude-fable-5-1's 14; the matrix's switch row,
+ # 2026-10-12). A subagent's message carries parent_tool_use_id and does not count.
+ m = str((obj.get("message") or {}).get("model") or "")
+ if m and m != "<synthetic>":
+ state["_answer_model"] = m
if obj.get("type") == "result":
# A turn whose only "answer" is the CLI's error line is a failed turn with that reason: a
# Qwen Code turn Opus 5 refused completed with "[API Error: Model stream ended with empty
@@ -1731,6 +1741,10 @@ def _claude_passthrough(obj: dict, state: dict) -> list[dict]:
# produced the output is the served model, which a direct Anthropic key never sent through
# the relay (served_model stayed empty on every Claude Code run of a benchmark, 2026-09-30).
served = _claude_model_used(obj)
+ if served and state.get("_answer_model"):
+ # Only where the CLI reports usage by model (Claude Code): grok's result names no model
+ # and leaves the served one to the relay, through this same passthrough.
+ served = state["_answer_model"]
if served:
obj = {**obj, "model": served}
# Default (batch) mode: pass the CLI's stream-json through unchanged — the CLI emits one event
diff --git a/runner/tests/test_messages_route.py b/runner/tests/test_messages_route.py
index 85ae4cfe..2afd13ff 100644
--- a/runner/tests/test_messages_route.py
+++ b/runner/tests/test_messages_route.py
@@ -183,6 +183,27 @@ def test_the_claude_result_names_the_model_from_its_usage_by_model():
assert named[0]["model"] == "x"
+def test_a_resumed_claude_turn_after_a_switch_names_the_model_that_answered():
+ """modelUsage on the result is the session's running total: the first turn on a new model still
+ credits the old one with every earlier turn, so the most output tokens named the wrong model.
+ The answer's own message names the model that wrote it; a subagent's message does not count."""
+ state: dict = {}
+ server._claude_passthrough({"type": "assistant", "parent_tool_use_id": "toolu_1",
+ "message": {"model": "claude-haiku-4-5-20251001", "content": []}}, state)
+ server._claude_passthrough({"type": "assistant", "parent_tool_use_id": None,
+ "message": {"model": "anthropic/claude-fable-5.1",
+ "content": [{"type": "text", "text": "M3"}]}}, state)
+ out = server._claude_passthrough({"type": "result", "result": "M3", "modelUsage": {
+ "claude-haiku-4-5-20251001": {"outputTokens": 111}, # two earlier turns, the old model
+ "anthropic/claude-fable-5.1": {"outputTokens": 14}}}, state)
+ assert out[0]["model"] == "anthropic/claude-fable-5.1"
+ # a synthetic message (the CLI's own error line) names no model; the usage still decides then
+ st2: dict = {}
+ server._claude_passthrough({"type": "assistant", "message": {"model": "<synthetic>", "content": []}}, st2)
+ out = server._claude_passthrough({"type": "result", "result": "ok", "modelUsage": {"m": {"outputTokens": 3}}}, st2)
+ assert out[0]["model"] == "m"
+
+
def test_the_hermes_result_carries_the_relays_served_model_and_cache_split():
"""hermes builds its own result from its session counters, which carry no cache figures and no
model; through the relay the provider's statement is at hand and is what prices the turn."""