How to limit Claude Code token usage on long runs
A short Claude Code session costs little. A long one, such as a big refactor, an overnight task or a loop that retries, can use millions of tokens before you notice. This guide covers practical ways to keep that under control: habits that lower usage, ways to watch it, and a hard stop for when you are away from the keyboard.
Why long runs get expensive
Each turn sends the conversation so far back to the model. Prompt caching makes repeated context cheaper to read, but cached reads still count toward your usage, and every file read, command output and tool result adds to what is resent. A session that goes 300 turns deep resends a lot. Long runs also tend to retry, and a loop that repeats the same failing step spends tokens without making progress.
Habits that reduce usage
- One task per session. Use
/clearwhen you switch tasks so old context is not carried along. - Compact on purpose.
/compactreplaces history with a summary. You can pass instructions on what to keep. Doing it at a natural break beats letting it happen mid-step. - Keep tool output small. Ask for
head,tailor filtered output instead of dumping whole logs or large files into the conversation. - Be specific about files. "Fix the bug in src/parser.ts" costs less than letting the agent explore the whole repository.
- Check where it goes.
/contextshows what is filling the window, and/costshows what the session has used. Runclaude --helpto see current options for non-interactive runs, since some versions have flags that cap turns or spend. - Choose the model for the job. Simple edits rarely need your most expensive model.
Watch it live: a status line
Claude Code can show a custom status line that runs a command and prints the result. It passes your session data to that command as JSON on standard input, including context window use, rate limit use and cost. A one-line status that shows context percent and cost makes a runaway session visible early. Add it to ~/.claude/settings.json:
"statusLine": { "type": "command", "command": "python3 ~/tools/longhaul/statusline.py" }
A hard stop: a budget hook
Habits and displays rely on you looking. For unattended runs you want a limit that acts by itself. Claude Code hooks let you run a script on events such as PostToolUse and Stop, and a hook can tell Claude to stop with a reason. A budget guard does this:
- After each tool call, read the session transcript and count tokens used.
- Compare with a cap, for example 5 million per session and 30 million per project.
- At 80%, warn once. At 100%, stop with a clear message.
The details matter: a transcript repeats the same message across streamed lines, so you must count each message once, and reading the whole file on every call gets slow when it reaches tens of megabytes, so read only what was added since last time. A project-wide total also needs to survive restarts.
Longhaul does exactly this
Longhaul is a Claude Code plugin made by the MacAppKit team. Its budget guard counts input, cache-write, cache-read and output tokens for each API message once, reads the transcript incrementally and keeps project totals across sessions, so a task that restarts ten times still has one budget. You set the caps in .longhaul/budget.json inside your project:
{ "session_cap": 5000000, "project_cap": 30000000, "warn_at": 0.8 }
It warns at the level you set (80% by default) and stops Claude cleanly at 100% with a reason. It includes the status line described above and a ledger that survives compaction (see our guide on keeping state across compaction), plus a cleanup guard that nudges Claude to stop leftover dev servers and temp files before saying it is done. It uses only the Python standard library, makes no network calls, and runs on macOS and Linux with Python 3.8 or later. Try it for one session with claude --plugin-dir ~/tools/longhaul and run /hooks to confirm it loaded. Add .longhaul/ to your .gitignore.
Longhaul is pay-what-you-want, from $9. The ledger part is free and MIT-licensed on GitHub.
See Longhaul
A realistic setup
- Set a session cap you can afford to lose to a bad loop, and a project cap for the whole task.
- Use the status line so you can see the bar fill during interactive work.
- Write the next step to a file before you walk away, so a stop at the cap does not lose your place.
- When the cap triggers, read why, trim the context or fix the loop, then raise the cap only on purpose.
A cap does not make Claude smarter; it limits the cost of a mistake. That is exactly why it is worth setting before a long run.