Ninja Builder Sign in Get started

How to reduce token usage when coding with AI

Where AI coding tokens really go, and seven habits that cut them without slowing you down.

By Ninja Builder team6 min read
Title card reading How to cut token usage in AI coding, beside a Ninja Builder chat with background work and token counts

Key takeaways

  • Every turn resends the whole conversation, so long chats cost more per message.
  • Use /clear between unrelated tasks and /compact when you need to keep history.
  • Specific prompts and lean CLAUDE.md files keep the context small from the start.
  • Use a mid-size model for routine work and give helper agents a cheaper one.
  • Check /usage or your provider's dashboard before optimizing.

To use fewer tokens with an AI coding agent, keep the conversation short and focused: start a fresh chat for each unrelated task, compact long chats, write specific prompts, keep rules files lean, pick a cheaper model for routine work, and run fewer background agents. Every message resends the whole conversation, so most of the savings come from controlling what the model has to reread.

This guide explains where the tokens actually go, then walks through the habits that cut them, with the exact Claude Code commands and the equivalents for other tools.

Where the tokens go

A coding agent does not remember your chat between turns. Each time it answers, the tool sends the model a full package: the system prompt and tool list, your rules files, every earlier message and reply, the output of every file it read and command it ran, and finally your new message. Anthropic's own cost guide for Claude Code says it plainly: the full conversation goes along with every request, and each tool call sends another request carrying that batch of results.

Diagram of one request to the model: system prompt, rules files, the conversation so far, tool results, and a small slice for your new message
Your new message is the smallest part of each request. The history around it is what grows.

Prompt caching softens this. Repeated content is read from a cache at a lower rate, but it is still counted, and the cache expires after a while. Your first message after a long break can reprocess the whole context at full price. So a one-line question in a chat that has been open all day can cost far more than the same question in a new chat.

1. Start fresh between unrelated tasks

This is the biggest single habit. When you move from fixing a login bug to writing a pricing page, the login history is dead weight on every later turn.

  • Claude Code: run /clear. It starts a clean context and, per Anthropic's docs, costs nothing. Use /rename first if you want to find the old session later with /resume.
  • Other tools: open a new chat or thread. In Codex, Copilot CLI or Cursor, a new conversation has the same effect.

Before you clear, ask the agent for a three-line handover note (what was done, what is left, which files matter) and paste it into the new chat if you need continuity.

2. Compact long chats instead of dragging them along

When you do need the history, summarize it. In Claude Code, /compact replaces older messages with a summary, and you can steer what survives:

/compact Keep the API decisions and the failing test names, drop the debugging logs

Two things to know. Compacting reads the conversation it summarizes, so it is a large request in itself; do it at natural break points, not every few minutes. And auto-compact will kick in on its own when the context nears its limit, which is later than ideal if cost is what you care about.

The /rewind menu also has Summarize from here and Summarize up to here, which compress part of a session and keep the rest word for word. That is handy after a long detour of trial and error.

3. Write prompts that limit the search

Vague requests make agents explore. "Improve the codebase" sends the model reading file after file, and every file it reads lands in the context for the rest of the chat. Compare:

  • Expensive: "Something is wrong with sign-up, have a look."
  • Cheaper: "In src/auth/signup.ts, the email check accepts a@b. Make it require a dot in the domain and add a test in signup.test.ts."

Name the files, the expected result and how to check it. Planning first helps on bigger jobs too: in Claude Code, Shift+Tab cycles to plan mode, so you approve the approach before the agent spends tokens building the wrong thing. If it heads off in the wrong direction, press Esc early rather than letting it finish.

4. Trim what rides along on every turn

Some content is loaded at the start of every session and sent with every request, whether you need it or not.

  • Rules files. CLAUDE.md and AGENTS.md are read every time. Anthropic suggests keeping CLAUDE.md under about 200 lines and moving workflow-specific instructions into skills, which load only when used. Our guide to writing CLAUDE.md and AGENTS.md covers what belongs there.
  • MCP servers. Each connected server adds tool names and instructions. Run /mcp and turn off the ones you are not using, and prefer a command-line tool such as gh when one exists.
  • Noisy output. A 5,000-line test log read into the chat stays there. Ask for the last 50 lines, or a summary of failures only.

To see what is taking up room right now, run /context in Claude Code. /usage shows your plan usage and, on subscription plans, which skills, subagents and MCP servers account for it.

5. Match the model to the job

Bigger models cost more per token and often think longer. Anthropic's guidance is to use Sonnet for most coding and keep Opus for hard architectural or multi-step reasoning work; switch with /model. Thinking tokens are billed as output, so for simple edits you can lower the effort with /effort.

The same logic applies elsewhere. On GitHub Copilot, CLI usage is now billed in AI credits that depend on the tokens processed and the model used, so the model picker matters directly. On a ChatGPT plan, Codex usage counts against your plan's limits, and lighter models stretch them further.

6. Run fewer, leaner background agents

Subagents and agent teams are useful, but each one starts with its own context and sends its own requests. Anthropic's docs note that subagent requests count toward the same usage limits as your main chat, and that agent teams use roughly seven times more tokens than a standard session when teammates run in plan mode.

HabitWhy it saves tokens
Give helpers a cheaper modelSearch and test-running rarely need the biggest model
Keep teams smallToken use grows roughly with the number of active agents
Write short, focused briefsEverything in the brief is in the helper's context from the start
Check once, after all editsRe-running the full test suite after every file repeats the same output
Stop agents when they finishAn idle teammate can keep consuming tokens until it exits

Our explainer on AI agent teams and subagents goes deeper into when a team is worth its cost.

7. Measure before you optimize

Guessing where tokens go is unreliable. Check the numbers first: /usage in Claude Code, the usage dashboard on your ChatGPT or GitHub account, or your provider's billing page for API keys. Look for one chat or one workflow that dwarfs the rest. It is common for a single long-running chat, or one chat that started many background agents, to account for most of a week.

Ninja Builder Activity panel, Usage tab, showing the last 24 hours split into chats and background agents, with each chat's share listed
Usage by chat in Ninja Builder: background agents count under the chat that started them.

Doing this in Ninja Builder

Ninja Builder is a desktop app that runs Claude Code and other agents for you, and several of these habits are built in. The Context chip in the message box fills up as the chat grows, /compact works as usual, and you can set the auto-compact limit lower in Settings, General, Speed and cost, so chats compact sooner and each reply costs less (see Context and auto-compact). A switch there makes background agents and workflow agents run on Sonnet while your own chat keeps its model.

The Activity panel's Usage tab shows tokens per chat over 24 hours, 7, 30 or 90 days, with workflows and helper agents counted under the chat that started them, and the bell warns you once when a single workflow passes 10M tokens and again at 25M. The Accounts and usage docs cover the details. Watch the two-minute walkthrough below, or download Ninja Builder and try it on your own project; it is free for 14 days, then $4.99 a month.

Track Claude Usage & Limit Resets Per Chat: Ninja Builder Statistics

If you are already at your limit, read what to do when you hit your Claude usage limit next.

Frequently asked questions

Does /compact save tokens in Claude Code?

Yes for later turns, because the summary is shorter than the history it replaces. The compact itself reads the whole conversation, so it is a large request; do it at natural break points.

What is the difference between /clear and /compact?

/clear starts a fresh context with no history and costs nothing. /compact keeps a summary of the conversation so the agent still knows what happened.

Why does my usage climb even when I send short messages?

Each message carries the full conversation and all earlier tool output. After a break the cache may have expired, so the whole context is processed again.

Do subagents use my plan's limits?

Yes. Anthropic's docs say subagent requests count toward the same usage limits as your main conversation, so give them focused tasks and a cheaper model where you can.

Keep reading