Skip to content

Instantly share code, notes, and snippets.

@markus1189
Created August 23, 2025 10:09
Show Gist options
  • Select an option

  • Save markus1189/3f609877d3b52c779d093449418149e5 to your computer and use it in GitHub Desktop.

Select an option

Save markus1189/3f609877d3b52c779d093449418149e5 to your computer and use it in GitHub Desktop.
Calculate Claude Code context length from transcript JSONL using bash/jq
#\!/usr/bin/env nix
#\! nix shell nixpkgs#bash nixpkgs#jq nixpkgs#coreutils --command bash
set -euo pipefail
# Get context length from Claude Code transcript JSONL file
transcript_file="${1:-}"
if [[ -z "$transcript_file" || \! -f "$transcript_file" ]]; then
echo "Usage: $0 <transcript.jsonl>" >&2
exit 1
fi
# Find the most recent main chain entry (isSidechain \!= true) and extract context length
jq -r '
select(.message.usage and (.isSidechain \!= true)) |
{
timestamp: .timestamp,
context_length: (
(.message.usage.input_tokens // 0) +
(.message.usage.cache_read_input_tokens // 0) +
(.message.usage.cache_creation_input_tokens // 0)
)
}
' "$transcript_file" |
jq -s 'sort_by(.timestamp) | last | .context_length'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment