#!/usr/bin/env bash # # show-impact.sh — Display accumulated impact metrics from the log. # # Usage: ./show-impact.sh [session_id] # Without arguments: shows summary across all sessions. # With session_id: shows entries for that session only. set -euo pipefail PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$(cd "$(dirname "$0")/../.." && pwd)}" LOG_FILE="$PROJECT_DIR/.claude/impact/impact-log.jsonl" if [ ! -f "$LOG_FILE" ]; then echo "No impact log found at $LOG_FILE" echo "The PreCompact hook will create it on first context compaction." exit 0 fi FILTER="${1:-.}" echo "=== Impact Log ===" echo "" while IFS= read -r line; do sid=$(echo "$line" | jq -r '.session_id') if ! echo "$sid" | grep -q "$FILTER"; then continue fi ts=$(echo "$line" | jq -r '.timestamp') trigger=$(echo "$line" | jq -r '.trigger') turns=$(echo "$line" | jq -r '.assistant_turns') tools=$(echo "$line" | jq -r '.tool_uses') source=$(echo "$line" | jq -r '.token_source // "heuristic"') cum_input=$(echo "$line" | jq -r '.cumulative_input_tokens') # Support both old field name and new field name output=$(echo "$line" | jq -r '.output_tokens // .estimated_output_tokens') cache_create=$(echo "$line" | jq -r '.cache_creation_tokens // 0') cache_read=$(echo "$line" | jq -r '.cache_read_tokens // 0') energy=$(echo "$line" | jq -r '.energy_wh') co2=$(echo "$line" | jq -r '.co2_g') cost=$(echo "$line" | jq -r '.cost_cents') printf "%s [%s] session=%s\n" "$ts" "$trigger" "${sid:0:12}..." printf " Turns: %s Tool uses: %s Token source: %s\n" "$turns" "$tools" "$source" printf " Input tokens (cache-weighted): %s Output tokens: %s\n" "$cum_input" "$output" if [ "$cache_create" != "0" ] || [ "$cache_read" != "0" ]; then printf " Cache: %s created, %s read\n" "$cache_create" "$cache_read" fi LC_NUMERIC=C printf " Energy: ~%s Wh CO2: ~%sg Cost: ~\$%.2f\n" "$energy" "$co2" "$(echo "$cost / 100" | bc -l 2>/dev/null || echo "$cost cents")" echo "" done < "$LOG_FILE" # Totals TOTAL_ENERGY=$(jq -s '[.[].energy_wh] | add' "$LOG_FILE") TOTAL_CO2=$(jq -s '[.[].co2_g] | add' "$LOG_FILE") TOTAL_COST=$(jq -s '[.[].cost_cents] | add' "$LOG_FILE") TOTAL_ENTRIES=$(wc -l < "$LOG_FILE") echo "=== Totals ($TOTAL_ENTRIES snapshots) ===" LC_NUMERIC=C printf " Energy: ~%s Wh CO2: ~%sg Cost: ~\$%.2f\n" \ "$TOTAL_ENERGY" "$TOTAL_CO2" \ "$(echo "$TOTAL_COST / 100" | bc -l 2>/dev/null || echo "$TOTAL_COST cents")"