Skip to content

Official Codebase of 3DCodeVerse

Python

claude-code: every --model ALIAS was recorded as the CLI's housekeeping model

gaoypenga64f41c2009c
diff --git a/harness/codeverse/agents/claude_code.py b/harness/codeverse/agents/claude_code.py
index a3fe0e03..5104a3ab 100644
--- a/harness/codeverse/agents/claude_code.py
+++ b/harness/codeverse/agents/claude_code.py
@@ -77,13 +77,36 @@ def usage_from_envelope(env: dict[str, Any], model: str) -> Usage:
cost_usd=float(env.get("total_cost_usd") or 0.0),
latency_ms=int(env.get("duration_ms") or 0),
)
- # modelUsage carries the real served model name(s); keep the first when ours is an alias
- served = list((env.get("modelUsage") or {}).keys())
- if served and model not in served:
- usage.model = served[0]
+ usage.model = primary_served_model(env, model)
return usage
+def primary_served_model(env: dict[str, Any], model: str) -> str:
+ """The model that actually did the WORK, of the (up to two) that one session bills.
+
+ ``claude -p`` bills the work model PLUS the small background model the CLI uses for
+ its own housekeeping, and claude 2.1.243 lists the AUXILIARY one first — so taking
+ ``served[0]`` recorded a model that did ~1% of the tokens whenever ``--model`` was an
+ ALIAS ('sonnet', 'opus', or the default arm). A sonnet-vs-default comparison was
+ therefore labelled haiku-vs-haiku, in CallCost.model and in OneShotResult.usage.
+
+ The envelope's top-level ``usage`` block reports the MAIN conversation only, so the
+ entry whose token counts match it is the work model; otherwise take the dearest.
+ An id we passed verbatim is already a served name and is kept as-is.
+ """
+ served: dict[str, Any] = env.get("modelUsage") or {}
+ if not served or model in served:
+ return model
+ u = env.get("usage") or {}
+ want = (int(u.get("input_tokens") or 0), int(u.get("output_tokens") or 0))
+ if any(want):
+ for name, row in served.items():
+ r = row or {}
+ if (int(r.get("inputTokens") or 0), int(r.get("outputTokens") or 0)) == want:
+ return name
+ return max(served, key=lambda n: float((served[n] or {}).get("costUSD") or 0.0))
+
+
class ClaudeCodeAgent:
kind = "claude-code"
@@ -164,4 +187,4 @@ class ClaudeCodeAgent:
)
-__all__ = ["ClaudeCodeAgent", "parse_claude_json", "usage_from_envelope", "ALLOWED_TOOLS"]
+__all__ = ["ClaudeCodeAgent", "parse_claude_json", "primary_served_model", "usage_from_envelope", "ALLOWED_TOOLS"]
diff --git a/harness/tests/agents/test_claude_code.py b/harness/tests/agents/test_claude_code.py
index f99a463f..ec0768c4 100644
--- a/harness/tests/agents/test_claude_code.py
+++ b/harness/tests/agents/test_claude_code.py
@@ -8,7 +8,12 @@ from pathlib import Path
import pytest
-from codeverse.agents.claude_code import ClaudeCodeAgent, parse_claude_json, usage_from_envelope
+from codeverse.agents.claude_code import (
+ ClaudeCodeAgent,
+ parse_claude_json,
+ primary_served_model,
+ usage_from_envelope,
+)
from codeverse.agents.cli_common import begin_session
from codeverse.agents.materialize import materialize_workspace
from codeverse.contracts.agent import AgentJob
@@ -21,6 +26,20 @@ ENVELOPE = {
"modelUsage": {"claude-sonnet-4-6": {"inputTokens": 10, "outputTokens": 40, "costUSD": 0.0123}},
}
+#: what a real `claude -p --model sonnet` session bills: the work model PLUS the small
+#: background model the CLI uses for its own housekeeping — and claude 2.1.243 lists the
+#: AUXILIARY one FIRST. Token counts and costs are the ones observed in the CC-1 repro.
+TWO_MODEL_ENVELOPE = {
+ "type": "result", "subtype": "success", "is_error": False, "duration_ms": 10000,
+ "num_turns": 1, "result": "pong", "session_id": "xyz", "total_cost_usd": 0.023795,
+ "usage": {"input_tokens": 2, "output_tokens": 4, "cache_read_input_tokens": 0},
+ "modelUsage": {
+ "claude-haiku-4-5-20251001": {"inputTokens": 1035, "outputTokens": 21, "costUSD": 0.000955},
+ "claude-sonnet-5": {"inputTokens": 2, "outputTokens": 4, "costUSD": 0.02284},
+ },
+}
+
+
FAKE_CLAUDE = r'''
args = sys.argv[1:]
prompt = args[args.index("-p") + 1]
@@ -87,3 +106,34 @@ def test_live_claude_tiny(tmp_ws: Workspace):
assert res.ok, res.errors
assert (tmp_ws.src / "hello.txt").read_text().strip() == "hi"
assert res.usage.output_tokens > 0
+
+
+# --------------------------------------------------------------------------- CC-1
+def test_an_alias_records_the_model_that_did_the_work_not_the_housekeeping_one():
+ """`claude -p` bills two models and lists the AUXILIARY one first, so
+ `served[0]` recorded haiku — which did 0.9% of the tokens — for every
+ --model ALIAS ('sonnet', 'opus', and the default arm). A sonnet-vs-default
+ comparison was therefore labelled haiku-vs-haiku."""
+ for alias in ("sonnet", "opus", ""):
+ u = usage_from_envelope(TWO_MODEL_ENVELOPE, alias)
+ assert u.model == "claude-sonnet-5", alias
+ # the top-level usage block reports the MAIN conversation only — that is the signal
+ assert primary_served_model(TWO_MODEL_ENVELOPE, "sonnet") == "claude-sonnet-5"
+
+
+def test_a_full_id_we_passed_verbatim_is_kept():
+ """Full ids were never affected; the bug bit only the form the owner asked for."""
+ assert primary_served_model(TWO_MODEL_ENVELOPE, "claude-sonnet-5") == "claude-sonnet-5"
+ assert primary_served_model(TWO_MODEL_ENVELOPE, "claude-haiku-4-5-20251001") == "claude-haiku-4-5-20251001"
+
+
+def test_the_dearest_entry_wins_when_the_token_counts_do_not_match():
+ """Fallback for an envelope whose top-level usage block does not line up with any
+ modelUsage row: the work model is the one that cost the money, never the side-call."""
+ env = {**TWO_MODEL_ENVELOPE, "usage": {"input_tokens": 0, "output_tokens": 0}}
+ assert primary_served_model(env, "sonnet") == "claude-sonnet-5"
+
+
+def test_no_model_usage_block_keeps_what_we_asked_for():
+ assert primary_served_model({"usage": {"input_tokens": 1}}, "sonnet") == "sonnet"
+ assert usage_from_envelope({"usage": {"input_tokens": 1}}, "sonnet").model == "sonnet"