# How Do You Control Agent Cost?

> How agents multiply token bills, and the hard limits, caching and spend caps that keep an agent's cost under control, explained simply for your AI interview.

**In short:** Set hard limits in code, such as a step limit and a token ceiling, and a monthly spend cap at the provider, so a run stops and hands off instead of running up the bill. Then cut the cost of each step with caching and smaller models for easy work.

AI and LLM interview question Q187, Agents and tool use. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-control-agent-cost

## Say it in 30 seconds

Every agent step re-reads everything so far, so its bill grows faster than its steps. I control it in a few places. I count tokens before sending. I cap each run in code with a step limit and a token ceiling, and hand the case to a person when a limit is hit. I set a monthly spend cap with the provider, and per-user quotas in my code. Then I make repeated parts cheaper with caching, and measure cost per finished task.

## Say it in 2 minutes

An agent does not answer once. It loops: the model picks a tool, the tool returns a result, and the model reads everything again before its next step. Each pass is billed, and the re-reading is the big cost, because the history keeps growing. A chat reply reads the question once. An agent run can read the same claim file many times, and Anthropic's own data shows agents using several times the tokens of a chat.

So I put the limits in code. A prompt can be ignored, but code can count steps and tokens, stop the run when a limit is hit, and send the case to a person. I also set a monthly spend cap with the provider and a quota per user. OWASP treats this as a security issue too, because attackers can force costly work on the service and run up its bill. It calls that Denial of Wallet.

Then I make each step cheaper. I cache the parts that repeat, such as the instructions and the tool list, and I keep the tool list fixed, because a change to it clears the cache. Easy steps can go to a smaller model. Cutting tokens can cut quality, though, so I measure the cost of each finished task and test the limits on real cases.

The honest part is that no cap is perfect. A token count is only an estimate, and a stopped run still needs a good handoff. I tell the interviewer that the goal is a bill you can predict and a run that fails safely.

## What does controlling agent cost mean, in plain words?

An AI agent does not answer once. It works in a loop: it asks the model what to do next, runs a tool such as a search, reads the result, and asks again. Every pass is billed. You pay for the tokens the model reads and writes, and a token is a small piece of text, often part of a word.

The big cost is the re-reading. Each step sends the whole history again: the task, the instructions, and every tool result so far. So the bill grows faster than the number of steps.

Think of a claims clerk who is paid per page read and must re-read the whole claim file from page one before each new form. The pay grows with every form, even when the work stays simple. A claims-triage agent does exactly this to your bill on every step.

- **Token:** A small piece of text. Models are billed by the number they read and write.
- **Agent loop:** The repeat cycle: model step, tool call, result, then the model step again.
- **Prompt cache:** A saved copy of the repeated start of a request, which the provider re-uses at a lower price.
- **Circuit breaker:** A switch in code that ends a run when a limit is hit, like a fuse in a wall.
- **Spend cap:** A monthly ceiling on what an account or project may spend on the API, the way apps call the model.

## Why does an agent cost so much more than one chat reply?

A chat reply reads your question once and writes one answer. An agent run repeats the model many times, and each repeat carries everything before it. Anthropic measured the gap in its own multi-agent research system, and it is large.

- **One chat reply: Read once, answered once.** The model reads your question and the chat so far, then writes one answer.
- **One agent run: Read again on every step.** Each step re-reads the task, the instructions and every tool result so far.

*Diagram, The agent loop, and what gets billed on each pass: The agent loop repeats. The model asks for a tool, the result goes back into the history, and the next step reads all of it again. Every pass is billed, so a long file or a big search result makes the bill grow.*
1. The claim file starts the run.
2. The model asks for a tool, and this pass is billed.
3. The result goes back in, so the next step reads more.

> "In our data, agents typically use about 4× more tokens than chat interactions, and multi-agent systems use about 15× more tokens than chats." (Anthropic, on its multi-agent research system)
>
> "The autonomous nature of agents means higher costs, and the potential for compounding errors." (Anthropic, on building effective agents)

The practical point: cost follows the number of loops, so the controls belong on the loop itself.

## Which hard limits belong in the code?

Put the limits in code. A prompt asks the model to behave, while code decides what the run is allowed to do. There are four things to set up.

1. **Count before you send:** Measure a request before the model sees it. Anthropic's token counting exists for this, and its count is only an estimate.
2. **Cap the run:** Set a step limit, a token ceiling and a time limit for every run. OWASP, a security group that publishes a top-10 risk list for AI apps, says in its 2026 list that agent runs need these, enforced on every execution.
3. **Hand over, don't just fail:** When a limit is hit, stop the run in code and send the case to a person with what was done so far. A silent failure leaves the claim stuck.
4. **Set a spend cap at the provider:** Keep a monthly cap on the provider side (the company that runs the model) as the last wall. Anthropic lets you set your own limit below your account's usage tier cap, the spending level it gives your account, and requests stop once it is reached.

Here is a small run with two limits. No real model is used, and each step re-reads the history, which is the part that costs.

```python
# A simplified claims-triage agent with a hard budget. No real model: each step re-reads the whole history.
MAX_STEPS = 6
MAX_TOKENS = 30_000  # ceiling for one claim, across all steps

def run_claim(claim_id, steps_needed, tokens_per_step):
    history = 2_000  # the claim form and the policy text
    spent = 0
    for step in range(1, MAX_STEPS + 1):
        if spent + history > MAX_TOKENS:
            return f"{claim_id}: STOPPED before step {step}, sent to a person"
        spent += history  # the model reads the whole history again
        history += tokens_per_step  # each tool result is added to it
        if step == steps_needed:
            return f"{claim_id}: DONE in {step} steps, {spent} tokens"
    return f"{claim_id}: STOPPED at the {MAX_STEPS}-step limit, sent to a person"

print(run_claim("CLM-1001", steps_needed=3, tokens_per_step=3_000))
print(run_claim("CLM-1002", steps_needed=9, tokens_per_step=1_000))
print(run_claim("CLM-1003", steps_needed=5, tokens_per_step=6_000))
```

Output:

```text
CLM-1001: DONE in 3 steps, 15000 tokens
CLM-1002: STOPPED at the 6-step limit, sent to a person
CLM-1003: STOPPED before step 4, sent to a person
```

Claim 1 finishes in three steps, for 15,000 tokens. Claim 2 hits the six-step limit and goes to a person. Claim 3 stops before its fourth step, because one more read of the history would take it past the token ceiling in the code. A stopped run spends nothing more after that point.

*Diagram, Limits that stop a run in code: The model proposes each step, but code decides whether it runs. Two checks come before the run and two limits sit during it. Any limit that is hit ends the run and hands it to a person.*
1. The size check passes the request to the agent.
2. The user's quota lets the run start, or blocks it.
3. Every step is checked against the limits.
4. A hit limit ends the run and hands it to a person.

## How do you make each step cheaper?

Once the limits are in place, make each step cheaper, and start with prompt caching. That keeps a saved copy of the repeated start of each request, so the provider can re-use it cheaply. The instructions and the tool list repeat on every step, so a cache can serve them. Saving the copy is a write, and re-using it is a read. Anthropic's prompt caching docs, checked in November 2026, bill a cache read at 10 percent of the base input price, with per-model exceptions. A 5-minute cache write costs 25 percent more than base input, a 1-hour write costs 2x base input, and the default cache lasts 5 minutes.

Two mistakes break the cache. Changing a tool's name, description or parameters clears the whole cache. And a timestamp inside the saved part means the saved copy never matches, so that part is written again on every request, at the higher write price, instead of being read back cheaply.

Easy steps can go to a smaller model. Anthropic's building-effective-agents guide gives routing easy, common questions to smaller models as a pattern, and keeping hard ones on a stronger model.

> **Tip:** Trim big tool results before they go back into the history. A 40-page file pasted in full on every step can make up much of the bill.

## What about someone running up your bill on purpose?

Cost is also a security problem. OWASP's 2026 list calls it Unbounded Consumption. One attack is Denial of Wallet: the attacker pays almost nothing, while your side pays for each expensive step of the loop, and your bill and your service both suffer.

> "Traditional request-rate limiting alone is no longer sufficient." (OWASP, LLM06:2026)

So per-second limits are only part of the defence. OWASP's answer includes rate limiting and user quotas, token-aware cost controls, budget ceilings per user and per key, hard caps that halt inference when they are hit, and monitoring that attributes cost. An alert alone is too slow, because fast workloads outrun it. It also warns that a long open session pays more on every turn, since per-turn cost climbs as the context grows.

> **Tip:** Asked how to stop a runaway bill, don't promise a perfect forecast. Say the run halts at a hard ceiling in code, and that you track the cost of each finished task.

### How would you cap a support agent's monthly bill?

A bank runs a support agent that handles about 40,000 chats a month. One chat that loops on a bad document could use many times a normal chat's tokens, and a monthly invoice would show it too late.

- **Per chat, in code:** A step limit and a token ceiling. When either is hit, the chat goes to a human agent with a summary.
- **Per customer, daily:** A token quota, so one account cannot use up the team's shared budget.
- **Per month, at the provider:** A spend cap below the account's usage limit, set in the Claude Console, so requests stop before the surprise.
- **Per week, for the team:** An alert at half the cap, and cost per resolved chat on a dashboard, so drift shows early.

The trade-off is real. A tight ceiling can cut a valid chat short, so the handoff to a person has to work every time. A provider cap is blunter: once it is hit, requests fail for the whole bank until the next calendar month or until the account moves to a higher spending level, so every chat stops at once.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Agent cost compounds, because each step re-reads the history.
- [ ] Limits live in code: steps, tokens and time. The spend cap sits at the provider.
- [ ] What happens at a limit: the run stops and a person takes over.
- [ ] Cheaper steps: caching, with its write cost, and smaller models for easy work.
- [ ] Attackers: quotas per user, because cost is also a security risk.
- [ ] Measure the cost of each finished task.

**Common traps**

- Trap: "A stronger prompt will keep the agent in budget." Better: A prompt asks the model to behave. Put the step limit and token ceiling in code, where the model cannot skip them.
- Trap: "Just use a cheaper model." Better: A smaller model helps the easy steps. It does not stop a loop, so the run still needs a cap in code.
- Trap: "Caching makes the cost go away." Better: A cache read is cheap, but a write costs more, and changing a tool definition clears the cache.

## Can you spot the cost leak?

**Spot the cost leak:** A claims agent's bill rose after a release. Which line in the design is the likely cause?

A. Each claim file is read once and kept in the cache for the whole run.
B. Each request puts the current time at the top of the cached instructions.
C. The step limit is set to six steps for each claim.
D. Final answers are capped at a fixed length of output tokens.

**Answer:** B. A changing value at the top of the cached part makes the start of every request differ, so the cache never hits and each request pays the write price again instead of a cheap read. Keep the tool list fixed, and keep changing values out of the cached part.

**Quick check:** Which change puts a hard ceiling on one agent run?

A. A longer system prompt that asks for brevity
B. A step limit and a token ceiling checked in code
C. A bigger model for the hard steps
D. A lower temperature (the setting that makes the wording less random) on every call

**Answer:** B. A prompt can be ignored. Code that counts steps and tokens stops the run at the number you set. A bigger model or a lower temperature leaves the number of steps unchanged.

## Can you answer these from memory?

**Flash cards**

- **Why does an agent cost more than a chat?** Each step re-reads the whole history, so the bill grows with every loop.
- **What is a step limit?** A set maximum number of model steps. Code stops the run when it is hit.
- **What is a circuit breaker here?** A switch in code that ends a run at a limit and hands it to a person.
- **What does a cache read cost?** 10 percent of base input price, with per-model exceptions, per Anthropic.
- **What is Denial of Wallet?** An attack that forces costly work on an AI service, so the victim's bill rises.
- **What is a spend cap?** A monthly ceiling on API spend, set with the provider and enforced by it.

## Likely follow-up questions

- Q173 How do you stop endless agent loops? https://designervenkat.online/interview/ai/how-do-you-stop-endless-agent-loops
- Q176 How should an agent handle tool errors and retries? https://designervenkat.online/interview/ai/how-should-an-agent-handle-tool-errors-and-retries
- Q182 How do long-running agents use checkpoints? https://designervenkat.online/interview/ai/how-do-long-running-agents-use-checkpoints
- Q030 Why are input and output tokens priced differently? https://designervenkat.online/interview/ai/why-are-input-and-output-tokens-priced-differently
- Q105 How do you budget tokens for chat history? https://designervenkat.online/interview/ai/how-do-you-budget-tokens-for-chat-history
- Q181 How do you evaluate agents? https://designervenkat.online/interview/ai/how-to-evaluate-ai-agents
