iTranslated by AI
Solve Claude Code's Context Remaining Issue with the Status Line — Always Show Your Branch Name
What is Claude Code's Status Line?
When working with Claude Code, I can't help but constantly worry about the remaining context. Many people find themselves thinking, "It's almost time to compact..." or "I hope the rate limits are okay..." in the back of their minds.
In fact, Claude Code has a status line feature that allows you to display any information you like at the bottom of the terminal at all times. By default, it only shows the model name you're using, but you can freely customize it with shell scripts.
This article introduces how to display context usage, rate limits, and Git branch names on the status line. The final result looks like this:
Opus │ ctx 23% │ 5h 12% 7d 3% │ master
When the context exceeds 80%, it turns red, so you can instantly see when to run /compact.
What You Can See with the Default Status Line
Without any configuration, the status line only shows the model name. Information about context remaining and rate limits is not visible without deliberate effort.
It's unfortunate to continue working without noticing that the context is about to overflow, or to only realize you've hit the rate limit after being blocked. By displaying these details constantly, you can better pace your work.
Configuring the Status Line in settings.json
Customization of the status line is done via the statusLine field in ~/.claude/settings.json. By specifying type: "command", you can display the output of any shell command on the status line.
{
"statusLine": {
"type": "command",
"command": "bash ~/.claude/scripts/statusline.sh"
}
}
Claude Code periodically executes this command and reflects its output in the status line. The command receives JSON via standard input, from which you can retrieve model information and context usage.
Check the Contents of the Input JSON
First, let's take a peek at what information is available using jq.
#!/usr/bin/env bash
cat | jq .
If you set this as the status line command, you'll see that the following JSON is passed:
{
"model": {
"display_name": "Claude Sonnet 4 (1M context)",
"api_name": "claude-sonnet-4-20250514"
},
"context_window": {
"used_percentage": 12.5
},
"rate_limits": {
"five_hour": {
"used_percentage": 8.3
},
"seven_day": {
"used_percentage": 2.1
}
}
}
The main fields are as follows (field names may change depending on the Claude Code version):
-
model.display_name— The model name in use (for display) -
context_window.used_percentage— Context window usage percentage (%) -
rate_limits.five_hour.used_percentage— 5-hour rate limit usage percentage (%) -
rate_limits.seven_day.used_percentage— 7-day rate limit usage percentage (%)
Implementation Example: Color-Coded Display of Context Remaining, Rate Limits, and Branch Name
The following shell script displays the model name, context usage, rate limits, and Git branch name with color coding.
#!/usr/bin/env bash
input=$(cat)
# ANSI color codes
D='\033[2m' # dim
Y='\033[33m' # yellow
R='\033[31m' # red
RST='\033[0m' # reset
SEP="${D}│${RST}"
# Function to colorize based on usage
colorize() {
local val="$1"
if [ -z "$val" ]; then echo "--"; return; fi
local n=$(printf '%.0f' "$val")
local s="${n}%"
if [ "$n" -ge 80 ]; then
printf "${R}%s${RST}" "$s" # 80% or more: red
elif [ "$n" -ge 50 ]; then
printf "${Y}%s${RST}" "$s" # 50% or more: yellow
else
echo "$s" # below: normal color
fi
}
# Shorten model name (e.g., "Claude Sonnet 4 (1M context)" → "Sonnet 4")
model=$(echo "$input" | jq -r '.model.display_name // "--"' \
| sed -E 's/ \([^)]+\)//; s/ [0-9.]*$//; s/^Claude //')
# Get each usage percentage
ctx=$(echo "$input" | jq -r '.context_window.used_percentage // empty')
five=$(echo "$input" | jq -r '.rate_limits.five_hour.used_percentage // empty')
week=$(echo "$input" | jq -r '.rate_limits.seven_day.used_percentage // empty')
# Git branch name (warning in red for branches other than master/main)
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "??")
if [ "$branch" != "master" ] && [ "$branch" != "main" ]; then
branch=$(printf "${R}%s${RST}" "$branch")
fi
# Output
echo -e "${model} ${SEP} ctx $(colorize "$ctx") ${SEP} 5h $(colorize "$five") 7d $(colorize "$week") ${SEP} ${branch}"
Script Highlights
Shortening the Model Name: The display_name is quite long as is, so we use sed -E (extended regex) to remove information in parentheses (like context size), version numbers, and the "Claude" prefix to make it shorter.
Color Thresholds: The color switches to yellow at 50% usage and red at 80%. When the context turns red, it's a good time to consider /compact; when the rate limit turns red, it's a signal to slow down your pace.
Branch Name Warning: Branches other than master/main are displayed in red. This helps prevent accidentally working on the wrong branch.
Customization Tips
The information you can display on the status line is not limited to this. Since it's a shell script, you can add any information you like.
-
Uncommitted Changes — Display
*if there are differences withgit status --porcelain -
Project Name — Display the current directory name with
basename $(pwd) -
Current Time — Show
date +%H:%Mas a marker for long work sessions - Custom Flags — Check for the existence of specific files to display environment status
The key is to limit the status line to only the information you truly need to see at all times, as space is limited. Packing too much in can make it harder to read.
Summary
By using Claude Code's statusLine configuration, you can continuously display context remaining and rate limits in your terminal. It can be set up with just one shell script, so customizing it to match your workflow seems like a great idea.
Discussion