Summary
Capture real-time context window usage data from Claude Code via hooks and display it in supertrace with a visual progress bar per session.
Problem
Currently, supertrace captures token usage from Claude Code transcripts, but the Claude API doesn't return context window percentage. This data IS available internally in Claude Code and exposed via hooks/statusline.
Proposed Solution
Use Claude Code hooks to capture and send context window data to supertrace backend.
Hook JSON Data Available
{
"hook_event_name": "Status",
"session_id": "abc123...",
"transcript_path": "/path/to/transcript.jsonl",
"model": {
"id": "claude-opus-4-5-20251101",
"display_name": "Opus 4.5"
},
"context_window": {
"used_percentage": 42.5,
"remaining_percentage": 57.5,
"context_window_size": 200000,
"total_input_tokens": 15234,
"total_output_tokens": 4521,
"current_usage": {
"input_tokens": 8500,
"output_tokens": 1200,
"cache_creation_input_tokens": 5000,
"cache_read_input_tokens": 2000
}
},
"cost": {
"total_cost_usd": 0.01234,
"total_duration_ms": 45000
}
}
Implementation
Backend:
Hook Script:
Frontend:
Example Progress Bar:
[████░░░░] 45% context used • Opus 4.5
Benefits
- Real-time visibility into context window usage per session
- Proactive warning before context exhaustion
- Better session monitoring and debugging
- Model info display per session
Additional Context
- Hook data is the same JSON that Claude Code statusline receives
- Session matching can use
session_id or transcript_path (both available)
- Only need periodic updates (PreToolUse), not every event
Summary
Capture real-time context window usage data from Claude Code via hooks and display it in supertrace with a visual progress bar per session.
Problem
Currently, supertrace captures token usage from Claude Code transcripts, but the Claude API doesn't return context window percentage. This data IS available internally in Claude Code and exposed via hooks/statusline.
Proposed Solution
Use Claude Code hooks to capture and send context window data to supertrace backend.
Hook JSON Data Available
{ "hook_event_name": "Status", "session_id": "abc123...", "transcript_path": "/path/to/transcript.jsonl", "model": { "id": "claude-opus-4-5-20251101", "display_name": "Opus 4.5" }, "context_window": { "used_percentage": 42.5, "remaining_percentage": 57.5, "context_window_size": 200000, "total_input_tokens": 15234, "total_output_tokens": 4521, "current_usage": { "input_tokens": 8500, "output_tokens": 1200, "cache_creation_input_tokens": 5000, "cache_read_input_tokens": 2000 } }, "cost": { "total_cost_usd": 0.01234, "total_duration_ms": 45000 } }Implementation
Backend:
POST /api/sessions/{session_id}/contextto receive context datasession_idortranscript_pathGET /api/sessions/{session_id}/contextto retrieve latest context dataHook Script:
PreToolUsehook eventFrontend:
Example Progress Bar:
Benefits
Additional Context
session_idortranscript_path(both available)