惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
博客园 - 【当耐特】
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
5 Tips to Cut Claude Code Token Usage by 30%
Alaric · 2026-05-18 · via DEV Community

I've been using Claude Code daily for the past few months. The output quality is great, but the API bill at the end of the month was painful. After some experimentation, I found a few habits that consistently cut my token consumption by 25–35% without sacrificing code quality.

Sharing them here in case anyone else is in the same boat.


1. Put a CLAUDE.md at the project root

Claude Code reads CLAUDE.md automatically on startup and treats it as durable context. Without it, Claude has to re-discover your project structure on every session — that's a lot of file-reading tokens.

A minimal template that works for me:

# Project: <name>

## Stack
- Language: Go 1.22 / TypeScript 5
- Framework: Gin / React 19
- DB: PostgreSQL via GORM

## Layout
- `controller/` — HTTP handlers
- `service/`    — business logic
- `model/`      — DB models

## Conventions
- Use `common.Marshal` instead of `encoding/json`
- All new code must compile under `go vet`

Enter fullscreen mode Exit fullscreen mode

Keep it under 200 lines. Anything longer and Claude will start spending tokens summarizing the file itself.
cost, then reads are ~10% of normal price.

What this means in practice:

  • Don't change CLAUDE.md mid-session — it invalidates the cache
  • When asking follow-up questions, append rather than rewrite the prompt
  • For long files, paste once and refer back to "the file above" instead of re-pasting

For a 200K-token project context, my cache hit rate is around 70%, which roughly cuts input cost from $0.60 to $0.18 per session.


4. Prefer Read tool over pasting code into the prompt

Two ways to give Claude a file:

A) "Here's the content: <paste 5000 lines>"
B) "Read src/foo.go"

Enter fullscreen mode Exit fullscreen mode

Both work, but (B) is cheaper because Claude only reads the file when it actually needs to. Often it'll read a 50-line slice instead of the whole file. With (A), you've already paid for all 5000 lines whether they were needed or not.


5. Use a smaller model for routine tasks

You don't need Opus for "write a unit test for this function." Switch to Sonnet (or Haiku for trivial edits) when the task is mechanical:

  • Boilerplate generation
  • Adding logging
  • Renaming variables across a file
  • Simple test cases

Claude Code lets you swap models per session. For me, Sonnet handles ~70% of daily edits, and I save Opus for hard reasoning (architecture decisions, bug investigation, complex refactors).

Rough cost difference per million output tokens:

  • Opus 4.7: $75
  • Sonnet 4.5: $15
  • Haiku 4: $5

A 5x–15x saving on the 70% of routine work adds up fast.


What didn't work for me

  • "Compress" the prompt manually — too much effort, marginal savings, and Claude often misses context you compressed away
  • Using ultra-cheap third-party "Opus" relays — twice I found out the model was actually a Chinese open-source model in disguise. Quality dropped immediately. If price looks 5x too good to be true, it usually is
  • Disabling Prompt Caching to "stay on the latest context" — caching is invalidated automatically when context changes, so you don't gain anything by disabling it

TL;DR

Habit Saving
CLAUDE.md at project root 20–30% on first messages
One task per session + /clear 10–20% on long sessions
Use Prompt Caching 30–60% on follow-ups
Use Read tool, don't paste 10–30% on file-heavy tasks
Sonnet/Haiku for routine work 5–15× on those tasks

Combined effect on my monthly bill: roughly a third of what I was paying before.

If you have other tips that worked for you, I'd love to hear them in the comments.

2. Scope each session to a single task

If you start a session with "let's refactor the auth layer and also add OAuth and also fix the rate limiter," Claude keeps all three goals in context the whole time. Every subsequent message pays for that context.

Habit: one task per session, then /clear. You'll see token usage drop noticeably on the second message of each session.


3. Use Prompt Caching aggressively

Both Anthropic's API and most relays support prompt caching. The way it works: stable prefixes (system prompt, file contents, project context) are cached for 5 minutes at a small one-time write