$ recombobulate _
home / tips / track-claude-code-costs-and-duration-in-automated-runs
128

Track Claude Code Costs and Duration in Automated Runs

recombobulate @recombobulate · Mar 26, 2026 · Performance
track-claude-code-costs-and-duration-in-automated-runs

When you run Claude Code with --output-format json, every response includes metadata alongside the result that you can use to monitor your automation costs:

claude -p "Review the latest commit for issues" --output-format json

The JSON envelope always contains:

{
  "result": "The commit looks good overall...",
  "cost_usd": 0.0032,
  "duration_ms": 4812,
  "session_id": "sess_abc123",
  "is_error": false
}

Extract just the cost after every CI run using jq:

output=$(claude -p "Run the security audit" --output-format json)
cost=$(echo "$output" | jq -r '.cost_usd')
echo "Audit complete. Cost: \$$cost"

Fail the build automatically if a single run costs more than expected — a signal that something has gone wrong:

if (( $(echo "$cost > 0.10" | bc -l) )); then
  echo "Warning: unexpectedly high cost — check prompt or turn limits"
  exit 1
fi

Log costs over time to a CSV for cumulative tracking:

echo "$(date),$(echo "$output" | jq -r '.cost_usd'),$(echo "$output" | jq -r '.duration_ms')" \
  >> ci_costs.log

A single run that suddenly costs 10x the usual amount is a strong signal the prompt hit an edge case or the model got stuck in a loop. Tracking this per-run lets you catch it before it becomes a real bill.

Pair with --max-budget-usd as a hard ceiling so costs never exceed your limit even if you forget to check the log.

Per-run cost visibility is the first step to keeping Claude Code automation bills predictable.

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
116
Use /clear to Start Fresh When Your Session Context Gets Confused or Stale

When Claude starts referencing files you've since deleted, remembering old code you've already changed, or getting confused by contradictory instructions from a long session — type /clear to wipe the slate clean. Unlike /compact which preserves context, /clear gives you a true fresh start without restarting the CLI.

recombobulate @recombobulate · 1 month ago
64
Toggle /fast for Quicker Responses on Simple Tasks

Not every task needs deep reasoning. Type /fast to switch Claude Code into fast mode — same model, faster output — for quick edits, simple questions, and routine changes. Toggle it off when you need Claude to think harder on complex problems.

recombobulate @recombobulate · 1 month ago
112
Use /compact to Free Up Context Space When Your Session Gets Long

Long sessions eat through your context window as conversation history piles up. Type /compact to summarize the conversation so far and reclaim space — keeping Claude's understanding of what you're working on while freeing up room for more work.

recombobulate @recombobulate · 1 month ago