Parse Task tool result metrics (tokens, tool uses, duration) #115

Closed
opened 2026-02-06 16:13:18 -08:00 by hikari · 1 comment
Owner

Context

Claude Code CLI v2.1.30 added token count, tool uses, and duration metrics to Task tool results. When a subagent completes, the tool_result text now includes usage information like:

<usage>total_tokens: 34324
tool_uses: 33
duration_ms: 199618</usage>

Opportunity

We could parse these metrics from Task tool results and display them in the Agent Monitor Panel, giving Naomi visibility into how much each subagent cost and how long it ran. This would complement our existing agent tracking nicely.

What to Do

  1. When processing tool_result blocks where the originating tool was Task, parse the <usage> block from the result text
  2. Extract total_tokens, tool_uses, and duration_ms
  3. Include these in the AgentEndPayload / AgentInfo types
  4. Display in the Agent Monitor Panel

Impact

This is an enhancement, not a bug fix — our existing agent tracking still works fine without parsing these metrics. But it would be lovely to show per-agent cost/duration data!

References

  • Claude Code CLI v2.1.30 release notes
  • src/lib/types/agents.tsAgentInfo / AgentEndPayload
  • src/lib/components/AgentMonitorPanel.svelte

This issue was created with help from Hikari~ 🌸

## Context Claude Code CLI v2.1.30 added token count, tool uses, and duration metrics to Task tool results. When a subagent completes, the `tool_result` text now includes usage information like: ``` <usage>total_tokens: 34324 tool_uses: 33 duration_ms: 199618</usage> ``` ## Opportunity We could parse these metrics from Task tool results and display them in the Agent Monitor Panel, giving Naomi visibility into how much each subagent cost and how long it ran. This would complement our existing agent tracking nicely. ## What to Do 1. When processing `tool_result` blocks where the originating tool was `Task`, parse the `<usage>` block from the result text 2. Extract `total_tokens`, `tool_uses`, and `duration_ms` 3. Include these in the `AgentEndPayload` / `AgentInfo` types 4. Display in the Agent Monitor Panel ## Impact This is an enhancement, not a bug fix — our existing agent tracking still works fine without parsing these metrics. But it would be lovely to show per-agent cost/duration data! ## References - Claude Code CLI v2.1.30 release notes - `src/lib/types/agents.ts` — `AgentInfo` / `AgentEndPayload` - `src/lib/components/AgentMonitorPanel.svelte` --- ✨ This issue was created with help from Hikari~ 🌸
Author
Owner

Investigation Results 🔍

I've investigated this issue and here's what I found!

Original Approach Not Feasible

The <usage> block does not appear in Task tool result content from the CLI. I verified this by:

  • Adding debug logging to our parsing code
  • Testing with CLI v2.1.34
  • Checking all CLI flags (no flag to enable this)

The CLI simply doesn't output usage metrics in ToolResult content, and likely never will.

Alternative: Parse Session JSONL Files

The data DOES exist in Claude Code's session logs!

Location: ~/.claude/projects/.../subagents/agent-{id}.jsonl

Format: Each assistant message includes full usage data:

{
  "message": {
    "usage": {
      "input_tokens": 3,
      "cache_creation_input_tokens": 540,
      "cache_read_input_tokens": 83670,
      "output_tokens": 1
    }
  },
  "agentId": "ae365ab",
  "timestamp": "2026-01-22T00:11:23.927Z"
}

📊 Complexity Analysis

Implementation Difficulty: Moderate (6/10)

What We'd Need:

  1. JSONL parser in Rust (easy - 2/10)
  2. 🤔 Subagent file discovery (medium - 5/10)
  3. 🤔 Usage aggregation across messages (medium - 6/10)
  4. 🔴 Real-time monitoring with file watchers (hard - 8/10)
  5. 🔴 Edge case handling (hard - 7/10)

Estimated Time: 4-8 hours

Pros:

  • Accurate token counts from Anthropic's records
  • Historical data available
  • No CLI modifications needed
  • Works with current CLI

Cons:

  • Moderately complex implementation
  • File watching/polling overhead
  • Race conditions with file writes
  • Relies on undocumented file format (could break)
  • Timing challenges (when is file ready?)

💡 Recommendation

Keep this issue open as a future enhancement, but acknowledge it requires:

  1. Parsing session JSONL files (not live stream)
  2. File watching or polling
  3. Accepting risk of format changes

Current state: We already show duration_ms from hooks, which provides useful visibility!

📚 References

Tools that successfully parse session files:

This comment was written by Hikari~ 🌸

## Investigation Results 🔍 I've investigated this issue and here's what I found! ### ❌ Original Approach Not Feasible The `<usage>` block does **not** appear in Task tool result content from the CLI. I verified this by: - Adding debug logging to our parsing code - Testing with CLI v2.1.34 - Checking all CLI flags (no flag to enable this) The CLI simply doesn't output usage metrics in ToolResult content, and likely never will. ### ✅ Alternative: Parse Session JSONL Files **The data DOES exist** in Claude Code's session logs! **Location**: `~/.claude/projects/.../subagents/agent-{id}.jsonl` **Format**: Each assistant message includes full usage data: ```json { "message": { "usage": { "input_tokens": 3, "cache_creation_input_tokens": 540, "cache_read_input_tokens": 83670, "output_tokens": 1 } }, "agentId": "ae365ab", "timestamp": "2026-01-22T00:11:23.927Z" } ``` ### 📊 Complexity Analysis **Implementation Difficulty**: Moderate (6/10) **What We'd Need**: 1. ✅ JSONL parser in Rust (easy - 2/10) 2. 🤔 Subagent file discovery (medium - 5/10) 3. 🤔 Usage aggregation across messages (medium - 6/10) 4. 🔴 Real-time monitoring with file watchers (hard - 8/10) 5. 🔴 Edge case handling (hard - 7/10) **Estimated Time**: 4-8 hours **Pros**: - ✅ Accurate token counts from Anthropic's records - ✅ Historical data available - ✅ No CLI modifications needed - ✅ Works with current CLI **Cons**: - ❌ Moderately complex implementation - ❌ File watching/polling overhead - ❌ Race conditions with file writes - ❌ Relies on undocumented file format (could break) - ❌ Timing challenges (when is file ready?) ### 💡 Recommendation **Keep this issue open** as a future enhancement, but acknowledge it requires: 1. Parsing session JSONL files (not live stream) 2. File watching or polling 3. Accepting risk of format changes **Current state**: We already show `duration_ms` from hooks, which provides useful visibility! ### 📚 References Tools that successfully parse session files: - [ccusage](https://github.com/ryoppippi/ccusage) - CLI usage analysis - [Claude JSONL Browser](https://github.com/withLinda/claude-JSONL-browser) - Web viewer - [Analyzing logs with DuckDB](https://liambx.com/blog/claude-code-log-analysis-with-duckdb) ✨ This comment was written by Hikari~ 🌸
naomi closed this issue 2026-03-06 23:49:45 -08:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nhcarrigan/hikari-desktop#115