Loading...
Loading...
Analyze claude-trace JSONL files for session health, patterns, and actionable insights. Use when debugging session issues, understanding token usage, or identifying failure patterns.
npx skill4agent add rysweet/amplihack session-replay/transcriptsUser: Analyze my latest session health# Read latest trace file from .claude-trace/
trace_dir = Path(".claude-trace")
trace_files = sorted(trace_dir.glob("*.jsonl"), key=lambda f: f.stat().st_mtime)
latest = trace_files[-1] if trace_files else None
# Parse and analyze
if latest:
analysis = analyze_trace_file(latest)
print(format_session_report(analysis))User: Compare token usage across my last 5 sessionstrace_files = sorted(Path(".claude-trace").glob("*.jsonl"))[-5:]
comparison = compare_sessions(trace_files)
print(format_comparison_table(comparison))health# From each JSONL line containing a request/response pair:
{
"timestamp": "...",
"request": {
"method": "POST",
"url": "https://api.anthropic.com/v1/messages",
"body": {
"model": "claude-...",
"messages": [...],
"tools": [...]
}
},
"response": {
"usage": {
"input_tokens": N,
"output_tokens": N
},
"content": [...],
"stop_reason": "..."
}
}Session Health Report
=====================
File: log-2025-11-23-19-32-36.jsonl
Duration: 45 minutes
Token Usage:
- Input: 125,432 tokens
- Output: 34,521 tokens
- Total: 159,953 tokens
- Efficiency: 27.5% output ratio
Request Stats:
- Total requests: 23
- Average latency: 2.3s
- Errors: 2 (8.7%)
Tool Usage:
- Read: 45 calls
- Edit: 12 calls
- Bash: 8 calls
- Grep: 15 calls
Health Score: 82/100 (Good)
- Minor issue: 2 errors detectederrorsError Analysis
==============
Sessions analyzed: 5
Total errors: 12
Error Categories:
1. Rate limit (429): 5 occurrences
- Recommendation: Add delays between requests
2. Token limit: 3 occurrences
- Recommendation: Use context management skill
3. Tool failures: 4 occurrences
- Bash timeout: 2
- File not found: 2
- Recommendation: Check paths before operationscompareSession Comparison
==================
Session 1 Session 2 Session 3 Trend
Tokens (total) 150K 180K 120K -17%
Requests 25 30 18 -28%
Errors 2 0 1 stable
Duration (min) 45 60 30 -33%
Efficiency 0.27 0.32 0.35 +7%toolsTool Usage Analysis
===================
Tool Calls Avg Time Success Rate
Read 45 0.1s 100%
Edit 12 0.3s 92%
Bash 8 1.2s 75%
Grep 15 0.2s 100%
Task 3 45s 100%
Optimization Opportunities:
1. 5 Read calls to same file within 2 minutes
- Consider caching strategy
2. 3 sequential Bash calls could be parallelized
- Use multiple Bash calls in single messageimport json
from pathlib import Path
from typing import Dict, List, Any
def parse_trace_file(path: Path) -> List[Dict[str, Any]]:
"""Parse a claude-trace JSONL file."""
entries = []
with open(path) as f:
for line in f:
if line.strip():
try:
entry = json.loads(line)
entries.append(entry)
except json.JSONDecodeError:
continue
return entries
def extract_metrics(entries: List[Dict]) -> Dict[str, Any]:
"""Extract session metrics from trace entries."""
metrics = {
"total_input_tokens": 0,
"total_output_tokens": 0,
"request_count": 0,
"error_count": 0,
"tool_usage": {},
"timestamps": [],
}
for entry in entries:
if "request" in entry:
metrics["request_count"] += 1
metrics["timestamps"].append(entry.get("timestamp", 0))
if "response" in entry:
usage = entry["response"].get("usage", {})
metrics["total_input_tokens"] += usage.get("input_tokens", 0)
metrics["total_output_tokens"] += usage.get("output_tokens", 0)
# Check for errors
if entry["response"].get("error"):
metrics["error_count"] += 1
# Extract tool usage from request body
if "request" in entry and "body" in entry["request"]:
body = entry["request"]["body"]
if isinstance(body, dict) and "tools" in body:
for tool in body["tools"]:
name = tool.get("name", "unknown")
metrics["tool_usage"][name] = metrics["tool_usage"].get(name, 0) + 1
return metricsdef find_trace_files(trace_dir: str = ".claude-trace") -> List[Path]:
"""Find all trace files, sorted by modification time."""
trace_path = Path(trace_dir)
if not trace_path.exists():
return []
return sorted(
trace_path.glob("*.jsonl"),
key=lambda f: f.stat().st_mtime,
reverse=True # Most recent first
)def safe_parse_trace_file(path: Path) -> Tuple[List[Dict], List[str]]:
"""Parse trace file with error collection for malformed lines.
Returns:
Tuple of (valid_entries, error_messages)
"""
entries = []
errors = []
if not path.exists():
return [], [f"Trace file not found: {path}"]
try:
with open(path) as f:
for line_num, line in enumerate(f, 1):
if not line.strip():
continue
try:
entry = json.loads(line)
entries.append(entry)
except json.JSONDecodeError as e:
errors.append(f"Line {line_num}: Invalid JSON - {e}")
except PermissionError:
return [], [f"Permission denied: {path}"]
except UnicodeDecodeError:
return [], [f"Encoding error: {path} (expected UTF-8)"]
return entries, errors
def format_error_report(errors: List[str], path: Path) -> str:
"""Format error report for user display."""
if not errors:
return ""
report = f"""
Trace File Issues
=================
File: {path.name}
Issues found: {len(errors)}
"""
for error in errors[:10]: # Limit to first 10
report += f"- {error}\n"
if len(errors) > 10:
report += f"\n... and {len(errors) - 10} more issues"
return report| Scenario | Cause | Handling |
|---|---|---|
| Empty file | Session had no API calls | Report "No data to analyze" |
| Malformed JSON | Corrupted trace or interrupted write | Skip line, count in error report |
| Missing fields | Older trace format | Use |
| Permission denied | File locked by another process | Clear error message, suggest retry |
| Encoding error | Non-UTF-8 characters | Report encoding issue |
| Need | Use This | Why |
|---|---|---|
| "Why was my session slow?" | session-replay | API latency and token metrics |
| "What did I discuss last session?" | /transcripts | Conversation content |
| "Extract learnings from sessions" | CodexTranscriptsBuilder | Knowledge extraction |
| "Reduce my token usage" | session-replay + context_management | Metrics + optimization |
| "Resume interrupted work" | /transcripts | Context restoration |
1. session-replay: Analyze token usage patterns (health action)
2. Identify high-token operations
3. context_management skill: Apply proactive trimming
4. session-replay: Compare before/after sessions (compare action)1. session-replay: Identify error patterns (errors action)
2. /transcripts: Review conversation context around errors
3. session-replay: Check tool usage around failures (tools action)
4. Document findings in DISCOVERIES.md1. session-replay: Analyze 5-10 recent sessions (compare action)
2. Establish baseline metrics (tokens, latency, errors)
3. Track deviations from baseline over time.claude-trace/*.jsonl~/.amplihack/.claude/runtime/logs/<session_id>/claude-trace/transcriptshealtherrorstoolscompare/transcriptsUser: My last session was really slow, analyze it
1. Run health action on latest trace
2. Check request latencies
3. Identify tool bottlenecks
4. Report findings with recommendationsUser: I'm hitting token limits, help me understand usage
1. Compare token usage across sessions
2. Identify high-token operations
3. Suggest context management strategies
4. Recommend workflow optimizationsUser: I keep getting errors, find the pattern
1. Run errors action across last 10 sessions
2. Categorize and count error types
3. Identify root causes
4. Provide targeted fixes.claude-trace//transcriptscontext-management~/.amplihack/.claude/context/PHILOSOPHY.mdAMPLIHACK_USE_TRACE=1.claude-trace/toolsmv .claude-trace/old-*.jsonl .claude-trace/archive/