Cost optimizationBreakdown
How to estimate the cost of an AI agent
Why agent bills are dominated by input tokens, a formula you can run before the agent does, a worked 30-step example and seven ways to cut the cost.

On this page
An AI agent doesn't make one request — it makes dozens, and every step re-sends the growing conversation. That's why agent bills are dominated by input tokens, and why the "price per answer" intuition from chat apps doesn't work. Here is how to estimate the cost before the agent runs, and where the money actually goes.
The formula#
The cost of one request is input tokens times the input price plus output tokens times the output price. Prices are quoted per million tokens:
request_cost = input_tokens / 1,000,000 × input_price
+ output_tokens / 1,000,000 × output_priceThe cost of a task is the sum over all requests the agent makes while solving it.
Why agents are input-heavy#
On every step the agent sends the model:
- the system prompt and the tool definitions;
- the whole history so far — earlier messages, the model's previous answers and tool results (file contents, command output, search results);
- the new instruction.
The model usually answers with a short message or a tool call. Then the tool result is appended to the history and the next step sends everything again. So the input of each step grows, while the output stays small.
A worked example#
Conditions (assumptions — replace them with numbers from your agent):
- system prompt and tool definitions: 6,000 tokens;
- each step adds 1,500 tokens of tool results and 500 tokens of model output to the history;
- the task takes 30 steps;
- model: DeepSeek-V4-Flash at $0.0221 per 1M input tokens and $0.10 per 1M output tokens (current prices).
The input of step n is 6,000 + (n − 1) × 2,000 tokens. Over 30 steps:
- total input: 30 × 6,000 + 2,000 × (0 + 1 + … + 29) = 180,000 + 870,000 = 1,050,000 tokens;
- total output: 30 × 500 = 15,000 tokens;
- cost: 1.05 × $0.0221 + 0.015 × $0.10 ≈ $0.0247 per task.
Input is 98.6% of the tokens and almost all of the cost. How much input dominates depends on the scenario: long answers, generated files and reasoning models raise the output share, while a trimmed history and short tool output lower the input share.
The same task on every model at current prices, with OpenRouter's reference prices for comparison:
| Model | Input $/1M | Output $/1M | Cost | OpenRouter |
|---|---|---|---|---|
| GLM-5.3-Flash | $0.027 | $0.08 | $0.0296 | $0.0534 |
| DeepSeek-V4-Flash | $0.0221 | $0.10 | $0.0247 | $0.0264 |
| MiniMax-M2.7 | $0.05 | $0.15 | $0.0548 | $0.333 |
1,050,000 input and 15,000 output tokens in total. Prices per 1M tokens are current and change together with our tariffs; the OpenRouter column shows reference prices for the same workload.
Estimate it in code#
The same calculation as a function — change the assumptions and the prices:
def estimate(steps, base_tokens, tool_tokens, out_tokens, price_in, price_out):
"""Token totals and cost of one agent task. Prices: $ per 1M tokens."""
grow = tool_tokens + out_tokens # what each step adds to the history
total_in = steps * base_tokens + grow * steps * (steps - 1) // 2
total_out = steps * out_tokens
cost = (total_in * price_in + total_out * price_out) / 1_000_000
return total_in, total_out, round(cost, 5)
# example rates, $ per 1M tokens — put in the current ones from the pricing table below
print(estimate(30, 6_000, 1_500, 500, price_in=0.10, price_out=0.40))
# (1050000, 15000, 0.111)Note the quadratic term: doubling the number of steps roughly quadruples the input. Long sessions get expensive not because of any single step, but because every step carries the entire history.
Measure instead of guessing#
Every response includes usage. (input) and usage. (output). Log them for each step of a real task, multiply by the current rates, and you have the actual cost. The dashboard also shows usage by model and by day.
Current rates — the same data as on the pricing page:
| Model | Context | Input $/1M | Output $/1M | OpenRouter in | OpenRouter out |
|---|---|---|---|---|---|
| GLM-5.3-Flash reasoning temporarily unavailable | 390K | $0.027 | $0.08 | $0.048 | $0.20 |
| DeepSeek-V4-Flash temporarily unavailable | 380K | $0.0221 | $0.10 | $0.016 | $0.64 |
| MiniMax-M2.7 temporarily unavailable | 200K | $0.05 | $0.15 | $0.30 | $1.20 |
Prices in USD per 1M tokens. OpenRouter figures shown for comparison and may change.
Seven ways to cut the cost#
- Start a new task for unrelated work. A fresh session doesn't carry the history of the previous one.
- Give the agent fragments, not whole files. A function and its callers instead of a 3,000-line module.
- Trim tool output. Long logs and command output stay in the history for the rest of the session — limit them, for example with
| tail -n 50. - Summarize long sessions. Many agents can compact the history; use it before the context gets long.
- Cap the answer length with
max_tokenswhere long answers aren't needed. - Use the right model for the step. Routine steps don't need a reasoning model.
- Keep a prepaid budget. The balance is prepaid: when it runs out, requests stop with HTTP 402 instead of running up an open-ended bill.
To see how the same workload compares across models and with OpenRouter, use the cost comparison on the home page.



