Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<local-command-caveat>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.</local-command-caveat>", isMeta=True),
user("c2", "c1", "<command-name>/context</command-name>\n"
" <command-message>context</command-message>\n"
" <command-args></command-args>"),
user("c3", "c2", "<local-command-stdout>Context Usage</local-command-stdout>")]
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", "<command-name>/tokeneconomizer:status</command-name>")])
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
Expand Down
21 changes: 19 additions & 2 deletions tokeneconomizer/trace/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<local-command-caveat>")


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


Expand Down