From 7087ad433d9507435c196c56e66129728e1b52e5 Mon Sep 17 00:00:00 2001 From: Saksham Uboweja <76595406+SakshamUboweja@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:54:35 +0000 Subject: [PATCH] trace: leave Claude Code's local commands out of the prompt count Claude Code runs a local command (/context, /usage, /clear, ...) itself and writes it as three user entries: a meta , the entry as its child, and the command's output. `_is_prompt` skips the first and the last but accepts the command, so a session that only ran /context folded as one prompt with no model request, and tool pruning counted every catalog tool as unused through it. `claude_prompt_count` now skips an entry whose parent is such a caveat. A prompt-type slash command (/tokeneconomizer:status) has no caveat and still counts; `claude_prompt_turns` is unchanged. --- tests/test_trace.py | 27 +++++++++++++++++++++++++++ tokeneconomizer/trace/claude.py | 21 +++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/test_trace.py b/tests/test_trace.py index 485b7bb3..c1f21132 100644 --- a/tests/test_trace.py +++ b/tests/test_trace.py @@ -205,6 +205,33 @@ def user(content, **flags): assert trace.claude_prompt_count(p) == 3 +def test_a_claude_local_command_is_no_prompt(tmp_path): + """Claude Code runs a local command (`/context`, `/usage`, `/clear`, ...) itself and writes + it as a meta caveat, the command as the caveat's child, and its output (2.1.325). The model + never sees it, so a session that only ran one gave no prompt. A prompt-type slash command + has no caveat and still counts.""" + def user(uuid, parent, content, **flags): + return dict({"type": "user", "uuid": uuid, "parentUuid": parent, + "message": {"role": "user", "content": content}}, **flags) + local = [ + user("c1", None, "Caveat: The messages below were generated by" + " the user while running local commands. DO NOT respond to these messages or" + " otherwise consider them in your response unless the user explicitly asks you" + " to.", isMeta=True), + user("c2", "c1", "/context\n" + " context\n" + " "), + user("c3", "c2", "Context Usage")] + assert trace.claude_prompt_count(_jsonl(tmp_path / "l.jsonl", local)) == 0 + p = _jsonl(tmp_path / "m.jsonl", [ + user("u1", None, "fix the test"), + {"type": "assistant", "uuid": "a1", "parentUuid": "u1", + "message": {"role": "assistant", "content": "done"}}, + *local, + user("u2", "c3", "/tokeneconomizer:status")]) + assert trace.claude_prompt_count(p) == 2 + + def test_a_claude_background_task_notification_is_no_prompt(tmp_path): """Claude Code tells the model that a background task finished in a `user` entry of its own, `origin.kind` "task-notification" (2.1.317). The person typed one prompt here. The diff --git a/tokeneconomizer/trace/claude.py b/tokeneconomizer/trace/claude.py index ea79b136..e1962dc3 100644 --- a/tokeneconomizer/trace/claude.py +++ b/tokeneconomizer/trace/claude.py @@ -95,16 +95,33 @@ def _is_notification(ev): return isinstance(origin, dict) and origin.get("kind") == "task-notification" +def _is_local_command_caveat(ev): + """Is this the meta entry Claude Code writes before a local command it runs itself + (`/context`, `/usage`, `/clear`, ...)? The command's own entry is its child, and the + model never sees it (2.1.325: "was run directly in Claude Code, not sent to you").""" + if ev.get("type") != "user" or not ev.get("isMeta"): + return False + content = (ev.get("message") or {}).get("content") + return isinstance(content, str) and content.startswith("") + + def claude_prompt_count(transcript_path): """How many prompts the person gave a Claude Code session (`_is_prompt`, less the - background-task notifications).""" + background-task notifications and the local commands Claude Code ran itself).""" n = 0 + caveats = set() for line in _lines(transcript_path): try: ev = json.loads(line) except ValueError: continue - n += isinstance(ev, dict) and _is_prompt(ev) and not _is_notification(ev) + if not isinstance(ev, dict): + continue + if _is_local_command_caveat(ev) and isinstance(ev.get("uuid"), str): + caveats.add(ev["uuid"]) + parent = ev.get("parentUuid") + n += (_is_prompt(ev) and not _is_notification(ev) + and not (isinstance(parent, str) and parent in caveats)) return n