MacAppKit Guides

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

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:

  1. After each tool call, read the session transcript and count tokens used.
  2. Compare with a cap, for example 5 million per session and 30 million per project.
  3. 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.

LonghaulClaude Code plugin made by the MacAppKit team. Pay what you want, from $9.
See Longhaul

A realistic setup

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.