Token Tracking Architecture Specification
Task: T2897 Epic: T2163 Date: 2026-02-01 Status: COMPLETE Schema Version: 1.0.0Summary
This specification defines a hybrid token tracking architecture with two tracking layers: session-level for total consumption (actual Claude Code data) and spawn-level for task attribution (estimated tokens). The system integrates with Claude Code’s OpenTelemetry metrics when available, falling back to estimation when not.1. Architecture Overview
1.1 Two-Tier Tracking System
The architecture implements two independent tracking layers:1.2 Why NOT File-Ops Level
File operations tracking was explicitly rejected because:- Too noisy: Every
atomic_write()call generates metrics - Wrong granularity: File writes don’t correlate to task work
- Performance impact: Would slow down all write operations
- No value signal: Cannot attribute tokens to specific tasks
2. Session-Level Tracking (Tier 1)
2.1 Data Source
Claude Code exposes context state via.claude/context-state.json:
2.2 Session Lifecycle Hooks
2.3 Output Format (SESSIONS.jsonl)
3. Spawn-Level Attribution (Tier 2)
3.1 Purpose
Attribute token usage to specific tasks by estimating prompt and output sizes at spawn boundaries.3.2 Tracking Points
| Event | Tokens Tracked | Estimation Method |
|---|---|---|
spawn_start | Prompt tokens | wc -c / 4 |
spawn_end | Output tokens | Manifest entry size |
manifest_read | Summary tokens | Entry JSON size |
skill_inject | Skill tokens | Skill file size |
3.3 Output Format (TOKEN_USAGE.jsonl)
4. Integration Points
4.1 OpenTelemetry (When Available)
4.2 Manifest System
5. Metrics Dashboard
5.1 Available Commands
5.2 Dashboard Output
6. Configuration
6.1 Enable Tracking
6.2 Environment Variables
References
- METRICS-VALIDATION: Complete metrics system
- METRICS-VALUE-PROOF: Value demonstration
