How do I cut the tokens my coding agent burns per turn?

A turn's input tokens are the context the agent carries: the files it read, tool output and tool definitions. Reduce what enters that context. Give the agent a code search tool that returns the matching chunks instead of whole files, keep the result count low, and disable the MCP servers you do not use.

Checked Markdown version

What a turn pays for

A client sends the conversation to the model again on each turn: the system prompt, the tool definitions, the earlier messages, every file the agent read and every tool result. A file that the agent opened to find one function is counted again on each later turn, until the client compacts the conversation.

Two actions follow from this:

  • Keep text that the agent does not need out of the context.
  • Find out what is in the context now. In Claude Code, /context shows what consumes the space (Manage costs).

Return chunks instead of files

When an agent opens a file to find one function, the whole file enters the context. A code index does the search outside the context window.

cocoindex-code is an open-source (Apache 2.0) code search tool with a ccc CLI and an MCP server. It parses files with Tree-sitter, tries to split them at function and class boundaries, and embeds the chunks. Chunks target about 1,000 characters, which is about 300 tokens.

The agent calls the MCP search tool with a description of what it needs. A default call returns up to 5 chunks, so the code in the result is at most about 1,500 tokens (5 chunks of about 300 tokens), plus file paths, line numbers and scores. A file read costs the full length of the file.

The ccc search CLI, which the skill runs, returns up to 10 results by default. Pass --limit 5 for the same size.

These parameters limit what a call returns:

MCP parameterCLI flagEffect
limit--limitNumber of results. MCP: 1 to 100, default 5. CLI: default 10.
offset--offsetSkips results, so the agent can ask for the next page only when it needs it.
paths--pathPath glob such as src/utils/*, to search one part of the repository.
languages--langLanguage filter such as python or typescript.

Install and connect

bash
pipx install 'cocoindex-code[full]'
# or with uv:
uv tool install --upgrade 'cocoindex-code[full]'

cd your-repo
ccc index
bash
# Claude Code: skill (recommended in the README)
npx skills add cocoindex-io/cocoindex-code

# Claude Code: MCP
claude mcp add cocoindex-code -- ccc mcp

# OpenAI Codex CLI
codex mcp add cocoindex-code -- ccc mcp

Cursor and other MCP clients that launch a stdio server take the generic entry. The client must start ccc mcp with the project as its working directory:

json
{
  "mcpServers": {
    "cocoindex-code": {
      "command": "ccc",
      "args": ["mcp"]
    }
  }
}

Other cuts that do not need an index

These come from Claude Code’s cost guide.

  • Unused MCP servers. Run /mcp to see the configured servers and turn off the ones you do not use.
  • CLI tools. The guide calls CLI tools more context-efficient than MCP servers. The ccc skill follows that route: the agent runs ccc search in the shell.
  • Symbol navigation. The guide notes that code intelligence plugins give precise symbol navigation instead of text-based search.

Limits, and when another tool fits better

  • Task type. Search saves tokens when the agent would otherwise read files to find code. When the agent has the exact identifier, a regular-expression search for it already returns few lines.
  • Savings figure. The saving depends on the repository and the task. Measure on yours: run the same task with and without the search tool, and compare the token usage that your client reports.
  • Indexing cost. With the local model the cost is compute time on your machine. With a cloud embedding provider you pay that provider per token (embedding guide).
  • Output tokens. Search reduces what the agent reads. It does not change how much the agent writes.
  • Callers and references. Questions about callers and references need a symbol tool such as Serena or CocoIndex Code Plus (ccx), a separate self-hosted product.

Sources and further reading