# The agent worked fine. It cost $4,260.

> Nothing failed. The loop just paid for its own history, twelve times per task.

- **Format:** short video, 12 steps, ~51 seconds
- **Topic:** Why an AI agent loop costs far more than it looks like it should — resent context, quadratic input growth, prompt caching, and the one line that silently switches caching off.
- **Author:** Suthahar Jegatheesan (MSDEVBUILD)
- **Category:** Azure · Azure
- **Tags:** ai, aiagents, promptcaching, llm, tokens, claude, azureai, aifoundry, finops, cloudcost, dotnet, systemdesign, aiengineering, msdevbuild
- **Canonical URL:** https://blog.msdevbuild.com/shorts/ai-agent-token-cost/

---
## What you'll learn

- Why an agent loop’s input tokens grow with the square of its step count
- What prompt caching actually charges — reads at 0.1×, writes at 1.25×
- The one line that silently stops the cache being read, with no error

## Understand it one step at a time

### 1. The agent cost $4,260 last month

One triage agent. Four thousand tasks. The invoice was six times the estimate.

### 2. Nobody wrote a bug

Every task succeeded. There is no incident to find, which is exactly why it ran for a month.

### 3. The API has no memory

Every step is a fresh request carrying the entire conversation so far. Nothing is stored between calls.

### 4. Step 12 pays for steps 1 to 11

The bars are one task. Each one is longer than the last because it carries everything before it.

### 5. Twelve steps. 195,000 input tokens

Add the bars up. The steps grow in a straight line, so the total grows as a square.

### 6. Output costs 5x more, and barely matters

Output is the expensive token and the rare one. The agent barely speaks; it mostly re-reads.

### 7. Prompt caching reprices the repeat

A cached prefix reads back at about a tenth of the input rate. Writing it costs 1.25 times.

### 8. The same run, 32 cents

Same agent, same steps, same tokens. $2,976 a month stops leaving the account.

### 9. One line puts the bill back

The cache is a prefix match. Change a single byte near the top and everything after it is new.

### 10. Nothing errors. Nothing warns

Caching is switched on, the code is correct, and it has never been read once. Only one number says so.

### 11. Three fixes, in this order

Freeze what repeats, cache it, and stop dragging dead tool output through every later call.

### 12. The loop is the bill

Your agent is not expensive because it thinks. It is expensive because it re-reads.

---

## The takeaway

**Agents are not billed for thinking. They are billed for re-reading**

Freeze the prefix, cache it, prune the history — then check that the cache is actually being read.
