Last active
June 22, 2026 19:00
-
-
Save chiradeep/b348f36fd41b53445127ab5ecf9737d2 to your computer and use it in GitHub Desktop.
Claude Code status line to show real time cost, context and cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # ~/.claude/statusline-command.sh | |
| input=$(cat) | |
| # --- model --- | |
| model=$(echo "$input" | jq -r '.model.display_name // ""') | |
| # --- folder --- | |
| dir=$(echo "$input" | jq -r '.workspace.current_dir // .cwd // ""') | |
| dir_name=$(basename "$dir") | |
| # --- git branch --- | |
| branch="" | |
| if [ -d "${dir}/.git" ] || git -C "$dir" rev-parse --git-dir > /dev/null 2>&1; then | |
| branch=$(git -C "$dir" symbolic-ref --short HEAD 2>/dev/null || git -C "$dir" rev-parse --short HEAD 2>/dev/null) | |
| fi | |
| # --- session cost --- | |
| cost=$(echo "$input" | jq -r '.cost.total_cost_usd // empty') | |
| cost_str="" | |
| [ -n "$cost" ] && cost_str=$(printf '$%.2f' "$cost") | |
| # --- context window --- | |
| used=$(echo "$input" | jq -r '.context_window.used_percentage // empty') | |
| ctx_str="" | |
| ctx_tokens_str="" | |
| if [ -n "$used" ]; then | |
| used_int=$(printf "%.0f" "$used") | |
| ctx_str="${used_int}%" | |
| ctx_used=$(echo "$input" | jq -r '(.context_window.current_usage.cache_read_input_tokens + .context_window.current_usage.cache_creation_input_tokens + .context_window.current_usage.input_tokens + .context_window.current_usage.output_tokens) // empty' 2>/dev/null) | |
| ctx_total=$(echo "$input" | jq -r '.context_window.context_window_size // empty' 2>/dev/null) | |
| if [ -n "$ctx_used" ] && [ -n "$ctx_total" ]; then | |
| ctx_used_k=$(( ctx_used / 1000 )) | |
| ctx_total_k=$(( ctx_total / 1000 )) | |
| ctx_tokens_str="${ctx_used_k}k/${ctx_total_k}k" | |
| fi | |
| # auto-compact kicks in around 83-84% (16.5% buffer); suggest compacting before that | |
| if [ "$used_int" -ge 85 ]; then | |
| compact_str="compact now" | |
| compact_color="\033[38;2;255;85;85m" | |
| elif [ "$used_int" -ge 70 ]; then | |
| compact_str="compact now" | |
| compact_color="\033[38;2;255;193;7m" | |
| fi | |
| fi | |
| # --- cache TTL countdown (based on transcript file mtime) --- | |
| CACHE_TTL=300 | |
| ttl_str="" | |
| ttl_color="" | |
| transcript_path=$(echo "$input" | jq -r '.transcript_path // empty') | |
| if [ -n "$transcript_path" ] && [ -f "$transcript_path" ]; then | |
| last_mod=$(stat -f %m "$transcript_path" 2>/dev/null || stat -c %Y "$transcript_path" 2>/dev/null) | |
| now=$(date -u +%s) | |
| if [ -n "$last_mod" ]; then | |
| elapsed=$(( now - last_mod )) | |
| remaining=$(( CACHE_TTL - elapsed )) | |
| if [ "$remaining" -gt 0 ]; then | |
| mins=$(( remaining / 60 )) | |
| secs=$(( remaining % 60 )) | |
| ttl_str=$(printf "ttl %dm%02ds" "$mins" "$secs") | |
| if [ "$remaining" -le 60 ]; then | |
| ttl_color="\033[38;2;255;193;7m" | |
| else | |
| ttl_color="\033[38;2;156;162;175m" | |
| fi | |
| else | |
| ttl_str="cache expired" | |
| ttl_color="\033[38;2;255;85;85m" | |
| fi | |
| fi | |
| fi | |
| # --- assemble output --- | |
| SEP="\033[90m • \033[0m" | |
| # line 1: model | folder • branch | |
| printf "\033[38;5;208m\033[1m%s\033[22m\033[0m" "$model" | |
| printf "\033[90m | \033[0m" | |
| printf "\033[1m\033[38;2;76;208;222m%s\033[22m\033[0m" "$dir_name" | |
| if [ -n "$branch" ]; then | |
| printf "%b" "$SEP" | |
| printf "\033[1m\033[38;2;192;103;222m%s\033[22m\033[0m" "$branch" | |
| fi | |
| # line 2: cost • ctx • ttl • compact | |
| printf "\n" | |
| first=1 | |
| if [ -n "$cost_str" ]; then | |
| printf "\033[38;2;156;162;175m%s\033[0m" "$cost_str" | |
| first=0 | |
| fi | |
| if [ -n "$ctx_str" ]; then | |
| [ "$first" -eq 0 ] && printf "%b" "$SEP" | |
| printf "\033[38;2;156;162;175mctx %s\033[0m" "$ctx_str" | |
| [ -n "$ctx_tokens_str" ] && printf " \033[2m\033[38;2;156;162;175m(%s)\033[0m" "$ctx_tokens_str" | |
| first=0 | |
| fi | |
| if [ -n "$ttl_str" ]; then | |
| [ "$first" -eq 0 ] && printf "%b" "$SEP" | |
| printf "%b%s\033[0m" "$ttl_color" "$ttl_str" | |
| first=0 | |
| fi | |
| if [ -n "$compact_str" ]; then | |
| [ "$first" -eq 0 ] && printf "%b" "$SEP" | |
| printf "%b%s\033[0m" "$compact_color" "$compact_str" | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this to ~/.claude/settings.json
"statusLine": {
"type": "command",
"command": "bash ~/.claude/statusline-command.sh",
"refreshInterval": 10
},