Token optimization for AI agents: memory, body and brain
An AI agent is a model plus everything wrapped around it: its memory, its instructions, its tools and the conversation up to now. All of that goes to the model again on every turn, so an agent spends most of its tokens re-reading the same text. Token optimization means cutting that repeated text in three layers: the memory, the agent around the model (the body) and the model call itself (the brain). The model stays the same; it just gets less to read per turn.
Updated 2026-11-27
Why agents use so many tokens#
A chat answers once. An agent works in a loop: read, think, call a tool, read the result, think again. Each step is a new request, and each request carries everything before it. Twenty steps into a task, the model has read its own instructions twenty times.
So most of an agent’s tokens are not new work. Across our three measured Claude Code sessions, 4,133,084 of 4,314,135 tokens were cache reads: text the model had already seen, sent again.
Three layers to optimize#
| Layer | What it is | How to cut it |
|---|---|---|
| Memory | Notes, rules and project facts the agent loads, such as CLAUDE.md or AGENTS.md. | Keep a short core that loads every turn; move the rest to files that load only when a task needs them. |
| Body | The agent around the model: its instructions, tools, and how it reads files and command output. | Short instructions, few tools, read only the lines you need, trim long command output. |
| Brain | The model call: which model, which provider, how the prompt is cached. | Keep the prompt prefix stable so it caches, and pick the smallest model that does the job. |
Memory comes first because it loads on every turn of every session. A line cut from memory is saved again and again; a line cut from one answer is saved once.
Memory: the biggest repeated cost#
Memory files grow. Every fix and preference gets a line, and nothing gets taken out. Soon every turn starts with thousands of tokens of notes, most of them about other tasks.
- Split it: a short always-loaded core (who you are, the rules that apply to every task) and on-demand files for everything else.
- Index it: the core lists each on-demand file with a one-line hint of when to open it.
- Keep it split: check for drift as notes are added, so the core stays short.
MinniMemoryMCP does this for Claude Code and Codex. With MinniMemoryMCP 1.6.0 on our test memory, the agent went looking in its memory 17 times instead of 89 over the same 87 answers, and the run used 47.2% fewer tokens. Measured 15 November 2026: at API rates, a Claude Code session on our test memory cost $0.47 before MinniMemoryMCP 1.6.0 and $0.29 after on average, 37.7% less (each of the three sessions 33.9% to 40.0% less). A blind judge found 80 of 87 answers correct before and 81 after.
Body: the agent around the model#
- Instructions: an agent’s own system prompt can be thousands of words. Shorter is cheaper and leaves the model’s attention for your task.
- Tools: every tool’s description is sent every turn. Remove MCP servers you do not use.
- Reading: search and read line ranges instead of whole files.
- Output: long test runs and logs stay in the conversation once read. Filter them first.
MinniCode, our low-cost, open-model coding agent, is built this way: short instructions, memory on demand, any model on your own key. More in Low-token coding agents.
Brain: the model call#
Providers cache a prompt prefix that does not change, and a cached read costs much less than fresh input. Anything that changes near the top of the prompt, such as a timestamp or a reordered tool list, breaks the cache for everything after it. Keep stable text first and changing text last. Prompt caching for coding agents goes into it.
Then pick the model. A smaller or open model is cheaper per token, and it copes better when the agent hands it less to read.
Who we are#
Minni Labs (minniai.com) is an AI agent interface and memory-optimization R&D company. We build the layer between you and the model so the same model spends fewer tokens per turn. Our products are MinniMemoryMCP and MinniCode. More about us.