Skip to content

Instantly share code, notes, and snippets.

@chefgs
Last active October 6, 2026 08:56
Show Gist options
  • Select an option

  • Save chefgs/f9f35ccb9d30a779e753c8cab58f6bf2 to your computer and use it in GitHub Desktop.

Select an option

Save chefgs/f9f35ccb9d30a779e753c8cab58f6bf2 to your computer and use it in GitHub Desktop.
Claude Code token usage: status line + per-request Stop hook (local scripts, zero model tokens)

Claude Code token usage: status line + per-request report

A personal setup that shows token usage in Claude Code without spending any model tokens. Both pieces are local shell scripts that read the session transcript.

  • statusline.sh is an always-visible status line: Sonnet 5.5 | ctx 146.5k | out 7.3k | calls 15 | $1.01
  • token-report.sh is a Stop hook that prints a one-line breakdown after each response: tokens this request: in 6 | out 1742 | cache-read 190957 | cache-write 88736 | api-calls 2 | context now ~140871

Install

mkdir -p ~/.claude/hooks
cp statusline.sh token-report.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/*.sh

Merge this into ~/.claude/settings.json (keep your existing keys; do not replace the file):

{
  "statusLine": {
    "type": "command",
    "command": "~/.claude/hooks/statusline.sh"
  },
  "hooks": {
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "~/.claude/hooks/token-report.sh", "timeout": 10 }
        ]
      }
    ]
  }
}

If nothing appears, open /hooks once or restart Claude Code.

Why

You only see usage if you remember to run /context or /cost, and in the middle of work you won't. This puts the numbers on screen all the time, so you can:

  • clear the context before answers get worse
  • notice which prompts are expensive
  • never be surprised by usage or cost

The status line is always on. The Stop hook adds one extra line after each reply. If that's too much, remove the hook and keep the status line.

Fields

Field Meaning
ctx Size of the most recent prompt, including cached content. Drops after /clear or compaction.
out Cumulative output tokens for the session.
calls Model calls in the session.
$ Session cost reported by Claude Code. An estimate, not an invoice. Omitted if not supplied.
cache-read / cache-write Prompt cache traffic. Usually the largest numbers.

Requirements

jq and awk. If jq is missing, both scripts print a short "jq not installed" message and exit cleanly. Tested on macOS only.

Caveats

  • It reads the session transcript (JSONL), which is not a documented stable API. A Claude Code update could change the field names and break the numbers.
  • The status line re-parses the whole transcript on every refresh, so it can get slower in very long sessions.
  • Per-request totals may not include subagent calls.

Uninstall

Remove the statusLine and hooks blocks from ~/.claude/settings.json, and delete the scripts.

#!/usr/bin/env bash
# Status line: model | context size | session totals | cost. Local only, uses no model tokens.
set -u
command -v jq >/dev/null 2>&1 || { printf 'statusline: jq not installed'; exit 0; }
in=$(cat)
t=$(jq -r '.transcript_path // empty' <<<"$in")
model=$(jq -r '.model.display_name // .model.id // "?"' <<<"$in")
cost=$(jq -r '.cost.total_cost_usd // empty' <<<"$in")
fmt() { awk -v n="$1" 'BEGIN{ if(n>=1000000) printf "%.1fM",n/1e6; else if(n>=1000) printf "%.1fk",n/1e3; else printf "%d",n }'; }
ctx=0; out=0; calls=0
if [ -n "$t" ] && [ -f "$t" ]; then
read -r ctx out calls < <(jq -rs '
map(select(.type=="assistant" and .message.usage != null)) | unique_by(.message.id) as $a
| [ ($a | last.message.usage // {} | ((.input_tokens // 0) + (.cache_read_input_tokens // 0) + (.cache_creation_input_tokens // 0))),
($a | map(.message.usage.output_tokens // 0) | add // 0),
($a | length) ] | @tsv' "$t" 2>/dev/null)
fi
line="$model | ctx $(fmt "${ctx:-0}") | out $(fmt "${out:-0}") | calls ${calls:-0}"
[ -n "$cost" ] && line="$line | \$$(printf '%.2f' "$cost")"
printf '%s' "$line"
#!/usr/bin/env bash
# Stop hook: print token usage for the last request (since the last real user prompt).
# Reads the hook JSON on stdin, sums usage from the session transcript. Uses no model tokens.
set -u
command -v jq >/dev/null 2>&1 || { echo '{"systemMessage":"token-report: jq not installed"}'; exit 0; }
t=$(jq -r '.transcript_path // empty')
[ -n "$t" ] && [ -f "$t" ] || exit 0
jq -rs '
def real_user: .type=="user" and (.isSidechain|not) and (.isMeta|not)
and ((.message.content|type)=="string"
or ((.message.content|map(.type)|index("tool_result"))==null));
(to_entries | map(select(.value|real_user)) | last | .key // 0) as $start
| .[$start:]
| map(select(.type=="assistant" and .message.usage != null))
| unique_by(.message.id)
| { calls: length,
input: (map(.message.usage.input_tokens // 0) | add // 0),
output: (map(.message.usage.output_tokens // 0) | add // 0),
cache_read: (map(.message.usage.cache_read_input_tokens // 0) | add // 0),
cache_write: (map(.message.usage.cache_creation_input_tokens // 0) | add // 0),
ctx: (last.message.usage // {} | ((.input_tokens // 0) + (.cache_read_input_tokens // 0) + (.cache_creation_input_tokens // 0))) }
| "tokens this request: in \(.input) | out \(.output) | cache-read \(.cache_read) | cache-write \(.cache_write) | api-calls \(.calls) | context now ~\(.ctx)"
' "$t" 2>/dev/null | jq -Rc '{systemMessage: .}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment