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.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
runner: cline's inputTokens is the whole prompt; net the cached part out like codex's (#209)
Hao Lud8c8d1e1aff0
diff --git a/runner/server.py b/runner/server.pyindex 315bffdf..4e6f6d07 100644--- a/runner/server.py+++ b/runner/server.py@@ -4222,7 +4222,8 @@ def _cline_to_claude(obj: dict, state: dict) -> list[dict]:token-level — do not advertise token streaming); tool carries toolName,toolCallId and full input on start, output on end.run_result the terminal event: finishReason, text, usage {inputTokens, outputTokens,- cacheReadTokens, cacheWriteTokens}.+ cacheReadTokens, cacheWriteTokens}. inputTokens is GROSS — the whole prompt,+ cached part included — see the run_result branch."""t = obj.get("type")pre: list[dict] = []@@ -4265,10 +4266,22 @@ def _cline_to_claude(obj: dict, state: dict) -> list[dict]:return []if t == "run_result":u = obj.get("usage") or {}- usage = {"input_tokens": int(u.get("inputTokens") or 0),+ cache_read = int(u.get("cacheReadTokens") or 0)+ cache_write = int(u.get("cacheWriteTokens") or 0)+ # cline's inputTokens is the whole prompt, cached part included: its own cost formula bills+ # inputTokens - cacheReadTokens - cacheWriteTokens (sdk/packages/llms/src/providers/ai-sdk.ts,+ # calculateCost), and on the openai-compatible provider every turn here runs through it is the+ # sum of each request's prompt_tokens. Measured on 3.0.60 against deepseek-flash: eleven+ # requests summing to inputTokens 135208 / cacheReadTokens 122624, the second of them+ # 6999 / 6400 on a conversation of 6463 tokens so far. The contract is fresh input only+ # (_opencode_usage_add, _pi_usage_add, _norm_token_usage for codex), so net the cached part+ # out here as codex's path does; passed through, the biller charged the cached prefix at the+ # full input rate on top of the cache rate, and cline read as 30x opencode's tokens on the+ # same task.+ usage = {"input_tokens": max(int(u.get("inputTokens") or 0) - cache_read - cache_write, 0),"output_tokens": int(u.get("outputTokens") or 0),- "cache_read_tokens": int(u.get("cacheReadTokens") or 0),- "cache_write_tokens": int(u.get("cacheWriteTokens") or 0)}+ "cache_read_tokens": cache_read,+ "cache_write_tokens": cache_write}state["_cl_usage"] = usagestate["_cl_done"] = Truereason = str(obj.get("finishReason") or "")diff --git a/runner/tests/test_cline_backend.py b/runner/tests/test_cline_backend.pyindex 5ffc64e9..d8bb256c 100644--- a/runner/tests/test_cline_backend.py+++ b/runner/tests/test_cline_backend.py@@ -127,6 +127,25 @@ def test_the_verified_tool_round_trip_normalizes():assert _cline_eof(state, 0) == [], "a healthy turn already has its result; eof must add nothing"+def test_run_result_usage_nets_the_cached_read_out_of_input():+ """Captured live on 3.0.60 through the openai-compatible provider against deepseek-flash: a+ turn of eleven requests whose per-request usage cline stores as (inputTokens, outputTokens,+ cacheReadTokens) 6236/227/0, 6999/62/6400, 7222/298/7040, ... 18226/299/18048 — inputTokens+ grows with the conversation and always covers cacheReadTokens, i.e. it is the whole prompt,+ and run_result reports their sums. The contract is fresh input only, so the cached part must+ come out; passed through, the turn billed 135208 input tokens for 12584 fresh ones."""+ state = {}+ out = _ev('{"ts":"t","type":"run_result","finishReason":"completed","iterations":11,"usage":{"inputTokens":135208,"outputTokens":6597,"cacheReadTokens":122624,"cacheWriteTokens":0,"totalCost":0},"durationMs":63000,"text":"done","model":{"id":"deepseek-flash"}}', state)+ assert out[-1]["usage"] == {"input_tokens": 135208 - 122624, "output_tokens": 6597,+ "cache_read_tokens": 122624, "cache_write_tokens": 0}+ # A cache write is part of the gross prompt too, by cline's own cost formula.+ out = _ev('{"ts":"t","type":"run_result","finishReason":"completed","iterations":1,"usage":{"inputTokens":1000,"outputTokens":10,"cacheReadTokens":600,"cacheWriteTokens":300},"text":"done","model":{"id":"m"}}', {})+ assert out[-1]["usage"]["input_tokens"] == 100+ # Never below zero if a provider ever reports net input with a cache count beside it.+ out = _ev('{"ts":"t","type":"run_result","finishReason":"completed","iterations":1,"usage":{"inputTokens":50,"outputTokens":10,"cacheReadTokens":600},"text":"done","model":{"id":"m"}}', {})+ assert out[-1]["usage"]["input_tokens"] == 0++def test_the_verified_error_shape_fails_the_turn():"""Captured live: a bad credential emits agent_event{type:error} and run_resultfinishReason 'error' whose text restates the message."""