Token Budget Management Script to Prevent Cloud Code API Bill Shocks
1. Blocking Overages by Setting Token Limits
If you've ever been shocked by an API bill while running a small project, it's because you didn't have budget limits in place. Agents maintain longer conversation contexts than you might think and repeatedly read unnecessary file contents. If left unchecked, hundreds of thousands of tokens will disappear in a single day. You need to open your ~/.claude/settings.json file and directly limit session and daily usage.
Here is how to add the budget object to your user global settings file:
- Open your terminal and use an editor to open the ~/.claude/settings.json file.
- Inside the JSON block, enter a sessionLimit of 500000 and a dailyLimit of 2000000.
- Set warningThreshold to 0.75 and autoCompactAt to 0.60, then save.
When the context capacity reaches 60 percent, the conversation history is automatically compacted, eliminating unnecessary token waste. Since sessions are blocked before reaching the daily limit, you can stop worrying about going over budget.
2. Building a Loop Breaker to Force Stop on 3 Consecutive Errors
If an agent gets stuck in the same hook due to a syntax error or file path issue, it will run in an infinite loop without resolving it. Leaving this state unchecked will exhaust your session tokens in a matter of minutes. Simply writing "be careful" in your prompt is useless. You must bind a shell script-based hook system to physically block it.
Create a hook script that immediately terminates the process if the same error is detected 3 times in a row.
- Create a .claude/hooks/loop-breaker.sh file in your project directory and grant it execution permissions.
- Inside the script, reference a temporary state file to increment the error counter by 1. If the cumulative count is 3 or greater, output standard error and return an exit code of 2 to forcefully stop the task.
- Register the script path under the PostToolUseFailure entry in your .claude/settings.json file to monitor it in real time.
By setting up this mechanism, the phenomenon where an agent endlessly fixes incorrect code and burns through tokens disappears. You can save time previously wasted in debugging hell.
3. Separating Model Routing and Manual Approval Stages by Task Nature
If you leave all coding tasks entirely to Claude, your budget won't survive. Delegate simple formatting or boilerplate writing to lightweight models, and require developers to manually approve irreversible tasks like database migrations. Utilize shell aliases to build an execution environment tailored to the difficulty of the task.
Here is how to configure separate agent delegation commands by task nature:
- Open your ~/.bashrc or ~/.zshrc file.
- Register the command
alias cc-quick='claude --model claude-haiku-3-20250307 --token-budget 100000' for simple documentation tasks.
- Add
alias cc-dev='claude --model claude-sonnet-4-20250514 --token-budget 500000 --thinking-budget 8000' for general feature development, then restart your terminal.
Applying this method allows the agent to safely handle 90 percent of simple repetitive coding, and the ratio of completed PRs per 1 million tokens increases noticeably. It is the most realistic way to reliably cut unnecessary API costs in half.