Last updated 13 October 2026
How to manage token costs in Claude Code
The direct relationship between context growth and cost
The cost of running Claude Code scales with the size of your conversation history. Every message you send includes the entire back and forth of the session as input context. This means your tenth prompt costs significantly more than your first prompt. If you allow the conversation to grow without management, you will eventually reach a point where every small question requires thousands of tokens to process. This growth happens because the agent needs to remember previous file edits, shell outputs, and search results to maintain consistency across the project.
The primary driver of high bills is the input token count. While output tokens for the answers have a cost, the repetitive sending of a massive context window is what usually surprises developers. You must treat the context window as a finite resource that you need to prune regularly. If you ignore the size of your session, you are effectively paying a tax on every new thought you share with the agent. Monitoring this growth is the first step toward a sustainable workflow. You can use the /cost and /usage commands at any time to see exactly how much you have spent in the current session.
Truncate large outputs with Bash and Grep
Claude Code uses the Bash tool to interact with your system. If you run a command that generates thousands of lines of logs or a massive JSON response, all that text enters your context window. This is a common way to accidentally burn through your budget. To prevent this, always pipe your commands into Grep or use head and tail to limit what the agent sees. If you only need to know if a specific error exists in a log file, do not let the agent read the whole file. Filter for the error string first.
The agent also uses the Read and Glob tools to explore your codebase. If you ask it to find a function, it might try to Read several large files. You can guide it to be more efficient by providing specific paths. If you know a bug is in the source directory, tell the agent to limit its search to that folder. Using the Glob tool with a specific pattern is much cheaper than letting the agent browse the entire repository root. Narrowing the scope of tool use is one of the most effective ways to keep your token count low.
Use /compact to summarize and save space
The /compact command is your primary tool for managing long sessions. When you run /compact, Claude Code summarizes the conversation so far and removes the detailed history of every individual message. This process significantly reduces the number of tokens sent with each new prompt. The agent still remembers the important decisions and the state of the project, but it no longer has to process every line of code it previously read or every minor shell output it generated.
You should run /compact whenever you feel the session is becoming sluggish or after you have finished a major sub-task. You can also automate this by using the PreCompact hook event to trigger specific actions or reminders. While the summary is very good, it does lose some granular detail. If you need to refer back to a very specific line of a previous discussion, do that before you run the command. For most development workflows, the cost savings of a compacted session far outweigh the loss of detailed history.
Start fresh with /clear or new sessions
Sometimes a session becomes too cluttered to save even with a summary. If you have finished fixing a bug and want to start a new feature, do not continue the same conversation. Use the /clear command to wipe the current session context while staying in the same directory. This resets your input tokens to the minimum required for the project structure. It is the most drastic and effective way to stop a price spike when you are shifting focus to a different part of the codebase.
If you need to switch between different tasks frequently, use the terminal to manage multiple sessions. You can quit a session with Ctrl+C and then use claude --resume later to pick from a list of previous conversations. This allows you to keep context-heavy discussions separate. If you use adhdTerminal, you can see these different sessions as tiles on a wall. This makes it easier to keep track of which session is which and prevents you from accidentally running an expensive, bloated session for a task that only requires a fresh start.
Optimize project instructions in CLAUDE.md
The CLAUDE.md file sits in your repository root and provides persistent instructions to the agent. You can use this file to list important build commands and code style rules, which prevents the agent from wasting tokens trying to figure them out through trial and error. If the agent knows exactly how to run your tests because you documented it in CLAUDE.md, it will not need to use the Bash tool to explore your package.json or Makefile every time.
You can also use CLAUDE.md to define which directories the agent should ignore or prioritize. If your project has a large folder of assets or compiled binaries that are not in .gitignore but are also not relevant to coding, mention this in the instructions. This prevents the agent from using Glob or Read on files that do not matter for the task. Well-written project instructions act as a map, guiding the agent through the most efficient path and saving you money on every tool call.
Manage permission modes and the plan stage
Claude Code has different permission modes that change how it uses tools. You can cycle through these using Shift+Tab inside a session. The plan mode is particularly useful for cost control. In this mode, the agent creates a plan for what it wants to do but asks for your permission before executing any tool that might be expensive. This gives you a chance to see if the agent is about to read a massive directory or perform a complex search that you know is unnecessary.
By reviewing the plan first, you can intervene and suggest a more direct approach. For example, if the agent plans to use WebSearch to look up a common library function, you can tell it to use the Read tool on your local documentation instead. This prevents unnecessary API calls and keeps the context focused. Once you are confident in the approach, you can switch back to the default or acceptEdits mode to speed up the execution. This manual oversight ensures that tokens are only spent on actions that actually move the project forward.
Visual session management with adhdTerminal
When you are working on a complex project, you might have five or six different Claude Code sessions running for different components. Managing these in separate terminal tabs makes it easy to lose track of which one has a large context window and which one is idle. adhdTerminal solves this by displaying your sessions as a wall of tiles on your macOS or Windows desktop. Each tile shows the most recent exchanges of the conversation, allowing you to see at a glance if a session has grown too large.
The visual interface helps you identify sessions that should be cleared or compacted. Because each tile shows whether a session is working or waiting for you, you can quickly jump into the most expensive sessions to finish the task and close them. This level of visibility is helpful for developers who struggle with the out-of-sight nature of standard terminal tabs. By keeping your active sessions organized and visible, you are less likely to leave a high-cost conversation running longer than it needs to be.
Use claude -p for single tasks
Not every interaction requires a full interactive session. If you just need a quick explanation of a command or a one-time file edit, use the claude -p flag followed by your prompt. This runs a single prompt and prints the answer without initializing the full interactive interface and its associated overhead. This is the most token-efficient way to use the agent for small, isolated questions because it does not maintain a long-running context window.
This mode is perfect for integration into your shell scripts or for quick checks of your code environment. Since it does not start a persistent session, you do not have to worry about accidentally accumulating a large history. If you find yourself asking a question that does not lead to a follow-up, get into the habit of using this flag. It keeps your interactive sessions reserved for complex tasks that actually benefit from a deep conversation history, while keeping your daily costs lower.
Configure tool permissions in settings
You have granular control over which tools Claude Code can use in the .claude/settings.json file. If you find that the agent is frequently using WebSearch or WebFetch and driving up costs with external data, you can set these tools to require explicit permission. By modifying the permissions section of your settings, you force the agent to stop and ask you before it accesses the web or performs wide-reaching file operations.
This configuration can be done at the project level or globally in your home directory. If you are working on a project where you know certain tools are never needed, disabling them entirely is a safe way to prevent accidental spend. You can also use .claude/settings.local.json for your own overrides that are not tracked in the repository. Taking the time to tune these settings ensures that the agent operates within the boundaries you define, preventing unexpected spikes in your API usage during a long coding session.
Questions people ask
How can I check how much a specific task cost me?
You can type /cost inside your session at any time. This will display the total cost of the current session and break down the tokens used. To see your total spend over a longer period, use the /usage command.
Will Claude Code read my entire project and charge me for it?
No, it only reads files that it deems relevant to your prompt using tools like Read and Glob. It also respects your .gitignore file. You can further limit its reach by providing specific instructions in your CLAUDE.md file.
What is the fastest way to reduce a high token count in a long session?
The fastest way is to use the /compact command, which summarizes the history. If you no longer need the history at all, the /clear command is even more effective as it resets the context window entirely.
Read next
- Add build instructions to your CLAUDE.md fileImprove project context by adding specific build commands and environment requirements to your CLAUDE.md file for the AI to follow.
- Add common error fixes to CLAUDE.mdImprove project memory by adding a section for common errors and their fixes to your CLAUDE.md configuration file.
- Add custom shell aliases for Claude CodeSpeed up your workflow by creating shell aliases for common Claude Code invocations and project-specific startup commands.
- Add test commands to a CLAUDE.md fileUnderstand how to define specific test runners and environment flags in your CLAUDE.md file so the tool runs the correct test suite.