Experience-Driven Black-Box Cost Control for Coding Agents
Python agentic-aiagentic-codingai-codingai-coding-assistantanthropicartificial-intelligenceclaude-codecodexcoding-agentcoding-agentscost-optimizationcost-reductiondeveloper-toolsgithub-copilotllmopen-sourceopenaiproductivitytoken-optimization
measure: a probe that exits non-zero raises even when stdout has usage
Useless007248eb4dc06d4
diff --git a/tests/test_toolcheck_measure.py b/tests/test_toolcheck_measure.pyindex d4fb1b2..a4cd8ca 100644--- a/tests/test_toolcheck_measure.py+++ b/tests/test_toolcheck_measure.py@@ -44,3 +44,19 @@ def test_a_failed_probe_raises_instead_of_counting_zero(monkeypatch, stdout):_fake_claude(monkeypatch, [], stdout=stdout)with pytest.raises(RuntimeError):toolcheck.measure(["mcp__x__y"], cli="claude")+++def test_a_probe_that_exits_non_zero_raises_even_with_usage(monkeypatch):+ # A crashed lean probe can still print usage; counting it would report 80 tokens saved.+ replies = iter([(0, 100, ""), (1, 20, "boom")])++ def run(cmd, **kw):+ code, tokens, err = next(replies)+ return subprocess.CompletedProcess(+ cmd, code, stdout=json.dumps({"is_error": False, "usage": {"input_tokens": tokens}}),+ stderr=err)++ monkeypatch.setattr(toolcheck.subprocess, "run", run)+ monkeypatch.setattr(toolcheck, "claude_flags", lambda cli=None: [])+ with pytest.raises(RuntimeError, match="boom"):+ toolcheck.measure(["mcp__x__y"], cli="claude")diff --git a/tokeneconomizer/plugin/toolcheck.py b/tokeneconomizer/plugin/toolcheck.pyindex b083107..c3e04e0 100644--- a/tokeneconomizer/plugin/toolcheck.py+++ b/tokeneconomizer/plugin/toolcheck.py@@ -230,7 +230,7 @@ def _prompt_tokens(extra_args, cli=None, timeout=180):u = data.get("usage") or {}total = sum(int(u.get(k) or 0) for k in ("input_tokens", "cache_read_input_tokens","cache_creation_input_tokens"))- if data.get("is_error") or not total:+ if out.returncode or data.get("is_error") or not total:# A failed probe would otherwise count as 0 tokens, and the whole request as saved.raise RuntimeError("`claude -p` reported no usage: "+ ((out.stderr or out.stdout or "").strip()[:300] or "no output"))