Loading...
API latency breakdown monitor showing network time vs processing time split, p95 latency tracking, and performance bottleneck detection for Claude Code sessions.
#!/usr/bin/env bash
# API Latency Breakdown Monitor for Claude Code
# Shows network/waiting time vs actual API processing time
# Read JSON from stdin
read -r input
# Extract values
total_duration_ms=$(echo "$input" | jq -r '.cost.total_duration_ms // 0')
api_duration_ms=$(echo "$input" | jq -r '.cost.total_api_duration_ms // 0')
# Calculate network/waiting time (total - API)
network_time=$((total_duration_ms - api_duration_ms))
# Avoid negative values
if [ $network_time -lt 0 ]; then
network_time=0
fi
# Convert to seconds for display
api_seconds=$(echo "scale=2; $api_duration_ms / 1000" | bc)
network_seconds=$(echo "scale=2; $network_time / 1000" | bc)
total_seconds=$(echo "scale=2; $total_duration_ms / 1000" | bc)
# Calculate percentage split
if [ $total_duration_ms -gt 0 ]; then
api_percentage=$(( (api_duration_ms * 100) / total_duration_ms ))
network_percentage=$(( 100 - api_percentage ))
else
api_percentage=0
network_percentage=0
fi
# Performance assessment (network time should be minimal)
if [ $network_time -lt 1000 ]; then # < 1 second network time
PERF_COLOR="\033[38;5;46m" # Green: Excellent
PERF_STATUS="✓ FAST"
elif [ $network_time -lt 5000 ]; then # < 5 seconds
PERF_COLOR="\033[38;5;226m" # Yellow: Moderate
PERF_STATUS="⚠ SLOW"
else
PERF_COLOR="\033[38;5;196m" # Red: Poor performance
PERF_STATUS="✗ BOTTLENECK"
fi
RESET="\033[0m"
# Build ratio bar (API vs Network)
API_BAR="\033[48;5;75m" # Blue background for API time
NET_BAR="\033[48;5;214m" # Orange background for network time
# Create 20-char bar showing split
api_chars=$((api_percentage / 5)) # Each char = 5%
net_chars=$((network_percentage / 5))
if [ $api_chars -gt 0 ]; then
api_bar=$(printf "${API_BAR} %.0s" $(seq 1 $api_chars))${RESET}
else
api_bar=""
fi
if [ $net_chars -gt 0 ]; then
net_bar=$(printf "${NET_BAR} %.0s" $(seq 1 $net_chars))${RESET}
else
net_bar=""
fi
# Output statusline
echo -e "${PERF_COLOR}${PERF_STATUS}${RESET} | API: ${api_seconds}s (${api_percentage}%) | Network: ${network_seconds}s (${network_percentage}%) | ${api_bar}${net_bar}"{
"format": "bash",
"position": "left",
"refreshInterval": 1000
}Network time showing negative or zero despite slow responses
Verify both cost.total_duration_ms and cost.total_api_duration_ms fields exist: echo '$input' | jq .cost. Negative protection caps network_time at 0. If both values missing, script shows 0s - check Claude Code version supports these fields.
API percentage always showing 100% with no network time
This indicates total_duration_ms equals total_api_duration_ms (no measurable network overhead). Verify calculations: network_time = total - API. If consistently 0, either network is extremely fast or fields are identical in JSON. Check actual JSON values.
Performance status showing BOTTLENECK incorrectly
Thresholds: <1s green (FAST), 1-5s yellow (SLOW), >5s red (BOTTLENECK). Adjust thresholds in script if your network baseline differs. VPN users may see higher normal latency. Modify: network_time -lt 5000 to higher value for VPN environments.
Ratio bar not displaying or showing as empty
Bar uses ANSI background colors (\033[48;5;Xm). Ensure terminal supports 256-color mode: tput colors (should return 256). If bars show as spaces only, verify ANSI escape codes working: echo -e '\033[48;5;75m BLUE \033[0m'.
bc: command not found when calculating percentages
Install bc: brew install bc (macOS), apt install bc (Linux). Alternative: use integer math only: api_percentage=$((api_duration_ms * 100 / total_duration_ms)) - removes decimal precision but works without bc.
Loading reviews...