Dev.to AI 🤖 Ai 👁 0 📖 6 min read

Your AI Agent Pays for the Same Prompt Five Times: The Tool-Loop Tax on Your Bill

Originally published at API Cost Lab. Your AI Agent Pays for the Same Prompt Five Times: The Tool-Loop Tax on Your Bill Last updated: November 2026 An AI agent task is not one API call. It is a loop: the mod

Originally published at API Cost Lab.

Your AI Agent Pays for the Same Prompt Five Times: The Tool-Loop Tax on Your Bill

Last updated: November 2026

An AI agent task is not one API call. It is a loop: the model answers, calls a tool, reads the result, answers again, calls another tool, and so on — and every round re-sends the entire conversation so far. The system prompt, the user question, the earlier answers, and every tool result come back as fresh input tokens, billed again at the full input rate. A support agent that needs five tool rounds costs six times a single call, not because the answers are expensive, but because the same 20,000-token context is paid for five times. That is the tool-loop tax, and it is the main reason agent bills surprise people who priced their app as if each task were one call.

One task, five bills

Here is how the loop actually bills. You send a system prompt and a user question. The model replies with a tool call instead of an answer. Your code runs the tool and sends the result back — but the API has no memory between calls, so that second request carries everything again: the system prompt, the user question, the model's first reply, and the new tool result. The third request carries all of that plus the second reply and the second tool result. Nothing is ever sent only once. Our Gemini usage logger shows this directly: its tool_use_prompt_token_count field climbs every turn, because tool results are fed back in as input tokens and billed at the input rate.

Two other charges ride along with the loop. Thinking tokens bill at the output rate on every round, so a reasoning model thinks — and charges — five times for one task. And the output of each round becomes input for the next, so a wordy intermediate answer is an expensive input on every round after it. Our agent cost explainer named the loop as the suspect; this article puts numbers on it.

The worked example: a five-round support agent

Take a support agent on Gemini 2.5 Flash, with a 20,000-token system prompt and help-centre document, and a 500-token user question. Each round the model writes 500 output tokens and each tool returns a 2,000-token result. Rates are the published standard rates, verified 16 November 2026 on Google's pricing page: input $0.30, output $2.50 per million tokens. The token counts are a worked example, not a measured run — the arithmetic is the point.

Round Input tokens sent Input cost Output cost
1 20,500 $0.00615 $0.00125
2 23,000 $0.00690 $0.00125
3 25,500 $0.00765 $0.00125
4 28,000 $0.00840 $0.00125
5 30,500 $0.00915 $0.00125

Each round adds the previous output (500 tokens) and the previous tool result (2,000 tokens) to the input, so the input grows by 2,500 tokens a round. Total input is 127,500 tokens at $0.30 per million: $0.03825. Total output is 2,500 tokens at $2.50 per million: $0.00625. The task costs $0.0445, against $0.0074 for the same question in a single call — almost exactly six times as much. And look at where the money went: the fixed 20,000-token context was sent five times, 100,000 tokens for $0.03 — 78 percent of the whole input bill. The tool results, the thing the loop is actually for, cost far less than re-reading the instructions.

At volume the gap stops looking small. Ten thousand of these tasks a day cost $445 a day, $13,350 a month, against $74 a day and $2,220 a month if each task were a single call. Same model, same question — the difference is entirely the loop. Run your own daily task count through our AI API Cost Calculator with the per-task figure from your logs, and the monthly gap is the number to stare at.

The runaway version: big tool results, ten rounds

The five-round example is the polite version. Now let each tool return a 10,000-token result — a full search dump, a whole database row set, a long document chunk — and let the loop run ten rounds, because the model keeps fetching more context to be sure. Round one sends 20,500 input tokens. Round two sends 31,000: the fixed context, the question, the first answer, and a 10,000-token result. Every round adds 10,500 tokens — 500 of model output plus 10,000 of tool result. After ten rounds the input total is 677,500 tokens, costing $0.20325, plus 5,000 output tokens at $0.0125. One task: $0.2158, or 29 times the $0.0074 single call.

The shape of that growth is what makes runaway loops dangerous. Input does not grow by one tool result per round; it grows by the sum of all previous results, because each one stays in the conversation. A loop with no round cap and no result-size cap has no cost ceiling at all, which is how a single user session can quietly burn a day's budget. Our per-user spending guard exists for exactly this failure: it stops one runaway session from eating the budget the other users need.

Three fixes, with the numbers

The first fix is to cache the fixed context. On the paid tier, cached input on Gemini 2.5 Flash costs $0.03 per million tokens instead of $0.30 — a tenth of the fresh rate. Apply that to the five-round example and the 100,000 tokens of re-read context cost $0.003 instead of $0.03. The fresh tokens — user question, prior answers, tool results — still bill at the full rate and come to $0.00825. Total input falls from $0.03825 to $0.01125, and the whole task from $0.0445 to $0.0175: a 61 percent saving, without changing a word the model sees. The catch is the same one our cache-write investigation found: caching has its own price — on Gemini it is storage rent of $1.00 per million tokens per hour for an explicit cache — and it is a paid-tier feature. Also note that Gemini 2.5 Flash is deprecated: from 1 December 2026 its requests route to Gemini 3.5 Flash-Lite and bill at that model's rates, so re-check the pricing page before budgeting on the numbers above.

The second fix is to cap the rounds. A hard maximum of three tool rounds turns the five-round example into inputs of 20,500, 23,000 and 25,500 tokens — $0.0207 of input plus $0.00375 of output, $0.02445 in total, 45 percent cheaper than letting it run to five. The cap is not just a saving, it is a ceiling: the worst a task can cost is now a number you chose in advance, and a stuck loop fails loudly at round three instead of silently at round ten.

The third fix is to trim tool results before they enter the conversation. In the five-round example this helps less than you would expect: capping each tool result at 500 tokens instead of 2,000 brings the task to $0.04, only a 10 percent saving, because the fixed 20,000-token context still dominates. In the runaway example it is the fix that matters most: the same ten rounds with 500-token results cost $0.0875 instead of $0.2158, a 59 percent saving, because there the tool results were the bill. So trim where the tokens actually are — summarise or filter the tool output when results are large, and spend the caching effort on the fixed context when the context is large.

How to see it in your own logs

Do not guess which of these is your problem — the logs already say. Our Gemini logger, and the matching OpenAI middleware and Claude logger, record one line per call with the input, output, thinking and tool-use token counts. Group a day's lines by feature and look at two numbers: input tokens per task, and how fast tool_use_prompt_token_count climbs across the rounds of one task. If input per task is five to ten times the size of your system prompt, the loop is re-reading context — cache it. If the tool-use column grows by thousands every round, the results are too big — trim them.

The same invisible-output pattern sits behind our reasoning-token investigation: the tokens you pay for are not the tokens you can see. With agents, the hidden charge is repetition. Count the rounds, cache the context, cap the loop, and the agent that looked six times too expensive turns out to be one task priced as six calls.

Rates verified 16 November 2026 on Google's Gemini API pricing page (ai.google.dev/gemini-api/docs/pricing): Gemini 2.5 Flash standard tier, input $0.30, output $2.50 (including thinking tokens), cached input $0.03 per million tokens; explicit-cache storage $1.00 per million tokens per hour. All worked figures are arithmetic on those published rates, using the stated example token counts — they illustrate the mechanism, they are not measured runs. Gemini 2.5 Flash is deprecated and routes to Gemini 3.5 Flash-Lite from 1 December 2026, billed at that model's rates.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.