Skip to content
NewA realistic 3D head for a free swim cap editor
Nov 9, 2026·4 MIN READ
A Claude Code status line that shows the cost of each request

A Claude Code status line that shows the cost of each request

I rewrote my Claude Code status line in one sitting: hostname, context left in tokens, session cost in USD and PLN, the cost of the current request, and the weekly plan limit. The whole thing is a bash script that reads one JSON blob.

I work on two Macs and I kept losing track of which one a Claude Code session was running on. The fix was small: put the hostname in the status line. Once I had the script open, I kept going. By the end of the hour the bottom line of every session looked like this:

status line
m1 ~/work/acme/acme-shop (main*) | Fable 5.1 | ctx 88% (123k/1M) | $6.18/24.21zł (+$0.14/0.56zł) | 7d 52%

Everything comes from one JSON blob

Claude Code runs the status line command after every update and pipes a JSON document into it. The script I had only read the directory, the model name and the context percentage. I dumped the whole document to a file once to see what else is in there, and it is a lot more than the docs made me expect.

FieldWhat it holdsWhere it ends up
workspace.current_dirthe folder the session works in~/work/acme/acme-shop
model.display_namethe modelFable 5.1
context_window.remaining_percentagefree context, in percentctx 88%
context_window.current_usageinput, cache write and cache read tokens123k
context_window.context_window_sizethe window size for this model and plan1M
cost.total_cost_usdrunning cost of the session at API prices$6.18
rate_limits.seven_day.used_percentagehow much of the weekly plan limit is used7d 52%
prompt_ida new id for every message I sendthe +$0.14 trick below

The two surprises were the window size and the cost. I had assumed 200k tokens; the field said 1,000,000. And the cost is already there, computed by Claude Code, so nobody has to multiply tokens by a price list.

The percentage is in tokens, not bytes. A token is about four characters of English, so a 300-line source file costs 3,000 to 5,000 tokens and a long git log can cost more than the whole conversation so far.

Cost per request from a running total

Claude Code only sends the total for the session. I wanted to see what the message I just sent is costing while it runs. The prompt_id field made that a ten-line addition: the script keeps a small state file per session, and when it sees a new prompt id it saves the current total as the base. The bracket shows total minus base, and it grows live while the agent works.

flowchart LR
    CC["Claude Code<br/>JSON after every update"] --> SH["statusline-command.sh"]
    SH --> ST["state file<br/>prompt id, base cost"]
    ST --> SH
    SH --> NBP["NBP API<br/>USD/PLN mid rate,<br/>cached 12 h"]
    SH --> LINE["one coloured line"]
statusline-command.sh, the per-request part
session_id=$(echo "$input" | jq -r '.session_id // "default"')
prompt_id=$(echo "$input" | jq -r '.prompt_id // empty')
state="${TMPDIR:-/tmp}/claude-statusline-$session_id"

[ -f "$state" ] && IFS=$'\t' read -r saved_prompt saved_base saved_total < "$state"
if [ "$saved_prompt" = "$prompt_id" ]; then
  base=$saved_base
else
  base=${saved_total:-$cost}   # new prompt: what was spent so far belongs to earlier requests
fi
printf '%s\t%s\t%s\n' "$prompt_id" "$base" "$cost" > "$state"
delta=$(echo "$cost - $base" | bc -l)

The PLN part is the same idea with a cache. The script asks the NBP API for the USD mid rate, stores it in ~/.cache, and refreshes it in the background when the file is older than twelve hours. The status line never waits on the network; the first run after an expiry shows USD only for a second, then both currencies.

Colour carries most of the information. The context percentage is green above 50, yellow above 20, red below. The weekly limit goes yellow at 70 and red at 90. The cost is always red, which is the point. The whole line renders in about 0.15 seconds, which matters because it runs after every update.

What it took

Most of the hour went into one thing I should have done first: looking at the real input instead of guessing field names from memory. One echo "$input" > file line answered every question I had, including the one about the window size, and it also showed me the cost and rate limit fields I did not know existed. The rest was bash, jq and bc. Next time a tool hands me a JSON document, I dump it before I write a single line against it.

Share
Mariusz Smenżyk
Written by

Mariusz Smenżyk

Builder for sport. Make. Test. Launch.

Related notes