Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(claude_cli): record the whole prompt as input tokens (#2337)
Nehal kanak kuniyil thottathila18951dae429
diff --git a/docs/agent/CLAUDE_CODE_PROVIDER.md b/docs/agent/CLAUDE_CODE_PROVIDER.mdindex 218b91899..41df2bb80 100644--- a/docs/agent/CLAUDE_CODE_PROVIDER.md+++ b/docs/agent/CLAUDE_CODE_PROVIDER.md@@ -62,15 +62,24 @@ content and mapping `usage`:| Claude Code field | Repowise field ||---|---|-| `usage.input_tokens` | `input_tokens` |+| `usage.input_tokens` + `usage.cache_read_input_tokens` + `usage.cache_creation_input_tokens` | `input_tokens` |+| `usage.input_tokens` (alone) | `usage.uncached_input_tokens` || `usage.output_tokens` | `output_tokens` || `usage.cache_read_input_tokens` | `cached_tokens` || `usage.cache_creation_input_tokens` | recorded in `usage`, not counted as cached || `stop_reason` | normalised `stop_reason` |+Claude Code bills a prompt across three disjoint fields, and only the uncached+remainder lands in `input_tokens`. A page prompt carries a large stable prefix,+so the bulk of it is a cache *write* on the first page of a page type and a+cache *read* on every later one, which left `input_tokens` at a couple of tokens+per page. Repowise sums the three, so the token volume `repowise status`,+`repowise costs` and the run report show is the prompt the model actually read.+`claude_cli/*` models are priced at **$0.00**, because billing is handled by thesubscription rather than per-token API spend. The CLI's own `total_cost_usd` is-still recorded under `usage.reported_cost_usd` for auditing.+still recorded under `usage.reported_cost_usd` for auditing, and each call is+booked into the `llm_costs` ledger (at $0.00) so `repowise costs` shows the run.### Default modeldiff --git a/packages/core/src/repowise/core/providers/llm/claude_cli.py b/packages/core/src/repowise/core/providers/llm/claude_cli.pyindex b538fad60..ff8c996c4 100644--- a/packages/core/src/repowise/core/providers/llm/claude_cli.py+++ b/packages/core/src/repowise/core/providers/llm/claude_cli.py@@ -428,18 +428,30 @@ class ClaudeCliProvider(BaseProvider):raw_usage = payload.get("usage")usage = raw_usage if isinstance(raw_usage, dict) else {}- input_tokens = int(usage.get("input_tokens", 0) or 0)+ # Claude Code reports only the *uncached remainder* of the prompt as+ # ``input_tokens``. A page prompt is large and its stable prefix is+ # cached, so the bulk of it arrives as a cache write (the first page of+ # a type) or a cache read (every later page of that type). Persisting+ # this field alone recorded ``input_tokens=2`` against a ~20k-token+ # page, which is what made ``repowise status`` sum to zero for a whole+ # wiki of claude_cli pages (#2267).+ uncached_input_tokens = int(usage.get("input_tokens", 0) or 0)output_tokens = int(usage.get("output_tokens", 0) or 0)- # The CLI reports cache reads and creations separately; repowise wants a- # single "served from cache" number, which is the read half.+ # The read half is the "served from cache" number repowise reports.cached_tokens = int(usage.get("cache_read_input_tokens", 0) or 0)cache_creation_tokens = int(usage.get("cache_creation_input_tokens", 0) or 0)+ # The three are disjoint (a prompt token is sent uncached, read from the+ # cache, or written to it), so their sum is the prompt total the rest of+ # the codebase means by ``input_tokens``. ``cached_tokens`` stays the+ # read half, separately, exactly as the provider docs describe.+ input_tokens = uncached_input_tokens + cached_tokens + cache_creation_tokensstop_reason, provider_stop_reason = normalize_stop_reason(payload.get("stop_reason"))log.debug("claude_cli.generate.done",input_tokens=input_tokens,+ uncached_input_tokens=uncached_input_tokens,output_tokens=output_tokens,cached_tokens=cached_tokens,request_id=request_id,@@ -449,6 +461,8 @@ class ClaudeCliProvider(BaseProvider):**usage,"source": "claude_cli","model": self.model_name,+ "input_tokens": input_tokens,+ "uncached_input_tokens": uncached_input_tokens,"cache_creation_input_tokens": cache_creation_tokens,# Recorded for auditing only. Cost is priced at zero in the cost# table: this is subscription usage, not API spend.@@ -459,7 +473,7 @@ class ClaudeCliProvider(BaseProvider):if not usage:usage_payload["estimated"] = True- return GeneratedResponse(+ response = GeneratedResponse(content=content,input_tokens=input_tokens,output_tokens=output_tokens,@@ -468,3 +482,27 @@ class ClaudeCliProvider(BaseProvider):stop_reason=stop_reason,provider_stop_reason=provider_stop_reason,)++ tracker = getattr(self, "_cost_tracker", None)+ if tracker is not None:+ # Booked under the prefixed label so the ledger prices the call at+ # $0.00 (``claude_cli/`` is a zero-cost prefix: a seat is not API+ # spend) while ``repowise costs`` still shows the run's token+ # volume. Without this the provider wrote no ``llm_costs`` row at+ # all and ``repowise costs`` reported "No cost records found" for a+ # whole claude_cli wiki (#2267).+ #+ # Awaited inline rather than fired off as a detached task: a+ # fire-and-forget write can still be in flight when the event loop+ # is torn down after generation. ``record()`` swallows its own+ # persistence errors, so generation is unaffected either way.+ with contextlib.suppress(Exception):+ await tracker.record(+ model=self.model_name,+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ operation=tracker.operation,+ file_path=None,+ )++ return responsediff --git a/tests/unit/test_providers/test_claude_cli_provider.py b/tests/unit/test_providers/test_claude_cli_provider.pyindex 4fd6a2193..25b2cb66b 100644--- a/tests/unit/test_providers/test_claude_cli_provider.py+++ b/tests/unit/test_providers/test_claude_cli_provider.py@@ -176,7 +176,11 @@ async def test_generate_success(claude_on_path, monkeypatch):assert isinstance(result, GeneratedResponse)assert result.content == "Hello from Claude"- assert result.input_tokens == 120+ # The prompt total, not the uncached remainder: Claude Code splits a prompt+ # across ``input_tokens``, ``cache_read_input_tokens`` and+ # ``cache_creation_input_tokens``.+ assert result.input_tokens == 120 + 30 + 7955+ assert result.usage["uncached_input_tokens"] == 120assert result.output_tokens == 40# cached_tokens is the *read* half; creations are recorded separately.assert result.cached_tokens == 30@@ -401,6 +405,162 @@ async def test_missing_usage_is_flagged_estimated(claude_on_path, monkeypatch):assert result.output_tokens == 0+# ---------------------------------------------------------------------------+# Token accounting and the cost ledger (#2267)+# ---------------------------------------------------------------------------+++def _cached_page_json(*, uncached: int, read: int, created: int) -> str:+ """A ``claude -p`` payload shaped like a real page generation.++ Claude Code splits the prompt across the three usage fields: a page prompt+ carries a large stable prefix, so the bulk of it arrives as a cache write on+ the first page of a type and a cache read on every later one, leaving a+ couple of uncached tokens. Only reading ``input_tokens`` recorded 2 against+ a ~20k-token page, and ``repowise status`` summed a whole wiki to 0.+ """+ return _success_json(+ "page prose",+ usage={+ "input_tokens": uncached,+ "output_tokens": 2465,+ "cache_read_input_tokens": read,+ "cache_creation_input_tokens": created,+ },+ )+++ ("uncached", "read", "created"),+ [+ # First page of a page type: the prompt is written to the cache.+ (2, 0, 19311),+ # Later pages of the same type: that prefix is read back.+ (2, 19311, 0),+ # A short prompt that fits entirely in the uncached remainder.+ (120, 0, 0),+ ],+)+async def test_input_tokens_is_the_prompt_total(+ claude_on_path, monkeypatch, uncached, read, created+):+ async def fake_exec(*_args, **_kwargs):+ return FakeProcess(stdout=_cached_page_json(uncached=uncached, read=read, created=created))++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)++ result = await ClaudeCliProvider().generate("sys", "user")++ assert result.input_tokens == uncached + read + created+ assert result.usage["uncached_input_tokens"] == uncached+ # The read half stays its own number: cached_tokens means "served from+ # cache", and the report and the UI both read it that way.+ assert result.cached_tokens == read+ assert result.usage["cache_creation_input_tokens"] == created+ assert result.output_tokens == 2465+++class _RecordingTracker:+ """Minimal stand-in that captures what the provider books."""++ def __init__(self) -> None:+ self.operation = "doc_generation"+ self.calls: list[dict[str, Any]] = []++ async def record(self, **kwargs: Any) -> float:+ self.calls.append(kwargs)+ return 0.0+++async def test_cost_ledger_row_carries_the_prompt_total(claude_on_path, monkeypatch):+ """No row was written at all, so a claude_cli wiki produced an empty ledger.++ ``repowise costs`` printed "No cost records found" and the run report's+ dollar figure stayed $0.000 because the tokens it multiplies by a rate were+ never recorded.+ """++ async def fake_exec(*_args, **_kwargs):+ return FakeProcess(stdout=_cached_page_json(uncached=2, read=0, created=19311))++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)++ provider = ClaudeCliProvider(model="claude-opus-5")+ tracker = _RecordingTracker()+ provider._cost_tracker = tracker++ await provider.generate("sys", "user")++ assert len(tracker.calls) == 1+ booked = tracker.calls[0]+ # The prefixed label is what prices the call at zero; the bare slug would+ # bill a subscription seat at API rates.+ assert booked["model"] == "claude_cli/claude-opus-5"+ assert booked["input_tokens"] == 19313+ assert booked["output_tokens"] == 2465+ assert booked["operation"] == "doc_generation"+++async def test_cost_ledger_row_is_priced_at_zero_for_a_seat(claude_on_path, monkeypatch):+ """The ledger shows the run's token volume and $0.00 of API spend."""++ async def fake_exec(*_args, **_kwargs):+ return FakeProcess(stdout=_cached_page_json(uncached=2, read=0, created=19311))++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)++ from repowise.core.generation.cost_tracker import CostTracker, get_model_pricing++ tracker = CostTracker()+ provider = ClaudeCliProvider(model="claude-opus-5")+ provider._cost_tracker = tracker++ await provider.generate("sys", "user")++ assert get_model_pricing(provider.model_name) == {"input": 0.0, "output": 0.0}+ assert tracker.session_tokens == 19313 + 2465+ assert tracker.session_cost == 0.0+++async def test_a_failing_cost_tracker_does_not_break_generation(claude_on_path, monkeypatch):+ """Cost telemetry is non-critical: a tracker that raises must not lose the page."""++ async def fake_exec(*_args, **_kwargs):+ return FakeProcess(stdout=_success_json("still produced"))++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)++ class _ExplodingTracker:+ operation = "doc_generation"++ async def record(self, **_kwargs: Any) -> float:+ raise RuntimeError("ledger unavailable")++ provider = ClaudeCliProvider()+ provider._cost_tracker = _ExplodingTracker()++ result = await provider.generate("sys", "user")++ assert result.content == "still produced"+++async def test_no_cost_tracker_is_a_no_op(claude_on_path, monkeypatch):+ """The MCP and server paths construct the provider without a tracker."""++ async def fake_exec(*_args, **_kwargs):+ return FakeProcess(stdout=_success_json("no tracker"))++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)++ provider = ClaudeCliProvider()+ assert not hasattr(provider, "_cost_tracker")++ result = await provider.generate("sys", "user")++ assert result.content == "no tracker"+ assert result.input_tokens == 120 + 30 + 7955++# ---------------------------------------------------------------------------# Registration# ---------------------------------------------------------------------------diff --git a/website/claude-cli.md b/website/claude-cli.mdindex 1c356d4c4..6a35352cc 100644--- a/website/claude-cli.md+++ b/website/claude-cli.md@@ -79,6 +79,13 @@ repowise init --provider claude_cli --model claude-opus-4-6because a subscription is not per-token API spend. The CLI's own reported cost iskept under `usage.reported_cost_usd` for auditing.+The token volume is real and reported: Claude Code splits a prompt across+`input_tokens` (the uncached remainder), `cache_creation_input_tokens` (a cache+write) and `cache_read_input_tokens` (a cache read), and only the first is+usually more than a couple of tokens for a page prompt. Repowise records the sum+as the run's input tokens, so `repowise status` and `repowise costs` show the+prompt the model actually read rather than a figure near zero.+What it does consume is your **subscription rate limits**, the same ones yourinteractive Claude Code sessions use. Budget roughly 9k output tokens and a fewminutes per page. A full wiki on a large repo is a real chunk of usage, so scope