diff --git a/tests/test_trace.py b/tests/test_trace.py
index 485b7bb..c1f2113 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 ea79b13..e1962dc 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