我如何避免 Claude Code 用量觸及上限
How I Stopped Hitting Claude Code Usage Limits
Md Ayan bag 分析自己 Mac 上的 Claude Code 工作階段記錄,整理出 9 項降低 token 用量、避免觸及使用限制的方法。
"You've hit your usage limit." I saw that message three times in one week, every time in the middle of a task, with half-written code open in front of me.
So I stopped guessing and measured. I wrote a small script that reads Claude Code's own session logs on my Mac. The result was embarrassing: my median session peaked at 252,696 tokens. 160 of my 230 sessions that week went past 150k. My largest hit 967k.
Note: I used Claude Code to help build the audit script and draft this post. The numbers and screenshots are from my own machine.
Here is what was actually eating my limit, and the nine fixes that work. Most of them are free and take five minutes.
Why Claude Code usage limits drain so fast
Claude Code re-sends the whole conversation to the model on every turn.
Your CLAUDE.md, every MCP tool definition, every file it opened, every test log it printed, every message you sent. Turn 80 pays for turns 1 to 79 again.
So your limit isn't eaten by your questions. It's eaten by context size times number of turns. A session sitting at 200k tokens carries ten times more per turn than one at 20k.
Every fix below shrinks one of those two numbers.
1. Put the limit where you can see it
You can't manage what you only see when it's too late. Claude Code's status line receives your context percentage and, on Pro and Max, your 5-hour and weekly limit usage. Most people never turn it on.
Save this as ~/.claude/gauge.py:
import json, sys
d = json.load(sys.stdin)
ctx = (d.get("context_window") or {}).get("used_percentage") or 0
rl = d.get("rate_limits") or {}
five = (rl.get("five_hour") or {}).get("used_percentage")
week = (rl.get("seven_day") or {}).get("used_percentage")
print(f"ctx {ctx:.0f}% | 5h {five or 0:.0f}% | week {week or 0:.0f}%")
Then add this to ~/.claude/settings.json and restart:
"statusLine": {"type": "command", "command": "python3 ~/.claude/gauge.py"}
Now you see the problem building instead of meeting it at 100%.
2. One task per session, then /clear
This was my biggest leak. 137 of my sessions ran 80+ model turns, because I kept working in the same window all afternoon.
Finished a feature? Run /clear before the next one. Mid-task and context is above 60%? Ask Claude to write a short handoff note to a file (goal, what's done, next steps, key files), then /clear and point the new session at that note.
/compact vs /clear: compacting keeps a summary in the same session. It's fine once. Do it four times and you're dragging a blurry, still-heavy history around. A 40-line handoff plus a clear is cheaper, and you decide what survives.
3. Stop Claude from reading junk files
One accidental read of a lockfile or a minified bundle can add tens of thousands of tokens that ride along for the rest of the session.
Add deny rules to your project's .claude/settings.json:
{
"permissions": {
"deny": [
"Read(./node_modules/**)", "Read(./dist/**)", "Read(./build/**)",
"Read(./package-lock.json)", "Read(./pnpm-lock.yaml)", "Read(./**/*.min.js)"
]
}
}
And name exact places in your prompts. "Fix the null check in src/lib/auth.ts around getUser" costs a fraction of "fix the login bug".
4. Keep CLAUDE.md under about 1,000 tokens
CLAUDE.md is loaded on every single turn. So is every CLAUDE.md in parent folders, which surprised me: mine was quietly pulling in a 1,800-token file from my home folder.
Keep it to the stack, the commands, where things live, and the few gotchas that bit you before. Move long docs into a docs/ folder and reference them by path. Claude opens them only when it needs them.
5. Turn off MCP servers you aren't using
Every MCP server injects its full tool list into context, on every turn. Run /mcp and disable what this project doesn't need. Three focused servers beat twelve "just in case" ones. The same goes for plugins you installed once and forgot.
6. Plan before Claude writes code
Press Shift+Tab for plan mode on anything non-trivial. Fixing a wrong approach after 40k tokens of code costs far more than reading a 2k-token plan and saying "no, do it this way".
Batch your asks, too. One message with three clear changes beats three back-and-forth turns that each re-send everything.
7. Use a cheaper model for mechanical work
Use your heaviest model for design and hard bugs, and switch with /model for renames, tests and docs.
Subagents can run on a lighter model too. Add model: haiku to an agent's frontmatter in ~/.claude/agents/ and send it the searching and boilerplate. The noise stays out of your main context.
8. Make Claude write less code
Ponytail is a free, MIT-licensed skill that pushes Claude to reuse what exists and skip speculative abstractions, without dropping validation or security. Its published benchmark on real Claude Code tasks shows about 54% less code and about 22% fewer tokens. I've had it on for weeks.
9. Measure every week
Your feeling about usage is wrong. Mine was. Claude Code keeps every session as a JSONL file under ~/.claude/projects/, with token counts on every response, so you can measure exactly how big your sessions get.
That's what my audit script does. It prints what your setup costs before you type a word, then your real session sizes for the week.
What I packaged up
Doing all of this by hand on every machine got old, so I turned my setup into a kit. Full disclosure: I built it and sell it.
It has the full limit gauge with colours and reset timers, a hook that blocks junk reads automatically, a /handoff command that saves the note and reloads it after /clear, the audit script, a local cost dashboard, a lean CLAUDE.md template and a 3-page playbook. It installs with one command and uninstalls with one command.
Everything in this article works without it. If you'd rather have it set up in two minutes, it's here: Claude Code Limit Saver Kit.
The short version
Your limit isn't spent on your questions. It's spent on carrying context. Watch the gauge, clear between tasks, block junk reads, keep CLAUDE.md lean, and measure.
What's your worst session? Run the numbers and tell me in the comments. I read every one.
Originally published on Medium.
來源:DEV Community · dev.to