You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Was curious how
/statsworks in Claude Code so I got Claude to reverse engineer it (on v2.0.69). Thought I'd share it here.● Summary: How /stats Works in Claude Code Location and Definition The /stats command is defined in the bundled cli.js at approximately line 4383 (minified/bundled): { type: "local-jsx", name: "stats", description: "Show your Claude Code usage statistics and activity", isEnabled: () => true, isHidden: false, async call(A, Q, B) { return createElement(StatsComponent, { onClose: A }) } } Data Source Stats are collected from .jsonl session log files stored in: - Base path: ~/.claude/projects/ - Structure: Each project gets a directory named with dashes replacing slashes (e.g., -home-ivan-Documents-tasks) - Session files: UUID-named .jsonl files like 0746bf28-45ff-4d23-8f44-592da6a7026d.jsonl Data Collection (kTA function) The stats processor reads each session file and extracts: 1. From each session: - Session ID, duration, message count, timestamp - Tool call counts - Hourly activity distribution 2. From assistant messages (type: "assistant"): - input_tokens - output_tokens - cache_read_input_tokens - cache_creation_input_tokens - web_search_requests (from server_tool_use) - Model name Stats Object Structure (eX5 empty template) { totalSessions: 0, totalMessages: 0, totalDays: 0, activeDays: 0, streaks: { currentStreak: 0, longestStreak: 0, currentStreakStart: null, longestStreakStart: null, longestStreakEnd: null }, dailyActivity: [], // GitHub-style contribution data dailyModelTokens: [], // Token usage by model per day longestSession: null, modelUsage: {}, // Per-model token counts firstSessionDate: null, lastSessionDate: null, peakActivityDay: null, peakActivityHour: null } Per-Model Usage Tracking (modelUsage) { inputTokens: 0, outputTokens: 0, cacheReadInputTokens: 0, cacheCreationInputTokens: 0, webSearchRequests: 0, costUSD: 0, contextWindow: 0 } UI Tabs The stats UI has 2 main tabs: 1. Overview - Summary stats including: - Total sessions count - Longest session duration - Current/longest streaks (days) - Activity contribution graph (GitHub-style) 2. Models - Per-model token usage breakdown Caching Stats are cached to avoid reprocessing all historical data: - Cache file: stats-cache.json in Claude config dir - Cache version: 1 (versioned for compatibility) - Incremental updates: Only processes new data since lastComputedDate Key Functions | Function | Purpose | |----------|-----------------------------------------------| | Ag2() | Main loader - orchestrates data collection | | eh2() | Gets list of session .jsonl files | | kTA() | Processes sessions and extracts stats | | _Y0() | Reads stats cache | | QQA() | Saves stats cache | | sX5() | Merges historical stats with today's activity | | SY0() | Renders activity contribution graph | ● That's the complete reverse engineering of /stats. The command reads session logs from ~/.claude/projects/*/, parses the JSONL files to extract message counts, token usage per model, session durations, and displays it in an interactive UI with Overview and Models tabs plus a GitHub-style activity contribution graph.Beta Was this translation helpful? Give feedback.
All reactions