Tracking Costs
Claude Code uses tokens, and tokens cost money. Here’s how to track and optimize your usage.
Understanding Tokens
Section titled “Understanding Tokens”Tokens are the units Claude uses to process text:
- ~4 characters = 1 token (English)
- ~100 tokens = 75 words
- A typical code file = 500-2000 tokens
Cost factors:
- Input tokens (what you send to Claude)
- Output tokens (what Claude generates)
- Context (accumulated conversation history)
Using CC Usage
Section titled “Using CC Usage”The best way to track costs is ccusage:
-
Install ccusage
Terminal window npx ccusageOr install globally:
Terminal window npm install -g ccusage -
View your usage
Terminal window npx ccusageThis shows:
- Total tokens used today
- Estimated cost
- Breakdown by session
-
Check specific time periods
Terminal window npx ccusage --days 7 # Last 7 daysnpx ccusage --month # This month
Sample Output
Section titled “Sample Output”╭─────────────────────────────────────────────────────╮│ Claude Code Usage Report │├─────────────────────────────────────────────────────┤│ Period: Today (Jan 5, 2026) ││ ││ Sessions: 12 ││ Input tokens: 145,230 ││ Output tokens: 42,891 ││ Total tokens: 188,121 ││ ││ Estimated cost: $2.34 │╰─────────────────────────────────────────────────────╯Token Usage by Activity
Section titled “Token Usage by Activity”Different activities use different amounts of tokens:
| Activity | Typical Token Usage |
|---|---|
| Simple question | 500-1,000 |
| Explain a file | 2,000-5,000 |
| Bug fix | 5,000-15,000 |
| Feature implementation | 20,000-50,000 |
| Full project scan | 50,000-200,000 |
Optimizing Token Usage
Section titled “Optimizing Token Usage”1. Start Fresh Sessions
Section titled “1. Start Fresh Sessions”Don’t continue conversations forever. Start new sessions for new tasks:
# New task? New session.claude "new task: add user authentication"2. Use /compact
Section titled “2. Use /compact”When context grows large, compress it:
> /compactThis summarizes the conversation, preserving important context while reducing tokens.
3. Be Specific
Section titled “3. Be Specific”Vague prompts require more back-and-forth:
Expensive:
> fix the bug> no, the other bug> in the auth module> the one about passwordsCheaper:
> fix the password validation bug in auth.py line 454. Use the Right Model
Section titled “4. Use the Right Model”| Model | Cost | Use For |
|---|---|---|
| Haiku | $ | Quick questions, simple tasks |
| Sonnet | $$ | Daily coding, most tasks |
| Opus | $$$$ | Complex architecture, hard problems |
Switch models based on task complexity:
> /model haiku> what's the syntax for a Python list comprehension?
> /model sonnet> refactor this authentication system5. Limit File Scanning
Section titled “5. Limit File Scanning”Instead of:
> explain all the code in this projectTry:
> explain the main entry point in app.py6. Use Print Mode for Quick Queries
Section titled “6. Use Print Mode for Quick Queries”# Interactive mode loads contextclaude> quick question
# Print mode is lighterclaude --print "quick question"Setting Budget Alerts
Section titled “Setting Budget Alerts”Track spending with a simple script:
#!/usr/bin/env python3"""Check Claude Code spending and alert if over budget."""import subprocessimport json
DAILY_BUDGET = 10.00 # dollars
def check_usage(): result = subprocess.run( ["npx", "ccusage", "--json"], capture_output=True, text=True ) data = json.loads(result.stdout) cost = data.get("estimated_cost", 0)
if cost > DAILY_BUDGET: print(f"⚠️ Over budget! ${cost:.2f} / ${DAILY_BUDGET:.2f}") else: print(f"✓ ${cost:.2f} / ${DAILY_BUDGET:.2f}")
if __name__ == "__main__": check_usage()Context Window Limits
Section titled “Context Window Limits”Claude has a maximum context window. When you hit it:
- Oldest messages get dropped
- You lose important context
- Claude may forget earlier instructions
Signs you’re hitting limits:
- Claude forgets what you discussed earlier
- Responses become less coherent
- You see warnings about context length
Solution: Use /compact proactively or start a new session.