← Blog

Save tokens with coding agents: the tools and habits that actually work

Updated · 7 min read

Most people who run out of quota aren't asking too many questions. Their agent is carrying too much on every step. A coding agent resends the whole conversation each time it acts: your instructions file, every tool definition, every file it has read and every command output it has seen. A session that read three big files and ran the test suite twice can be sending 80K tokens per step before you type anything new.

The good news is that most of that weight is optional. This post covers how to see where your tokens go, the built-in commands every agent has, six popular open-source tools that cut specific kinds of waste, and a starter setup you can copy.

For how llmapi meters usage (credits, five-hour windows, cached input), see the pricing section.

Where the tokens go

Every request an agent sends is roughly:

Text
instructions + tool definitions + conversation so far + new file reads and command output → reply (+ thinking)
  • Fixed overhead. System prompt, tool schemas, MCP servers and your CLAUDE.md / AGENTS.md. Easily 20–35K tokens before your first message, and sent on every step.
  • File reads. An agent that opens a 2,000-line file to change one function pays for all 2,000 lines, then keeps paying for them on every later step.
  • Command output. Test runs, build logs, git log, npm install. Often the biggest single source of waste, because almost all of it is noise.
  • History. Old turns that no longer matter, kept because the session is still open.
  • Output and thinking. What the model writes back, including its reasoning.

Each tool below attacks one of these.

Step 1: measure

Don't optimise blind. Two minutes of measuring tells you which of the five is your problem.

  • Your llmapi dashboard. Dashboard → Usage shows credits per model and per day, so you can see which sessions were expensive.
  • Inside the agent:
AgentSee what's in contextShrink itStart fresh
Claude Code/context/compact/clear
Codex/status/compact/new
Gemini CLI/stats/compress/clear
OpenCodethe session header/compact/new

Claude Code's /context is the most useful of these: it breaks the window down into system prompt, tools, MCP servers, memory files and messages, so a 15K-token MCP server you forgot about shows up immediately.

  • ccusage reads the local logs that Claude Code, Codex and OpenCode write and turns them into daily, weekly, per-session and five-hour-window reports. It works with llmapi because it reads your machine's logs, not the API. Ignore its dollar column (it uses official list prices); the token counts are what matter.
Terminal
npx ccusage@latest            # usage by day
npx ccusage@latest session    # which sessions were heavy
npx ccusage@latest blocks     # five-hour windows (Claude Code)

Step 2: the habits that cost nothing

These save more than any tool, and every agent supports them.

  • One task, one session. Finished the bug? /clear (or /new) before starting the feature. A fresh session costs a fraction of a long one per step, even with caching.
  • Compact with a focus. When a single task runs long, compact instead of clearing, and tell it what to keep: /compact keep the file paths, the failing test name and the plan.
  • Rewind instead of compacting around a mistake. If one step dumped something huge into the conversation, Claude Code's /rewind (press Esc twice) takes it back out. That's cleaner than summarising around it.
  • Point, don't paste. "Fix parseDate in src/lib/time.ts" lets the agent read one function. "Here's the file:" followed by 2,000 lines makes you pay for all of them, on every step.
  • Keep instruction files short. CLAUDE.md, AGENTS.md and GEMINI.md ride along on every request. Aim for under 200 lines of things the agent truly needs. Move long references into separate files the agent can open when needed.
  • Turn off MCP servers you aren't using. Each one adds its tool definitions to every request. In Claude Code, /mcp shows them and lets you disable them per session.
  • Delegate exploration. In Claude Code, "use a subagent to find where X is handled" runs the search in a separate context and returns only the answer. All the files it opened stay out of your main conversation.

Step 3: tools that cut specific waste

All six are free and open source, and all work with llmapi because they run on your machine, not between you and the API. Install only the ones that match what your measuring showed. Each one adds a little overhead of its own.

RTK: trims command output

Fixes: test logs, build output, git and ls dumps.

RTK (Rust Token Killer, about 85K GitHub stars) is a small binary that hooks into your agent's shell. When the agent runs git status, cargo test or npm run build, RTK rewrites the command so the agent gets a compact version of the output: failures instead of every passing test, a summary instead of 500 log lines. The project claims 60–90% less command output. Real savings depend heavily on your workflow: large for test-heavy work, close to nothing for small scripts.

Terminal
brew install rtk                 # or: curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/refs/heads/master/install.sh | sh
rtk init -g                      # Claude Code (use --codex, --gemini, or --agent cursor / cline for others)
rtk gain                         # see how much it saved

Check that rtk gain works after installing: an unrelated package called rtk exists on crates.io. If the agent ever needs the full output to debug something, it can run the command directly.

Serena: reads symbols, not whole files

Fixes: agents opening big files to change one function.

Serena is an MCP server that gives the agent IDE-style tools backed by language servers: find a symbol, read just that function, list references, edit in place. Instead of reading three 1,500-line files to rename a method, the agent asks for exactly the code it needs. It pays off most on large codebases. On a small project, its own tool definitions can cost more than they save.

Terminal
uv tool install -p 3.13 serena-agent
serena init

Then add it to your agent by following Serena's client guide. The project warns against installing it from MCP marketplaces, which carry outdated commands.

Context7: fetches only the docs you need

Fixes: pasted documentation, and rounds of fixing an API the model remembered wrong.

Context7 gives the agent current, version-specific documentation for thousands of libraries, as small focused snippets rather than whole web pages. The bigger saving is indirect: fewer loops of "that function was renamed in v5, try again".

Terminal
npx ctx7 setup        # pick CLI + skill, or MCP

It works without an account, but a free API key from context7.com raises the rate limits. In a prompt, "use context7" or a library ID like /supabase/supabase points it at the right docs.

Repomix: packs a repo for one-shot questions

Fixes: many small file reads when you want an overview.

Repomix packs a repository, or part of one, into a single file. With --compress it keeps only signatures and structure (function and class definitions, no bodies). That's ideal for "explain this codebase" or "review the architecture" questions, sent in one request rather than dozens of tool calls.

Terminal
npx repomix@latest --compress --include "src/**"

It's not for everyday edits. A packed repo dropped into an agent session is a lot of tokens to carry. Use it for one question, then start a fresh session.

Caveman: shorter replies

Fixes: long, chatty explanations.

Caveman is a hugely popular skill that makes the agent answer in terse fragments: "New ref each render. Wrap in useMemo." It's fun, and it does shorten prose. Be realistic about the numbers, though. Replies are usually a small share of a coding session's tokens, the project's own honest benchmark shows a small gain over simply saying "be concise", and it doesn't touch thinking tokens or code. Use it if you read a lot of explanations, and skip it for design discussions.

Install only the skill:

Terminal
npx skills add JuliusBrussee/caveman -g

Then type /caveman in a session. Avoid its one-command installer (npx @caveman-ai/cli) with llmapi: it also sets up a local proxy for your API traffic, which can get in the way of your llmapi configuration.

Ignore files: keep junk out of reach

Fixes: agents wandering into node_modules, dist, lockfiles and data dumps.

Most agents skip whatever your .gitignore covers, and several have their own ignore file on top:

AgentFile
Cursor.cursorignore
Cline.clineignore
Gemini CLI.geminiignore
Claude Codepermissions.deny in .claude/settings.json

For Claude Code:

JSON
{
  "permissions": {
    "deny": ["Read(./dist/**)", "Read(./**/*.min.js)", "Read(./data/**)", "Read(./package-lock.json)"]
  }
}

Step 4: let caching and model choice do the rest

Keep the start of each request stable. On llmapi, input that matches the start of a recent request counts at 10%. Agents already structure requests this way. You keep the discount by not editing CLAUDE.md mid-session, not toggling MCP servers on and off mid-task, and not using plugins that inject changing text (timestamps, random IDs) into the system prompt.

Use the lightest model that does the job. The Standard-credit models (x1), such as deepseek-v4.1-flash, cost half as many credits per token as the Large-model credits models (x2, such as kimi-k2.6), so they suit mechanical work: renames, formatting, boilerplate, quick lookups. In Claude Code you can send all subagents to the lighter model while your main session stays on the larger one:

JSON
{
  "env": { "CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-v4.1-flash" }
}

Add that to the env block in ~/.claude/settings.json, next to the settings the llmapi installer wrote.

A starter setup

If you only do four things:

  1. Run npx ccusage@latest session once to find your expensive sessions, and /context once to see your fixed overhead.
  2. Make /clear between tasks a habit, and keep CLAUDE.md / AGENTS.md under 200 lines.
  3. Install RTK if you run tests or builds through the agent.
  4. Add Serena if your repo is large, and Context7 if you work with fast-moving libraries.

Then measure again a week later. If a tool didn't help with your workload, remove it, since its definitions cost tokens too.

Get set up

New to llmapi? Sign up and run the one-line installer from the setup guides. It configures Claude Code, Codex, Gemini CLI, OpenCode, Cline and more with your key, and the tips above work the same on top of it.

Keep reading