Tips & Tricks

7 técnicas práticas para otimizar o uso de tokens do Claude Code

Reduza os custos do Claude Code com 7 técnicas comprovadas de otimização de tokens. Aprenda /compact, cache de prompts, estratégias de subagentes.

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 /compact and /clear
  • Shorten prompts with CLAUDE.md
  • Pipe only the relevant data
  • Parallelize and save context with subagents
  • Match the model to the task
  • Check /cost frequently

Making these habits cuts monthly costs in half or better. See the Anthropic official docs for further best practices.

#claude-code #otimização-tokens #redução-custos #eficiência

Leve seu fluxo no Claude Code a outro nível

50 modelos de prompt testados em campo, prontos para colar direto no Claude Code.

Grátis

PDF gratuito: Cheatsheet do Claude Code em 5 minutos

Basta informar seu e-mail e enviamos na hora o cheatsheet em uma página A4.

Cuidamos dos seus dados pessoais e nunca enviamos spam.

Masa

Sobre o autor

Masa

Engenheiro apaixonado por Claude Code. Mantém o claudecode-lab.com, uma mídia tech em 10 idiomas com mais de 2.000 páginas.