compaction-detector
БесплатноЗапускает вложенные скриптыНе проверенDetect Claude Code context compaction events in session JSONL logs. Identifies compaction boundaries, measures token delta before/after, reports compaction…
от oimiragieo★ 48 звёзд на GitHubОб этом скилле
Compaction Detector
Step 1: Locate the Session Log File
Claude Code session logs are stored as JSONL files. Find the target log:
# Default log location (adjust path for your OS)
# macOS / Linux
ls -lt ~/.claude/projects/*/logs/*.jsonl | head -5
# Windows (Git Bash / WSL)
ls -lt "/c/Users/$USER/.claude/projects/"*/logs/*.jsonl 2>/dev/null | head -5
# Or search by project path hash
find ~/.claude/projects -name "*.jsonl" -newer /tmp/sentinel 2>/dev/null
Expected output: One or more .jsonl file paths with modification timestamps.
Verify: File is non-empty — wc -l <path> should return > 0.
Step 2: Identify Compaction Boundary Lines
Each line in a Claude Code session JSONL is a JSON object. Compaction events are identified by a sharp drop in usage.input_tokens between consecutive turns — the context was summarised and reset to a smaller window.
Command — extract token counts with line numbers:
SESSION_LOG="<absolute-path-to-session.jsonl>"
grep -n '"input_tokens"' "$SESSION_LOG" \
| awk -F'[":,]' '{
for(i=1;i<=NF;i++) {
if($i ~ /input_tokens/) { print NR, $(i+2); break }
}
}'
Simpler alternative using jq (if available):
jq -r 'select(.usage.input_tokens != null) | [.timestamp, .usage.input_tokens, .usage.output_tokens] | @tsv' \
"$SESSION_LOG"
Expected output: Tab-separated rows: <timestamp> <input_tokens> <output_tokens>
Step 3: Detect Token Drop Events
A compaction event occurs when input_tokens[N] < input_tokens[N-1] * 0.5 (tokens dropped by more than 50%).
Command — detect drops with awk:
SESSION_LOG="<absolute-path-to-session.jsonl>"
grep '"input_tokens"' "$SESSION_LOG" \
| grep -oP '"input_tokens"\s*:\s*\K[0-9]+' \
| awk '
NR > 1 {
pct = ($1 / prev) * 100
if (pct < 50) {
printf "COMPACTION at line %d: %d -> %d tokens (%.1f%% retained)\n", \
NR, prev, $1, pct
}
}
{ prev = $1 }
'
Expected output:
COMPACTION at line 47: 98234 -> 8102 tokens (8.2% retained)
COMPACTION at line 203: 112450 -> 9341 tokens (8.3% retained)
Verify: Each reported line number corresponds to a real turn boundary. Cross-check with sed -n '<line>p' "$SESSION_LOG" | jq .timestamp.
Step 4: Extract Timestamps for Each Compaction
For each compaction line number identified in Step 3, extract the ISO timestamp:
SESSION_LOG="<absolute-path-to-session.jsonl>"
COMPACTION_LINE=47 # Replace with actual line number
# Extract timestamp from that JSONL line
sed -n "${COMPACTION_LINE}p" "$SESSION_LOG" \
| grep -oP '"timestamp"\s*:\s*"\K[^"]+'
Alternative with jq:
sed -n "${COMPACTION_LINE}p" "$SESSION_LOG" | jq -r '.timestamp // "unknown"'
Expected output: 2026-03-21T14:32:07.441Z
Step 5: Compute Token Delta Per Compaction Event
For each compaction boundary, calculate:
tokens_before: input_tokens on the line immediately before the droptokens_after: input_tokens on the compaction linedelta: tokens_before - tokens_afterretention_pct: (tokens_after / tokens_before) * 100
Full pipeline — produces structured TSV:
SESSION_LOG="<absolute-path-to-session.jsonl>"
paste \
<(grep -n '"input_tokens"' "$SESSION_LOG" | grep -oP '^\d+') \
<(grep '"input_tokens"' "$SESSION_LOG" | grep -oP '"input_tokens"\s*:\s*\K[0-9]+') \
| awk '
NR > 1 {
delta = prev_tokens - $2
pct = ($2 / prev_tokens) * 100
if (pct < 50) {
printf "%s\t%d\t%d\t%d\t%.1f\n", \
prev_line, $1, prev_tokens, $2, pct
}
}
{ prev_line = $1; prev_tokens = $2 }
' \
| column -t -s $'\t' \
-N "BOUNDARY_LINE,COMPACTION_LINE,TOKENS_BEFORE,TOKENS_AFTER,RETENTION_PCT"
Expected output:
BOUNDARY_LINE COMPACTION_LINE TOKENS_BEFORE TOKENS_AFTER RETENTION_PCT
46 47 98234 8102 8.2
202 203 112450 9341 8.3
Step 6: Produce Structured Report
Emit the compaction report as JSON to stdout or save to a file:
SESSION_LOG="<absolute-path-to-session.jsonl>"
OUTPUT_FILE=".claude/context/tmp/compaction-report-$(date +%Y%m%d-%H%M%S).json"
python3 - "$SESSION_LOG" "$OUTPUT_FILE" <<'PYEOF'
import json, sys,
Установить compaction-detector в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
ReadBashGrepTaskUpdateВложенные файлы
FAQ
Что делает скилл compaction-detector?
Detect Claude Code context compaction events in session JSONL logs. Identifies compaction boundaries, measures token delta before/after, reports compaction events with timestamps and token impact.
Как установить скилл compaction-detector?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл compaction-detector запускает скрипты?
Да, скилл несёт исполняемые скрипты. Проверь исходник перед установкой.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic