# Model Switch History Tracker Claude Code model switch detector tracking transitions between Opus/Sonnet/Haiku with switch count, current model indicator, and cost impact visualization. --- ## Metadata **Title:** Model Switch History Tracker **Category:** statuslines **Author:** JSONbored **Added:** October 2025 **Tags:** model-switching, opus-sonnet-haiku, cost-optimization, model-tracking, switch-history **URL:** https://claudepro.directory/statuslines/model-switch-history-tracker ## Overview Claude Code model switch detector tracking transitions between Opus/Sonnet/Haiku with switch count, current model indicator, and cost impact visualization. ## Content #!/usr/bin/env bash MODEL SWITCH HISTORY TRACKER FOR CLAUDE CODE TRACKS TRANSITIONS BETWEEN CLAUDE MODELS (OPUS, SONNET, HAIKU) READ JSON FROM STDIN read -r input EXTRACT CURRENT MODEL INFO currentmodel=$(echo "$input" | jq -r '.model.displayname // "unknown"') sessionid=$(echo "$input" | jq -r '.sessionid // "unknown"') MODEL HISTORY TRACKING DIRECTORY HISTORY_DIR="${HOME}/.claude-code-model-history" mkdir -p "$HISTORY_DIR" SESSION-SPECIFIC HISTORY FILE HISTORYFILE="${HISTORYDIR}/${session_id}.history" INITIALIZE HISTORY FILE IF DOESN'T EXIST if [ ! -f "$HISTORY_FILE" ]; then echo "${currentmodel}" > "$HISTORYFILE" echo "0" >> "$HISTORY_FILE" # Switch count fi READ PREVIOUS MODEL AND SWITCH COUNT previousmodel=$(sed -n '1p' "$HISTORYFILE") switchcount=$(sed -n '2p' "$HISTORYFILE") DETECT MODEL SWITCH if [ "$currentmodel" != "$previousmodel" ] && [ "$previous_model" != "" ]; then # Model changed - increment switch count switchcount=$((switchcount + 1)) # Log switch event (append to history) echo "$(date +%s)|${previousmodel}→${currentmodel}" >> "$HISTORY_FILE" fi UPDATE CURRENT MODEL AND SWITCH COUNT (OVERWRITE FIRST 2 LINES) tempfile="${HISTORYFILE}.tmp" echo "${currentmodel}" > "$tempfile" echo "${switchcount}" >> "$tempfile" tail -n +3 "$HISTORYFILE" >> "$tempfile" 2>/dev/null mv "$tempfile" "$HISTORYFILE" DETERMINE MODEL TIER AND COLOR case "$current_model" in "Opus"|"opus") MODEL_COLOR="\[38;5;201m" # Magenta: Opus (most expensive) MODEL_ICON="💎" MODEL_TIER="OPUS" ;; "Sonnet"|"sonnet") MODEL_COLOR="\[38;5;75m" # Blue: Sonnet (balanced) MODEL_ICON="🎵" MODEL_TIER="SONNET" ;; "Haiku"|"haiku") MODEL_COLOR="\[38;5;46m" # Green: Haiku (cheapest) MODEL_ICON="🍃" MODEL_TIER="HAIKU" ;; *) MODEL_COLOR="\[38;5;250m" # Gray: Unknown MODEL_ICON="❓" MODEL_TIER="UNKNOWN" ;; esac SWITCH FREQUENCY INDICATOR if [ $switch_count -eq 0 ]; then SWITCH_STATUS="stable" SWITCH_COLOR="\[38;5;46m" # Green: No switches elif [ $switch_count -le 3 ]; then SWITCHSTATUS="${switchcount} switches" SWITCH_COLOR="\[38;5;226m" # Yellow: 1-3 switches else SWITCHSTATUS="${switchcount} switches!" SWITCH_COLOR="\[38;5;196m" # Red: 4+ switches (frequent switching) fi GET LAST 3 SWITCHES FOR MINI-HISTORY lastswitches=$(tail -n 3 "$HISTORYFILE" | grep '→' | cut -d'|' -f2 | tr '\n' ' ' | sed 's/ $//') if [ -n "$last_switches" ]; then HISTORYDISPLAY="| ${lastswitches}" else HISTORY_DISPLAY="" fi RESET="\[0m" OUTPUT STATUSLINE echo -e "${MODELICON} ${MODELCOLOR}${MODELTIER}${RESET} | ${SWITCHCOLOR}${SWITCHSTATUS}${RESET} ${HISTORYDISPLAY}" KEY FEATURES ? Real-time model detection for Opus, Sonnet, and Haiku variants ? Switch count tracking showing total model changes in session ? Last 3 switches mini-history showing transition patterns (Opus→Sonnet→Haiku) ? Color-coded model tiers (magenta Opus, blue Sonnet, green Haiku) ? Switch frequency warnings (green stable, yellow 1-3, red 4+ switches) ? Persistent history storage per session in ~/.claude-code-model-history ? Cost awareness through model tier visualization ? Automatic switch event logging with timestamps CONFIGURATION Format: bash Refresh Interval: 1000ms Position: left USE CASES ? Cost optimization by tracking Opus vs Sonnet vs Haiku usage patterns ? Identifying unnecessary model switching that inflates costs ? Understanding which tasks require which model tier ? Budget management through model tier awareness ? Debugging unexpected model changes in Claude Code settings ? Analyzing session patterns for optimal model selection strategy TROUBLESHOOTING 1) Model always showing UNKNOWN despite valid Claude model running Solution: Check model.displayname field: echo '$input' | jq .model.displayname. Script matches case-insensitively on 'Opus', 'Sonnet', 'Haiku' keywords. If model name doesn't contain these (e.g., 'claude-3-5-sonnet-'), add custom matching: 'claude-3-5-sonnet') MODEL_TIER='SONNET'. Verify field exists and has expected format. 2) Switch count not incrementing when changing models Solution: Verify sessionid is consistent: echo '$input' | jq .sessionid. Each session has separate history file. If sessionid changes, switch count resets (expected). Check history file exists: ls ~/.claude-code-model-history/${sessionid}.history. Manually test: echo 'Opus' > file.history && echo '0' >> file.history. 3) Permission denied when creating model history directory Solution: Ensure HOME environment variable is set: echo $HOME. Check write permissions: mkdir -p ~/.claude-code-model-history. If permission denied, change location: HISTORY_DIR='/tmp/claude-model-history-$(whoami)'. Verify statusline script runs with correct user permissions. 4) Last 3 switches history not displaying transitions Solution: History format is 'timestamp|ModelA→ModelB' (e.g., '|Opus→Sonnet'). Check file content: tail ~/.claude-code-model-history/${session_id}.history. Grep filter looks for '→' character - ensure terminal encoding supports Unicode arrow. Alternative: replace → with ASCII '->' in script. 5) Switch frequency showing incorrect count Solution: Switch count stored on line 2 of history file. Verify: sed -n '2p' ~/.claude-code-model-history/${sessionid}.history. Count only increments when currentmodel != previousmodel AND previousmodel is not empty. New sessions start at 0 (expected). Manually reset: echo '0' to line 2 if corrupted. TECHNICAL DETAILS Documentation: https://docs.claude.com/en/docs/claude-code/statusline PREVIEW 🎵 SONNET | 2 switches | Opus→Sonnet Haiku→Sonnet --- Source: Claude Pro Directory Website: https://claudepro.directory URL: https://claudepro.directory/statuslines/model-switch-history-tracker This content is optimized for Large Language Models (LLMs). For full formatting and interactive features, visit the website.