ClawPulse
English··AI agent token calculator

AI Agent Token Calculator: How to Estimate and Cut Your LLM Costs in 2026

If you are running an AI agent in production, the difference between a profitable feature and a budget disaster often comes down to one number: tokens per request. A single autonomous Claude agent looping through tool calls can burn 50,000 tokens in under a minute, which translates to roughly $0.75 per execution at current Sonnet 4.6 pricing. Multiply that by 10,000 daily users and the math gets uncomfortable fast.

This guide shows you exactly how to calculate AI agent token consumption, what drives the cost up, and how teams using observability tools like ClawPulse cut their token spend by 40-70% without degrading quality.

Why Token Calculators for Agents Are Different

A simple chat completion is easy to estimate: you have one input prompt and one output. Multiply by the per-token rate and you are done. Agents are not chats.

An agent typically:

1. Receives a user query

2. Reads a system prompt (often 2,000-8,000 tokens)

3. Loads tool definitions (500-3,000 tokens depending on schema complexity)

4. Calls one or more tools, each returning a result that gets re-injected into context

5. Loops until the task completes or hits a max-iterations guard

Each iteration re-sends the entire conversation history, including all prior tool outputs. This is the part most token calculators miss. A 5-step agent does not cost 5x a single call. It costs closer to 15-25x because of the quadratic context growth.

Here is the actual formula:

```

total_tokens = sum(input_tokens_at_step_n + output_tokens_at_step_n) for n in steps

where input_tokens_at_step_n = system + tools + user + sum(prior_messages_and_tool_results)

```

That `sum(prior_messages_and_tool_results)` term is what kills budgets.

The Real Math: A Worked Example

Let us take a customer support agent built on Claude Sonnet 4.6. Pricing (as of April 2026): $3.00 per million input tokens, $15.00 per million output tokens, with prompt caching at $0.30 per million for cache reads.

Setup:

  • System prompt: 4,500 tokens
  • Tool definitions (5 tools): 1,800 tokens
  • User query: 150 tokens
  • Average tool result: 1,200 tokens
  • Average assistant reasoning per step: 400 output tokens
  • Steps to completion: 4

Without prompt caching:

| Step | Input tokens | Output tokens | Step cost |

|------|--------------|---------------|-----------|

| 1 | 6,450 | 400 | $0.0254 |

| 2 | 8,050 | 400 | $0.0302 |

| 3 | 9,650 | 400 | $0.0350 |

| 4 | 11,250 | 400 | $0.0398 |

| Total | 35,400 | 1,600 | $0.1304 |

With prompt caching enabled (system + tools cached after step 1):

| Step | New input | Cached read | Output | Step cost |

|------|-----------|-------------|--------|-----------|

| 1 | 6,450 (write) | 0 | 400 | $0.0322 |

| 2 | 1,750 | 6,300 | 400 | $0.0153 |

| 3 | 1,750 | 6,300 | 400 | $0.0153 |

| 4 | 1,750 | 6,300 | 400 | $0.0153 |

| Total | — | — | — | $0.0781 |

40% savings from caching alone, with zero quality impact. If you are not using prompt caching for your agents, that is the first lever to pull. Anthropic's prompt caching docs walk through the API specifics.

A Working Token Calculator in Python

Here is a minimal calculator you can drop into your codebase. It uses Anthropic's official tokenizer for Claude and `tiktoken` for OpenAI models.

```python

import anthropic

import tiktoken

client = anthropic.Anthropic()

PRICING = {

"claude-sonnet-4-6": {"input": 3.00, "output": 15.00, "cache_read": 0.30, "cache_write": 3.75},

"claude-opus-4-7": {"input": 15.00, "output": 75.00, "cache_read": 1.50, "cache_write": 18.75},

"gpt-4o": {"input": 2.50, "output": 10.00},

"gpt-4o-mini": {"input": 0.15, "output": 0.60},

}

def count_claude_tokens(messages, system, tools, model="claude-sonnet-4-6"):

response = client.messages.count_tokens(

model=model,

system=system,

tools=tools,

messages=messages,

)

return response.input_tokens

def estimate_agent_cost(system, tools, user_query, avg_tool_result_tokens,

avg_output_tokens, steps, model="claude-sonnet-4-6",

use_caching=True):

p = PRICING[model]

base_input = count_claude_tokens(

messages=[{"role": "user", "content": user_query}],

system=system, tools=tools, model=model

)

total_cost = 0

cumulative_context = base_input

for step in range(1, steps + 1):

if use_caching and step > 1:

cached = base_input

new_input = cumulative_context - base_input

cost = (cached p["cache_read"] + new_input p["input"]) / 1_000_000

else:

cost = (cumulative_context * p["input"]) / 1_000_000

cost += (avg_output_tokens * p["output"]) / 1_000_000

total_cost += cost

cumulative_context += avg_output_tokens + avg_tool_result_tokens

return round(total_cost, 4)

# Example

cost = estimate_agent_cost(

system="You are a helpful customer support agent...",

tools=[...],

user_query="My order hasn't arrived",

avg_tool_result_tokens=1200,

avg_output_tokens=400,

steps=4,

)

print(f"Estimated cost per agent run: ${cost}")

```

For OpenAI models, swap `client.messages.count_tokens` for `tiktoken.encoding_for_model(model).encode(text)`. The tiktoken repo has full encoding tables.

What Makes Token Counts Explode

After auditing hundreds of agent traces, the same five culprits show up:

1. Tool result bloat

A web search tool returning the full HTML of 10 results can dump 50,000+ tokens into context per call. Truncate aggressively. If your agent only needs the first paragraph, return only that.

2. Verbose system prompts

A common pattern is to keep stuffing edge-case instructions into the system prompt. Every new line costs you on every single API call across every conversation. Consolidate, then test removal — you will be surprised what the model handles fine without explicit instruction.

3. Unbounded loops

Agents without a hard `max_iterations` guard can spiral. We have seen agents loop 80+ times trying to fix a tool error. Set `max_iterations=10` as a default and alert on anything that hits it.

4. Re-injecting prior tool results

Some frameworks keep all tool results in context forever. After step 5, you may not need the result from step 1. Implement context pruning: drop tool results older than N steps.

5. Thinking tokens

Extended thinking on Claude Opus 4.7 can produce 5,000-15,000 reasoning tokens that you pay for at the output rate. Worth it for hard problems, expensive for trivial ones. Use `thinking.budget_tokens` to cap it.

Start monitoring your OpenClaw agents in 2 minutes

Free 14-day trial. No credit card. Just drop in one curl command.

Prefer a walkthrough? Book a 15-min demo.

Comparing Calculators: Langfuse, Helicone, ClawPulse

Most observability platforms include some form of token tracking. Honest comparison:

  • Langfuse offers detailed token cost breakdowns per trace and supports custom model pricing. Strong for offline analysis. Self-hosted setup is involved.
  • Helicone intercepts via proxy and gives clean per-request cost views. The proxy adds 50-200ms latency in our tests, which matters for agentic workflows that chain calls.
  • ClawPulse focuses on real-time agent observability without a proxy — SDK injection only — and exposes a per-step token waterfall that visualizes exactly where context bloats. See it in action on the demo page.

If you are tracking single chat completions, any of these works. If you are running multi-step agents and want to see which step in a 12-step trace cost you $0.40, the per-step waterfall view becomes essential. We dig into that more in our agent observability comparison.

Practical Cost Optimization Checklist

Before shipping any agent to production, run through these:

  • [ ] Enable prompt caching for system prompt + tool definitions (40-90% savings on repeat calls)
  • [ ] Set `max_iterations` and alert when reached
  • [ ] Truncate tool results to the minimum useful size
  • [ ] Use the cheapest model that passes your eval suite — Haiku 4.5 is often enough
  • [ ] Log per-step token counts, not just per-trace totals
  • [ ] Set up budget alerts at the user/tenant level, not just account-wide
  • [ ] Review your top 10 most expensive traces weekly

Teams that follow this checklist routinely see token spend drop 50% in the first month. The pricing page breaks down what monitoring this looks like at scale.

Quick Reference: Token Costs Across Major Models (April 2026)

| Model | Input ($/M) | Output ($/M) | Best for |

|-------|------------|-------------|----------|

| Claude Haiku 4.5 | $0.80 | $4.00 | High-volume agents |

| Claude Sonnet 4.6 | $3.00 | $15.00 | Balanced production agents |

| Claude Opus 4.7 | $15.00 | $75.00 | Complex reasoning, low volume |

| GPT-4o | $2.50 | $10.00 | OpenAI ecosystem |

| GPT-4o mini | $0.15 | $0.60 | Cheap classification |

| Gemini 2.5 Pro | $1.25 | $5.00 | Long context (2M tokens) |

Always check Anthropic's pricing page and OpenAI's pricing for current rates — these change.

FAQ

How accurate are token calculators?

For Claude, the official `client.messages.count_tokens` endpoint is exact — it uses the same tokenizer as the inference API. For GPT models, `tiktoken` is exact for the listed encodings. Estimates start to drift only when you predict future token usage (output length, number of agent steps), not when you measure existing prompts.

Why does my agent cost more than the calculator predicts?

Almost always one of: (1) tool results are larger than estimated, (2) the agent ran more steps than expected, or (3) you forgot to account for retries on failed tool calls. Real production traces from ClawPulse or Langfuse will show the actual numbers.

Does prompt caching work for agents that change every call?

Yes — caching applies to the prefix of your prompt. As long as your system prompt and tool definitions are identical, the user query and conversation history can vary freely. The cache hits on everything up to the first divergent token.

Should I use Haiku or Sonnet for agents?

Test both with your eval suite. Haiku 4.5 handles 70-80% of agentic workflows we have benchmarked at one-quarter the cost. Use Sonnet when Haiku fails your eval, and Opus only when Sonnet fails.

---

Stop guessing what your agents cost. Try the ClawPulse demo and watch a live token waterfall on your own agent traces — no credit card, no proxy, drop-in SDK.

See ClawPulse in action

Get a personalized walkthrough for your OpenClaw setup — takes 15 minutes.

Or start a free trial — no credit card required.

Back to all posts
C

Claudio

Assistant IA ClawPulse

Salut 👋 Je suis Claudio. En 30 secondes je peux te montrer comment ClawPulse remplace tes 12 onglets de monitoring par un seul dashboard. Tu veux voir une demo live, connaitre les tarifs, ou connecter tes agents OpenClaw maintenant ?

Propulse par ClawPulse AI