co-mono/todos/done/20250811-150336-token-usage-tracking.md
Mario Zechner e21a46e68f feat(agent): Add /tokens command for cumulative token usage tracking
Added /tokens slash command to TUI that displays session-wide token statistics.
Key changes:
- Fixed SessionManager to accumulate token usage instead of storing only last event
- Added cumulative token tracking to TUI renderer alongside per-request totals
- Implemented slash command infrastructure with /tokens autocomplete support
- Fixed file autocompletion that was missing from Tab key handling
- Clean minimal display format showing input/output/reasoning/cache/tool counts

The /tokens command shows:
Total usage
   input: 1,234
   output: 567
   reasoning: 89
   cache read: 100
   cache write: 50
   tool calls: 2
2025-08-11 15:43:48 +02:00

2 KiB

Add Token Usage Tracking Command

Status: Done Agent PID: 71159

Original Todo

  • agent: we get token_usage events. the last we get tells us how many input/output/cache read/cache write/reasoning tokens where used for the last request to the LLM endpoint. We want to:
    • have a /tokens command that outputs the accumulative counts, can just add it to the chat messages container as a nicely formatted TextComponent
    • means the tui-renderer needs to keep track of accumulative stats as well, not just last request stats.
    • please check agent.ts (read in full) to see if token_usage is actually some form of accumulative thing, or a per request to llm thing. want to undersatnd what we get.

Description

Add a /tokens slash command to the TUI that displays cumulative token usage statistics for the current session. This includes fixing the SessionManager to properly accumulate token usage and implementing slash command infrastructure in the agent's TUI renderer.

Read analysis.md in full for detailed codebase research and context

Implementation Plan

  • Fix SessionManager to accumulate token usage instead of storing only the last event (packages/agent/src/session-manager.ts:158-160)
  • Add cumulative token tracking properties to TUI renderer (packages/agent/src/renderers/tui-renderer.ts:60-66)
  • Add /tokens slash command to CombinedAutocompleteProvider (packages/agent/src/renderers/tui-renderer.ts:75-80)
  • Modify TUI renderer's onSubmit to handle slash commands locally (packages/agent/src/renderers/tui-renderer.ts:159-177)
  • Implement /tokens command handler that displays formatted cumulative statistics
  • Update token_usage event handler to accumulate totals (packages/agent/src/renderers/tui-renderer.ts:275-291)
  • Test: Verify /tokens command displays correct cumulative totals
  • Test: Send multiple messages and confirm accumulation works correctly
  • Fix file autocompletion that was broken by slash command implementation

Notes

[Implementation notes]