Claude Code Token 使用量优化的 7 个实战技巧
通过 7 个经过验证的 Token 优化技巧降低 Claude Code 成本。学习 /compact、提示词缓存、子代理策略和模型切换。
The more you use Claude Code, the more you start watching your token usage and costs. This article shares 7 practical techniques that reduce costs while improving speed.
How Token Consumption Works
Claude Code includes the entire conversation history in context. The longer the session, the more tokens each turn consumes. Controlling this consciously is the first step to cost reduction.
Technique 1: Compact the context with /compact
The most basic move. When the conversation grows, run /compact.
> /compact
It summarizes the past conversation and dramatically reduces context size. The context is preserved in summary form, so you can keep working without breaking flow.
Rule of thumb: Run /compact once after 50 turns.
Technique 2: /clear when no longer needed
When a task fully switches, use /clear instead of /compact.
> /clear
Wipes history completely and starts from scratch. No previous task context remains, so you can focus on the new one.
See also Claude Code Getting Started Guide.
Technique 3: Shorten prompts with CLAUDE.md
Repeating the same setup in every prompt is a token waste. Put project info into CLAUDE.md and your instructions get shorter.
# CLAUDE.md
## Project Overview
TypeScript + Next.js 15 e-commerce site. Tailwind CSS, Prisma + PostgreSQL.
## Coding Conventions
- Use async/await (avoid Promise.then)
- Use absolute imports (@/...)
- Tests in Vitest
## Common Commands
- npm run dev
- npm run test
- npm run build
Just having this cuts each prompt to about 1/3 of its previous length. See CLAUDE.md Best Practices for more.
Technique 4: Pass only what’s needed via pipe
Don’t have Claude Code read the entire file. Pipe only the relevant slice.
# Bad: pass the entire file
claude -p "Improve formatDate in src/utils/helpers.ts"
# Good: extract just the function
sed -n '/^export function formatDate/,/^}/p' src/utils/helpers.ts | claude -p "Improve this function"
Same with error logs.
# Pass only the last 100 lines
tail -n 100 production.log | claude -p "Identify the cause of errors"
Technique 5: Save main context with subagents
Delegate long research and large file exploration to subagents.
Spawn 3 subagents in parallel and investigate:
1. List of endpoints in src/api/
2. Main components in src/components/
3. Coverage status in tests/
Each agent should return a summary.
Only summaries return to the main context, saving significant tokens. See Subagent Activation Patterns for more.
Technique 6: Choose the right model for the job
Switch models based on task complexity.
# Simple work with Haiku
claude --model claude-haiku-4-5 -p "Translate the comments in this file"
# Complex design with Sonnet
claude --model claude-sonnet-4-5 -p "Redesign this architecture"
# Critical tasks with Opus
claude --model claude-opus-4-6 -p "Diagnose the root cause of this performance issue"
Haiku runs at about 1/4 the cost of Sonnet, so using it for simple tasks delivers serious savings. See Claude Code Pricing Guide for more.
Technique 7: Check /cost frequently
Run /cost mid-session to see current token usage and cost.
> /cost
Total tokens: 45,231
Estimated cost: $0.42
When you notice “wait, that’s more than I expected”, reset immediately with /compact or /clear.
Daily Workflow Example
A daily routine combining all of these.
# Morning: start a new task
$ claude
> Read CLAUDE.md and confirm today's tasks
# Mid-day: after 50 turns
> /compact
# Noon: switch to a different task
> /clear
> Starting refactoring on a different feature
# Evening: check costs
> /cost
> /compact
# Night: continue tomorrow
> Use /resume to pick up where you left off
Conclusion
- Reset context periodically with
/compactand/clear - Shorten prompts with CLAUDE.md
- Pipe only the relevant data
- Parallelize and save context with subagents
- Match the model to the task
- Check
/costfrequently
Making these habits cuts monthly costs in half or better. See the Anthropic official docs for further best practices.
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。