7 techniques pratiques pour optimiser l'utilisation des tokens de Claude Code
Réduisez vos coûts Claude Code avec 7 techniques éprouvées d'optimisation de tokens. Apprenez /compact, le cache de prompts, les stratégies de sous-agents.
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.
Passez votre flux Claude Code au niveau supérieur
50 modèles de prompts éprouvés, prêts à être copiés-collés dans Claude Code.
PDF gratuit : aide-mémoire Claude Code en 5 minutes
Laissez simplement votre e-mail et nous vous enverrons immédiatement l'aide-mémoire A4 en PDF.
Nous traitons vos données avec soin et n'envoyons jamais de spam.
À propos de l'auteur
Masa
Ingénieur passionné par Claude Code. Il gère claudecode-lab.com, un média tech en 10 langues avec plus de 2 000 pages.
Articles similaires
Comment naviguer rapidement dans de grandes bases de code avec Claude Code
Même les projets de plus de 100K lignes se comprennent en quelques minutes.
5 techniques de débogage pour écraser les erreurs avec Claude Code
Collez le message d'erreur et Claude Code identifie la cause et la corrige.
Créer des commandes slash personnalisées dans Claude Code
Apprenez à créer des commandes slash personnalisées dans Claude Code. Couvre l'emplacement des fichiers, les arguments et l'automatisation des tâches.