Safeguard
AI Security•

How to cut token spend in a coding agent

A coding agent bills the whole conversation again on every step. Seven changes that reduce the bill, each tied to a published price or caching rule.

Safeguard Security Team
4 min read

A coding agent is a loop. It reads a file, runs a command, reads the output and edits, and every step re-sends the conversation so far. The bill grows with the number of steps and the size of the context, not just with how much code you ask for. These changes target those two multipliers. Prices are from vendor pages read on 18 October 2026.

1. Measure first

Log the usage block of every call. Anthropic returns input_tokens, cache_creation_input_tokens and cache_read_input_tokens. OpenAI returns cached_tokens under input_tokens_details. Without these you are guessing which of the changes below matters.

2. Keep the start of the prompt stable

Both Anthropic and OpenAI say cache hits need an identical prefix. Put instructions, tool definitions and project notes first, and anything that changes (timestamps, the user message) last. Anthropic lists tool definition changes, tool choice changes and adding or removing images as cache invalidators, so avoid reshuffling tools mid session. Anthropic also lists changes to output_config.effort and thinking settings as invalidating parts of the cache on some models.

3. Stay inside the cache lifetime

Anthropic's default is 5 minutes, refreshed at no extra cost each time the cache is used, with a 1 hour option at 2x the base input price. If you pause for a meeting, the next turn pays a write again. OpenAI's GPT-5.6 and later default is 30 minutes.

4. Return less from tools

A tool result stays in the context and is re-read on every later turn. A 3,000 token test log read over 30 more turns is 90,000 input tokens. Filter output to the failing lines, cap file reads to the relevant range, and prefer search results over whole files.

5. Use a small model for small steps

List prices differ by a large factor. Per million input tokens: Claude Haiku 5.5 $0.10, Sonnet 5.5 $2, Opus 5.5 $4, Fable 5.1 $10. OpenAI lists gpt-6-luna at $0.10 and gpt-6-sol at $2. Routing file search, formatting and summarising to the cheapest tier that does the job, and keeping the largest model for planning and hard edits, is a decision you control. Test quality on your own tasks before you route.

6. Start fresh for a new task

Dragging a 150,000 token context into an unrelated task means every call re-reads it. Ask the agent for a short summary of what matters, then start a new session with only that.

7. Batch anything nobody is waiting for

Bulk review, test generation across a repo overnight and migration reports can go through batch APIs at 50 percent of standard price. Interactive loops cannot.

A small check you can run

def session_cost(turns, prefix, new, out, inp, read, outp):
    # prefix cached after first turn; new tokens billed at input price
    return (prefix * inp + (turns - 1) * prefix * read
            + turns * new * inp + turns * out * outp) / 1e6

print(session_cost(40, 20000, 3000, 600, 2.0, 0.10, 10.0))  # about 0.6
print(session_cost(40, 20000, 1000, 600, 2.0, 0.10, 10.0))  # trimmed tool output, about 0.44

The first call approximates the cached Sonnet 5.5 session in our caching guide. The second shows what trimming new input per turn from 3,000 to 1,000 tokens does. It ignores the cache write premium, so treat both as approximate.

If you connect Safeguard to your agent over MCP, tool calls are unlimited, which means the cost control sits where it belongs: in the model settings above.

Quick check

Why does a long tool output cost more than its size suggests?

It stays in the context and is billed as input again on every later turn until the session ends.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.