Verified Claude Code
Use -p with JSON output in scripts to get the cost of each run
What it does
Non-interactive -p with --output-format json returns total_cost_usd, is_error and num_turns, handy for batch jobs and cost tracking.
How
claude -p "Summarize this repo's README" --output-format json
Read result and total_cost_usd from the JSON.
How much to trust this
| Applies to | ≥ 2.0.28 |
|---|---|
| Verified | Oct 17, 2026 · v2.1.296 · tested locally 本机实际运行,返回 JSON 含 total_cost_usd |
| Depends on | -p--output-format |
| Source | https://code.claude.com/docs/en/cli-reference (official_docs) |