---
title: "AI coding agent run cost - steps, context and caching"
description: "Why a coding agent run costs more per step the longer it goes, how caching changes the total, and when a flat session beats per-token billing. Worked token math."
url: https://broadrouter.com/blog/ai-coding-agent-run-cost/
lang: en
updated: 2026-11-15
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# What one AI coding agent run costs: steps, context growth, caching

By the BroadNet Inference team · 7 min read · Published 2026-11-15

An agent resends its whole context on every step, so input grows with the square of the step count: 60 steps cost about 3.5 times the input of 30. Caching decides most of the bill; with append-only history, over 90% of input can bill at the cached rate. Multiply cost per run by monthly runs and compare with a flat session.

## Key takeaways

-   Total input for a run is steps × base context + growth per step × steps × (steps − 1) ÷ 2. Step count matters more than any other input.
-   With append-only history, only the tokens added since the last call are new. In the worked example that leaves 94% of a 30-step run's input cached.
-   At BroadNet's published DeepSeek V4.1 Flash prices, the same 30-step run costs about 8 times more with no cache than with a best-case cache.
-   Compacting or editing early history resets the cache. Do it rarely and in large steps.
-   A flat session wins when monthly runs × cost per run passes the plan price. One session runs one request at a time, so time per run caps how many runs fit in a month.

**On this page**

1.  How does context grow during an agent run?
2.  What does a worked run look like?
3.  How much does caching change the bill?
4.  How do you estimate your own run?
5.  What makes a run cost more than the estimate?
6.  When does a flat session beat per-token billing?
7.  Where BroadNet Inference fits

A coding agent doesn’t pay per task. It pays per step, and every step resends the whole conversation so far. That makes the cost of one run depend far more on how many steps it takes, and on how much of the context is cached, than on the price per token. This post builds the cost of one run from those parts, with numbers, and shows when a flat session beats paying per token.

For where the tokens inside each call come from (tool schemas, file reads, history), see [where an agent loop’s tokens go](https://broadrouter.com/blog/agent-token-budget/). This post is about the arithmetic of a whole run.

## How does context grow during an agent run?

Each call contains:

-   a **base context** sent on every call: system prompt, tool definitions, rules files, a repo map;
-   **everything added by earlier steps**: the model’s replies and tool calls, file contents and command output;
-   the new instruction or tool result.

Call that base context _B_ and the tokens each step adds _g_. Step 1 sends _B_. Step 2 sends _B + g_. Step _n_ sends _B + (n − 1) × g_. Add those up and a run of _n_ steps sends:

**Total input = n × B + g × n × (n − 1) ÷ 2**

The second term grows with the square of the step count. That is why long runs surprise people: the last step of a 60-step run is far bigger than the first, and every step in between pays for all the earlier ones again.

## What does a worked run look like?

Take an agent with a 15,000-token base context (system prompt, a dozen tools and a repo map). Each step adds 3,000 tokens: one file or test log plus the model’s own reply. Each step writes 800 output tokens, reasoning included.

| Steps | Total input | Output | Largest single call |
| --- | --- | --- | --- |
| 10 | 285,000 | 8,000 | 42,000 |
| 30 | 1,755,000 | 24,000 | 102,000 |
| 60 | 6,210,000 | 48,000 | 192,000 |

Going from 30 to 60 steps doubles the work but multiplies input by about 3.5. Output only doubles. Input is 36 to 130 times output in these runs, which is why the input price, and the cached input price, matter most for agents.

The largest call also has to fit the [context window](https://broadrouter.com/glossary/context-window/). 192,000 tokens fits DeepSeek V4.1 Flash and GLM 5.3 (1,048,576 tokens each) and Qwen3.8 27B (262,144). It doesn’t fit Gemma 4 VL 26B (32,768): in this example the seventh call is already too big.

## How much does caching change the bill?

Providers bill a repeated prompt prefix at a lower [cached input](https://broadrouter.com/glossary/cached-input-tokens/) rate. An agent that only appends to its history sends the same prefix every time plus a little more, so in the best case only the tokens added since the last call are new:

**New input (best case) = B + (n − 1) × g**

Everything else is a cache hit. The share of cached input rises as the run gets longer:

| Steps | New input, best case | Cached share |
| --- | --- | --- |
| 10 | 42,000 | 85% |
| 30 | 102,000 | 94% |
| 60 | 192,000 | 97% |

Real agents rarely reach the best case, so it helps to price three cases. At BroadNet’s DeepSeek V4.1 Flash prices ($0.175 input, $0.0055 cached, $0.52 output per 1M tokens, published 2026-11-14), here is each run’s cost relative to the 30-step best case:

| Steps | Best-case cache | 80% cached | No cache |
| --- | --- | --- | --- |
| 10 | 0.33× | 0.39× | 1.37× |
| 30 | 1× | 2.07× | 8.11× |
| 60 | 2.33× | 6.84× | 28.2× |

Two things stand out. With no cache, a 60-step run costs 28 times a well-cached 30-step run. And caching matters more the longer the run gets. In the 30-step best case, output is about a third of the cost, new input under half and cached input under a quarter, so after caching, output is where the next saving is.

Caching is not something you switch on. It depends on the provider keeping the prefix and on your agent not changing it. OpenAI’s guide describes exact prefix matching with a minimum cacheable length. DeepSeek’s own API stores cache units at the end of each request’s input and output, so a follow-up that extends the previous request can hit. BroadNet’s [pricing page](https://inference.broadnet.ai/pricing?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=body) says cached input needs no setup, and its docs report hits in `usage.prompt_tokens_details.cached_tokens` when the model provides them. BroadNet’s model catalogue lists DeepSeek V4.1 Flash among “models we never ran on our hardware”, and its Terms (§7.3) cover the model “when partner-hosted”, so on BroadNet it is served through a partner. Measure your hit rate rather than assuming DeepSeek’s own numbers. Write the prefix so it can cache: [prompt structure for caching](https://broadrouter.com/blog/prompt-structure-for-caching/).

## How do you estimate your own run?

This script reads current prices from BroadNet’s public [pricing.json](https://inference.broadnet.ai/pricing.json?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=body), so it never goes stale, and prints the token totals and cost per run for each cache case:

```python
import json, urllib.request

PRICES = json.load(urllib.request.urlopen("https://inference.broadnet.ai/pricing.json"))
m = next(x for x in PRICES["models"] if x["id"] == "broadnet/deepseek-v4.1-flash")
p_in, p_cached, p_out = m["input_per_m"], m["cached_input_per_m"], m["output_per_m"]

def run_tokens(steps, base=15_000, growth=3_000, out_per_step=800, cache_rate=None):
    total_in = steps * base + growth * steps * (steps - 1) // 2
    if cache_rate is None:                      # best case: only new tokens miss
        new_in = base + (steps - 1) * growth
    else:
        new_in = round(total_in * (1 - cache_rate))
    return total_in, new_in, steps * out_per_step

def run_cost(total_in, new_in, out):
    return ((new_in * p_in) + (total_in - new_in) * p_cached + out * p_out) / 1e6

for steps in (10, 30, 60):
    for label, rate in (("best", None), ("80%", 0.8), ("none", 0.0)):
        t, n, o = run_tokens(steps, cache_rate=rate)
        print(f"{steps:>3} steps  cache {label:>4}  input {t:>9,}  new {n:>9,}  "
              f"output {o:>6,}  cost/run {run_cost(t, n, o):.4f} USD")
```

Replace the defaults with your own numbers. Don’t guess them; read them from one real run:

1.  Log `prompt_tokens`, `prompt_tokens_details.cached_tokens` and `completion_tokens` for every call, as in [the agent token budget post](https://broadrouter.com/blog/agent-token-budget/).
2.  **B** is the `prompt_tokens` of the first call. **g** is the average increase in `prompt_tokens` from one call to the next.
3.  Output per step is the average `completion_tokens`. It includes [reasoning tokens](https://broadrouter.com/glossary/reasoning-tokens/), which bill as output.
4.  Your real cache rate is the sum of `cached_tokens` divided by the sum of `prompt_tokens`.

The free [agent run cost estimator](https://broadrouter.com/tools/agent-run-cost-estimator/) runs the same calculation in your browser, so you can try other step counts, growth rates and cache rates without code.

## What makes a run cost more than the estimate?

-   **Retries.** A failed tool call or a 429 retry is another full-context call. Count them as steps.
-   **Changing the prefix.** A timestamp in the system prompt, tools listed in a different order, or a rules file edited mid-run turns cached input back into new input from the change onward.
-   **Compaction.** Summarising history shrinks the context, which cuts later input, but the new history has never been cached, so the next call pays the full input price. Compact rarely and in large steps; [context management for long-running agents](https://broadrouter.com/blog/long-running-agent-context-management/) covers when.
-   **Big tool output.** A 20,000-line test log added at step 5 rides along on every later step. In the formula, it adds 20,000 × (remaining steps) input tokens.
-   **Reasoning effort.** High effort can multiply output per step. Set `reasoning_effort` to `low` for routine edits.
-   **Wandering.** An agent that takes 60 steps for a 30-step task pays the square, not the double. A step cap is a cost cap. [Ten ways to cut coding agent costs](https://broadrouter.com/blog/cut-coding-agent-llm-costs/) covers the levers.

## When does a flat session beat per-token billing?

Turn cost per run into a monthly figure: **monthly cost = cost per run × runs per month**. Then compare it with a flat plan. On BroadNet, the plans cover DeepSeek V4.1 Flash with no token meter: Full Moon is $60 a month, Orbit $15 a week and Meteor $1 an hour, each per session.

**Runs per month to break even = plan price ÷ cost per run.**

Two things make this comparison less simple than it looks:

1.  **Your cache rate sets the per-token side.** With a high cache rate, per-token billing on Flash is cheap and you need many runs to pass $60. With a poor one, you pass it quickly. The flat-rate post shows the break-even as a monthly volume: about 917 million input and 46 million output tokens at 80% cached ([the break-even math](https://broadrouter.com/blog/flat-rate-vs-pay-per-token-llm-api/)).
2.  **Time sets the flat side’s ceiling.** A session runs one request at a time. If one call takes 20 seconds on average (prefill, about 800 output tokens at about 100 tokens per second, plus tool time), a 30-step run takes about 10 minutes. One session then fits at most about six runs an hour. More parallel agents need more sessions.

For short bursts, Meteor works the same way at a smaller scale: if six runs fit in an hour, each costs $1 ÷ 6. Compare that with the script’s per-token cost for one run. To try your own volumes, use the [flat-rate vs per-token calculator](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/).

| Your pattern | Usually cheaper |
| --- | --- |
| A few short runs a day | Per token |
| Long runs with poor caching | Flat session |
| Agents running most of the working day | Flat session |
| Several agents in parallel, all day | One flat session per agent |
| Needs native tool calling on BroadNet | Per token on GLM 5.3 or Qwen3.8 27B |

## Where BroadNet Inference fits

BroadNet reports cached and reasoning tokens in the usage block, so you can measure every number above from your own runs. DeepSeek V4.1 Flash has a low cached-input price and a 1,048,576-token context, and the unlimited plans remove its token meter; [how sessions work](https://broadrouter.com/guides/unlimited-llm-api-sessions/).

Check one limit first. BroadNet’s docs list tool calling only for [GLM 5.3](https://broadrouter.com/models/glm-5.3/) and [Qwen3.8 27B](https://broadrouter.com/models/qwen3.8-27b/), and neither is on a flat plan. Agents that edit through text, such as [Aider](https://broadrouter.com/guides/aider-unlimited-deepseek/), don’t need native tool calls. If yours does, test DeepSeek V4.1 Flash with your agent before you buy a plan, or budget GLM or Qwen per token. And if your agent runs only now and then, paying per token will usually cost less than any flat plan.

Free tools for this

-   [Agent run cost estimator](https://broadrouter.com/tools/agent-run-cost-estimator/) Context growth per step, total tokens and cost of one agent run, with a DeepSeek session comparison.
-   [LLM batch job planner](https://broadrouter.com/tools/llm-batch-job-planner/) Run time, binding limit, cost per model and the flat sessions needed to finish a big job by a deadline.
-   [Flat-rate vs per-token LLM cost calculator](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/) Break-even between an unlimited plan and paying per token, per agent.
-   [LLM rate limit and concurrency calculator](https://broadrouter.com/tools/llm-rate-limit-calculator/) Which limit binds first: RPM, TPM or concurrency, with Little's law.

[All free tools](https://broadrouter.com/tools/)

## Terms in this article

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Tool calling](https://broadrouter.com/glossary/tool-calling/)

Tool calling, also called function calling, is an LLM API feature in which the model returns a structured request to run a function the developer has defined, with JSON arguments. The application runs the function and sends the result back; the model never runs code itself.

[Input and output tokens](https://broadrouter.com/glossary/input-and-output-tokens/)

Input tokens are the tokens a language model reads in a request, including the system prompt, conversation history, tool definitions and images. Output tokens are the tokens it generates in reply, including any reasoning. LLM APIs usually price them separately, and output usually costs more.

[Reasoning tokens](https://broadrouter.com/glossary/reasoning-tokens/)

Reasoning tokens are tokens a reasoning model generates to work through a problem before it writes its visible answer. They are usually billed as output tokens, count toward the output limit and the context window, and may be hidden or summarized by the API.

[System prompt](https://broadrouter.com/glossary/system-prompt/)

A system prompt is the instruction text an application sends to a language model ahead of the conversation, usually as a message with the system or developer role. It sets the model's role, rules, tone and output format, and the model is meant to follow it over conflicting user messages.

[AI coding agent](https://broadrouter.com/glossary/coding-agent/)

An AI coding agent is a software tool in which a language model works on a codebase in a loop: it reads files, edits them, runs commands such as tests, reads the output and decides the next step until the task is done or a limit is reached.

[AI agent](https://broadrouter.com/glossary/agent/)

An AI agent is a system in which a language model directs its own steps toward a goal: it decides which tools to call, reads the results and repeats until it produces a final answer or hits a limit. A fixed script that calls a model at set points is usually called a workflow instead.

[Context compaction](https://broadrouter.com/glossary/context-compaction/)

Context compaction is the practice of summarizing a conversation that is nearing the context window limit and continuing the session from that summary, so an agent keeps key decisions and state while dropping raw history such as old tool output.

## Questions

### How much does one AI coding agent run cost?

Count the tokens, then price them. In this post's example, a 30-step run with a 15,000-token base context and 3,000 tokens added per step sends 1,755,000 input tokens and 24,000 output tokens. What that costs depends mostly on how much of the input is billed at the cached rate.

### Why does a longer agent run cost disproportionately more?

Every step resends everything before it. Input per step grows in a straight line, so the total over a run grows with the square of the step count. Doubling a run from 30 to 60 steps multiplies its input by about 3.5 in the worked example.

### What cache hit rate should I expect from a coding agent?

It depends on the agent and the provider. With strictly append-only history, only the newest tokens miss, which gave 85% to 97% in our examples. Edited history, timestamps in the system prompt and long pauses lower it. Measure it from prompt\_tokens\_details.cached\_tokens.

### When is a flat session cheaper than per-token billing for an agent?

When cost per run × runs per month is more than the plan price. On BroadNet, Full Moon is $60 a month per session for DeepSeek V4.1 Flash. Light or occasional use is usually cheaper per token.

### Do reasoning tokens change the estimate?

Yes. They are billed as output, and output costs more per token than input. Put the reasoning you measure into output per step. On BroadNet, completion\_tokens\_details.reasoning\_tokens reports it when the model provides it.

## Keep reading

-   [Agent token budget: where an agent loop's tokens go](https://broadrouter.com/blog/agent-token-budget/)
-   [10 ways to cut coding agent LLM costs, with estimated savings](https://broadrouter.com/blog/cut-coding-agent-llm-costs/)
-   [Flat-rate vs pay-per-token LLM API: the break-even math](https://broadrouter.com/blog/flat-rate-vs-pay-per-token-llm-api/)
-   [Cached input tokens: how to cut an LLM API bill](https://broadrouter.com/blog/cached-input-tokens-explained/)
-   [Context management for long-running agents with a 1M window](https://broadrouter.com/blog/long-running-agent-context-management/)
-   [Claude Code with open models: proxies, native endpoints and limits](https://broadrouter.com/blog/claude-code-with-openai-compatible-models/)
-   [OpenClaw and Hermes Agent on a cheap model: setup and token math](https://broadrouter.com/blog/openclaw-hermes-agent-cheap-model/)
-   [Guide: How the unlimited LLM API plans work for agents](https://broadrouter.com/guides/unlimited-llm-api-sessions/)
-   [Guide: Run Aider on unlimited DeepSeek V4.1 Flash](https://broadrouter.com/guides/aider-unlimited-deepseek/)

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=page-cta) ·

**Sources**

-   [BroadNet Inference API reference: usage fields, cached\_tokens, reasoning counts toward max\_tokens, limits (checked 2026-11-15)](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=sources)
-   [BroadNet Inference pricing page and pricing.json, prices updated 2026-11-14 (checked 2026-11-15)](https://inference.broadnet.ai/pricing?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=sources)
-   [BroadNet Inference plans page: one request at a time per session, no token meter (checked 2026-11-15)](https://inference.broadnet.ai/plans?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-ai-coding-agent-run-cost&utm_content=sources)
-   [OpenAI prompt caching guide: prefix matching and minimum cacheable length (checked 2026-11-15)](https://developers.openai.com/api/docs/guides/prompt-caching)
-   [DeepSeek API docs, context caching: cache units at request boundaries (checked 2026-11-15)](https://api-docs.deepseek.com/guides/kv_cache)
