| description | allowed-tools | argument-hint |
|---|---|---|
Show statistics for recent Claude Code sessions (turns, duration, start time) |
Bash |
[number of sessions] |
Show statistics for recent Claude Code sessions.
Run this Python script via Bash. If the user provided a number argument, replace N_REQUESTED = 10 with their number:
import json
from datetime import datetime, timedelta
from pathlib import Path
N_REQUESTED = 10 # Replace with user's argument if provided
projects_dir = Path.home() / ".claude" / "projects"
if not projects_dir.exists():
print("No session data found.")
exit(0)
sessions = []
for jsonl_file in projects_dir.glob("*/*.jsonl"):
try:
turns = 0
first_ts = None
last_ts = None
with open(jsonl_file, 'r') as f:
for line in f:
try:
entry = json.loads(line)
if entry.get('type') == 'assistant':
turns += 1
ts = entry.get('timestamp')
if ts:
if first_ts is None:
first_ts = ts
last_ts = ts
except json.JSONDecodeError:
continue
if first_ts and last_ts and turns > 0:
sessions.append({
'id': jsonl_file.stem,
'project': jsonl_file.parent.name,
'turns': turns,
'start': first_ts,
'end': last_ts,
})
except Exception:
continue
sessions.sort(key=lambda x: x['start'], reverse=True)
now = datetime.now().astimezone()
day_ago = now - timedelta(hours=24)
recent_sessions = [s for s in sessions if datetime.fromisoformat(s['start'].replace('Z', '+00:00')) >= day_ago]
display_sessions = sessions[:min(N_REQUESTED, max(len(recent_sessions), N_REQUESTED))][:N_REQUESTED]
if not display_sessions:
print("No recent sessions found.")
exit(0)
print(f"| {'Session':<12} | {'Turns':>5} | {'Duration':>8} | {'Started':<16} | {'Project':<20} |")
print(f"|{'-'*14}|{'-'*7}|{'-'*10}|{'-'*18}|{'-'*22}|")
total_turns = 0
total_duration = 0
for s in display_sessions:
start_dt = datetime.fromisoformat(s['start'].replace('Z', '+00:00')).astimezone()
end_dt = datetime.fromisoformat(s['end'].replace('Z', '+00:00')).astimezone()
duration = (end_dt - start_dt).total_seconds() / 60
total_turns += s['turns']
total_duration += duration
project = s['project'] if len(s['project']) <= 20 else "..." + s['project'][-17:]
session_id = s['id'][:8] + "..."
print(f"| {session_id:<12} | {s['turns']:>5} | {duration:>6.1f}m | {start_dt.strftime('%Y-%m-%d %H:%M'):<16} | {project:<20} |")
print(f"\nShowing {len(display_sessions)} sessions. Total: {total_turns} turns, {total_duration:.1f} min")Execute this script and display the table to the user. The times shown are in local time.