How Claude Code Counts Tokens: A Complete Guide
Problem
When I ran /init in Claude Code, I saw this:
Token usage: 5,000 tokensContext window: 96% usedI had just started. Why was 96% of my context already used?
I checked the Reddit thread and found:
“When I was installing Claude code, it appeared that when I run /init it used like 5k tokens, and a message appear saying that I already used 96% of my usage”
“I just used the chat for 5 prompts, 3 of them were to install Claude code”
I needed to understand where all these tokens were going.
Environment
- Claude Code CLI (latest)
- Claude Pro subscription
- macOS Sequoia
- Python project (~50 files)
What happened?
I launched Claude Code and ran the initialization:
$ claude> /initThe output surprised me. Before I even asked a question, my context was nearly full.
I ran /context to see the breakdown:
System prompts: 35%System tools: 28%MCP tools: 12%Memory files: 5%Conversation: 4%My actual message was only 4% of the context. The rest was overhead.
How to solve it?
Understand What Consumes Tokens
When you send a message in Claude Code, your token usage includes:
1. System Prompt: ~5,000-15,000 tokens2. Tool Definitions: ~2,000-10,000 tokens3. MCP Server Tools: varies by configuration4. CLAUDE.md: ~500-5,000 tokens5. Conversation History: grows with use6. Your Actual Message: typically smallThe context window percentage is calculated as:
input_tokens + cache_creation_input_tokens + cache_read_input_tokensThis does NOT include output tokens (tracked separately).
Why /init Uses So Many Tokens
The /init command:
- Scans your project structure
- Reads key files to understand the codebase
- Generates a CLAUDE.md file with project context
- All of this adds to the context immediately
Leverage Prompt Caching
Claude Code uses prompt caching to reduce costs:
Base input tokens: 1x ($3/MTok for Sonnet)Cache write: 1.25x ($3.75/MTok)Cache read: 0.1x ($0.30/MTok) <-- 90% cheaper!After the initial load, cached content costs 90% less on subsequent requests.
Monitor Context with Built-in Commands
# View current token usage/cost
# See detailed context breakdown/context
# Clear context when switching tasks/clear
# Compact with specific focus/compact Focus on code samples and API usageConfigure Status Line for Real-Time Monitoring
Add to your settings:
{ "statusLine": { "command": "echo '{\"context_window\":{\"used_percentage\":'$PCT'}}'" }}Reduce MCP Server Overhead
/mcpEach MCP server adds persistent tool definitions. Disable unused servers to reduce baseline token overhead.
Keep CLAUDE.md Concise
Good: 100-300 lines, focused instructionsBad: 1000+ lines, everything including the kitchen sinkMove specialized instructions to separate skill files.
The reason
I think the key reason for high token consumption is that Claude Code is an autonomous agent, not a simple chat interface.
The extensive context (system prompts, tool definitions, MCP servers) enables Claude Code to operate autonomously on your codebase. But it comes with token costs.
A Reddit user speculated:
“I am wondering if this is caused due to some prefix caching changes they made - or a recursive system prompt that takes up a lot of tokens”
The “recursive system prompt” idea is close. Each tool and capability adds definitions that consume tokens.
Common Mistakes
| Mistake | Impact | Solution |
|---|---|---|
Not using /clear between tasks | Stale context wastes tokens | Clear when switching focus |
| Running many MCP servers | Tool definitions bloat context | Disable unused servers |
| Large CLAUDE.md files | Baseline token overhead | Keep under 500 lines |
| Vague prompts | Broad file scanning | Be specific about files/functions |
Summary
In this post, I showed how Claude Code counts tokens. The key point is that system prompts, tool definitions, and MCP servers all consume context before you even type a message.
Key takeaways:
- Prompt caching reduces costs by up to 90% for repeated content
- Use
/costand/contextto monitor where tokens are going - Clear context between unrelated tasks
- Keep CLAUDE.md concise and focused
- Disable unused MCP servers
For developers transitioning from web chat to Claude Code, the initial token consumption can be surprising. But understanding the architecture helps you work efficiently.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 Claude Code Costs Documentation
- 👨💻 Anthropic Prompt Caching
- 👨💻 Claude Code Context Window Usage
- 👨💻 Reddit Discussion: Claude limits are broken
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments