Skip to content
Open
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
22 changes: 16 additions & 6 deletions tokentab/providers/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ def _config_dir():
def _extract_text(content) -> str:
if not content:
return ""
parts = [c.get("text", "") for c in content if c.get("type") == "text"]
# Claude Code writes plain-string content for simple turns and a list of
# blocks for everything else; some records also carry non-dict blocks.
if isinstance(content, str):
return content[:500]
parts = [
c.get("text", "")
for c in content
if isinstance(c, dict) and c.get("type") == "text"
]
return " ".join(parts)[:500]


Expand Down Expand Up @@ -68,13 +76,15 @@ def collect() -> list[UsageRecord]:
continue
obj = try_json(line)
msg = obj.get("message") if isinstance(obj, dict) else None
if not msg:
if not isinstance(msg, dict):
continue
if msg.get("role") == "user" and not first_user_text:
first_user_text = _extract_text(msg.get("content"))
for block in msg.get("content") or []:
if block.get("type") == "tool_use" and block.get("name"):
tool_names.append(block["name"])
content = msg.get("content")
if isinstance(content, list):
for block in content:
if isinstance(block, dict) and block.get("type") == "tool_use" and block.get("name"):
tool_names.append(block["name"])
activity = classify(tool_names, first_user_text)

# second pass: token usage from assistant turns
Expand All @@ -83,7 +93,7 @@ def collect() -> list[UsageRecord]:
continue
obj = try_json(line)
msg = obj.get("message") if isinstance(obj, dict) else None
usage = msg.get("usage") if msg else None
usage = msg.get("usage") if isinstance(msg, dict) else None
if not usage:
continue

Expand Down