1. Understand Token Counting and Pricing
Before optimizing, understand how tokens work and impact your costs:
- Input tokens: Every character in your prompt and context counts
- Output tokens: Every character Claude generates costs tokens
- Pricing varies by model: Opus is more expensive than Sonnet, which is more expensive than Haiku
- Use
claude --count-tokensto estimate token usage before running tasks - Check your API dashboard regularly to track spending and identify patterns
2. Choose the Right Model for the Task
Not every task requires your most powerful model. Match model to complexity:
claude-haiku— Fast, cheap, perfect for simple tasks like formatting, linting, basic editsclaude-sonnet— Balanced speed and cost, ideal for most coding tasksclaude-opus— Most capable but most expensive, reserve for complex reasoning and architecture- Use
Alt+Pto quickly switch models without restarting - Set your default model in
ANTHROPIC_MODELenvironment variable
3. Optimize Context with Strategic Truncation
Large files and full codebase context drive token costs up. Be selective:
- Read only relevant files: Don't load entire repositories unless necessary
- Use grep/search: Find specific patterns instead of reading full files
- Summarize context: Ask Claude to summarize a large file into key points
- Archive old conversations: Long conversations consume massive token counts
- Use file references: Instead of pasting entire files, use
@filenamesyntax and let Claude fetch only what it needs
4. Leverage the /memory Command for Persistent Context
Store important context once, reuse it forever without re-sending tokens:
- Use
/remember [key] [value]to save important project context - Save architecture decisions, patterns, coding standards
- Save frequently used code snippets and templates
- View saved memory with
/memory - Update memory between sessions instead of re-explaining context each time
5. Use Agents to Parallelize and Reduce Iterations
Running agents in parallel can actually save tokens by reducing back-and-forth:
- Use
/generalagent to delegate complex tasks instead of doing everything yourself - Use
/exploreagent (Haiku-based) for fast codebase searches — saves tokens vs. manual exploration - Parallel agents finish work faster, reducing total iterations and token overhead
- Set
--max-agent-turns 3to limit agent exploration and prevent waste
6. Master Prompt Engineering for Efficiency
Precise prompts require fewer tokens and iterations:
- Be specific: "Add pagination to the users list" beats vague requests that require clarification
- Provide examples: Show input/output format instead of lengthy descriptions
- Set constraints: "Keep response under 100 lines" prevents verbose outputs
- Use structured formats: Ask for JSON or markdown instead of prose
- Front-load context: Include relevant context in the initial prompt instead of follow-up messages
7. Disable Extended Thinking When Not Needed
Extended thinking uses up to 10x more tokens for deep reasoning. Only enable when necessary:
- Set
MAX_THINKING_TOKENS=0to disable extended thinking by default - Enable only for complex architectural decisions or debugging
- Use
--effort=lowfor quick, simple tasks - Reserve high effort for truly complex problems
8. Reduce Output Verbosity
Longer outputs cost more tokens. Limit Claude's response length:
- Set
CLAUDE_CODE_MAX_OUTPUT_TOKENS=8000for compact responses - Ask Claude to "be concise" or "summarize key changes"
- Request code-only responses instead of explanations when you don't need them
- Use
--rawflag to get only the essential output
9. Batch Similar Tasks Together
Grouping tasks reduces per-task overhead and context setup:
- Format 5 files at once instead of one at a time
- Fix multiple related bugs in a single session instead of separate ones
- Process multiple files with the same transformation together
- Use
/batchfor massive parallel work across many agents
10. Use the --bare Flag for Scripted Workflows
The --bare flag skips loading unnecessary configs and MCPs:
- Eliminates overhead of loading local config files
- Skips LSP (Language Server Protocol) initialization
- Reduces startup tokens for quick one-off tasks
- Perfect for CI/CD pipelines and automated workflows
11. Cache Responses with Smart Session Management
Reuse prior session results instead of re-running expensive operations:
- Save session results with
/save-sessionto resume later - Use
claude --resume <session-id>to continue without re-processing - Archive sessions with their context to avoid rebuilding
- Keep your most-used project sessions active to reuse context
12. Monitor Spending and Set Alerts
Track costs regularly and set up monitoring:
- Check your API billing dashboard weekly
- Identify which models and tasks consume the most tokens
- Set up alerts if spending exceeds expected levels
- A/B test different approaches to measure token efficiency
13. Progressive Enhancement: Start Simple, Upgrade on Failure
The most effective cost optimization strategy:
- Start with Haiku: Try the cheapest model first
- Measure success: Did it work? Great, you're done
- Upgrade on failure: If Haiku fails, retry with Sonnet
- Reserve Opus: Only use Opus for tasks that require its reasoning capability
This approach saves 80%+ on token costs because most coding tasks complete with cheaper models.
Real-World Token Optimization Example
Scenario: Refactor a large codebase with 50 files.
- Naive approach: Paste all 50 files + full context + use Opus = 50,000+ tokens per task × 10 iterations = 500K tokens
- Optimized approach:
- Use grep to identify only files that need changes (5 files)
- Use Haiku for simple refactoring patterns
- Batch similar changes together
- Disable extended thinking
- Result: 2,000 tokens × 3 iterations = 6K tokens (98% savings)
Conclusion
Token optimization isn't about being cheap — it's about being smart. The strategies in this guide will save you thousands of dollars annually while actually improving your workflow speed and code quality.
Start with the three highest-impact strategies: (1) choose the right model, (2) optimize context, (3) use /memory for reusable context. These alone will cut your token usage in half.