Safeguard
AI Security•

How to estimate what one AI chat or agent session costs

A method and a runnable calculator for pricing one chat or coding agent session in tokens, including context growth and prompt caching, with list prices fetched on 18 October 2026.

Safeguard Security Team
7 min read

A coding agent session that produces 24,000 tokens of answers can bill more than three million input tokens. The reason is that every turn re-sends the whole conversation. This post gives you a method and a calculator to estimate the list price of one chat or one agent session before you run it.

The prices below come from Anthropic's and OpenAI's own pricing pages, fetched on 18 October 2026. The sessions are made up scenarios, not measurements of any real product, and the results are estimates of list price, not of what any provider spends.

The method in four steps

  1. Count the fixed prefix. The system prompt, tool definitions and any files you load at the start. Every turn pays for it again.
  2. Count what each turn adds. The new user message or tool result (new input) and the model's reply (output). Both stay in the context for later turns.
  3. Price each turn. Input tokens cost the input rate (or the cache read rate if they were cached), the new tokens cost the cache write rate if you cache, and output costs the output rate.
  4. Add up the turns. The context grows by new input plus output every turn, so total input grows roughly with the square of the number of turns.

Step 4 is why long sessions surprise people. Ten turns is ten times the work of one turn, but the input you pay for is far more than ten times as large.

Where the rates come from

ModelInputCache write (5 min)Cache readOutput
Claude Sonnet 5.5$2.00$2.50$0.10$10.00
Claude Opus 5.5$4.00$5.00$0.20$20.00
gpt-6.1-sol$2.00$2.50$0.10$10.00
gpt-6-luna$0.10$0.125$0.01$0.50

All prices are USD per million tokens. Anthropic publishes cache writes and reads on its pricing page. For OpenAI's GPT-6 models the page lists cached input directly and says cache writes are priced at 1.25x input, which is how the gpt rows above are built. Token counts are not comparable between vendors, because tokenizers differ. Anthropic says Claude 4.7 and later models produce about 30 percent more tokens than earlier ones for the same text.

The calculator

Save this as calc.py and run it. It assumes each turn arrives within the cache lifetime, so every earlier token is a cache hit.

# Prices in USD per million tokens (from vendor pages, 18 Oct 2026). Edit these.
PRICES = {
    "claude-sonnet-5.5": {"in": 2.00, "out": 10.00, "cache_write": 2.50, "cache_read": 0.10},
    "claude-opus-5.5":   {"in": 4.00, "out": 20.00, "cache_write": 5.00, "cache_read": 0.20},
    "gpt-6.1-sol":       {"in": 2.00, "out": 10.00, "cache_write": 2.50, "cache_read": 0.10},
    "gpt-6-luna":        {"in": 0.10, "out": 0.50,  "cache_write": 0.125, "cache_read": 0.01},
}

def session_cost(p, turns, prefix, new_in, out, cached=True):
    """Each turn re-sends the whole context. The context grows by new_in + out tokens per turn."""
    total = 0.0
    context = prefix
    for t in range(turns):
        if cached and t > 0:
            read = context                       # everything from earlier turns is a cache hit
            write = new_in                       # only the new tokens are written to cache
            total += read * p["cache_read"] + write * p["cache_write"]
        elif cached:
            total += context * p["cache_write"] + new_in * p["cache_write"]
        else:
            total += (context + new_in) * p["in"]
        total += out * p["out"]
        context += new_in + out
    return total / 1_000_000

if __name__ == "__main__":
    scenarios = {
        "chat (10 turns)":         dict(turns=10, prefix=2_000,  new_in=300,   out=500),
        "agent (40 turns)":        dict(turns=40, prefix=15_000, new_in=3_000, out=600),
    }
    for model, p in PRICES.items():
        for name, s in scenarios.items():
            print(f"{model:18} {name:18} no cache ${session_cost(p, cached=False, **s):7.3f}   cached ${session_cost(p, cached=True, **s):7.3f}")

Running it gives these list price estimates:

ModelChat, 10 turns, no cacheChat, cachedAgent, 40 turns, no cacheAgent, cached
claude-sonnet-5.5$0.168$0.068$7.296$0.917
claude-opus-5.5$0.336$0.136$14.592$1.834
gpt-6.1-sol$0.168$0.068$7.296$0.917
gpt-6-luna$0.008$0.004$0.365$0.063

The chat scenario is a 2,000 token prefix, then 300 new input and 500 output tokens per turn. The agent scenario is a 15,000 token prefix, then 3,000 new input and 600 output tokens per turn, which is a plausible shape for tool results such as file reads, but it is a scenario, not a measurement.

What the numbers show

In the agent scenario the model writes 24,000 tokens, and the new content you add is 135,000 tokens including the prefix. But the uncached input billed across 40 turns is 3,528,000 tokens, because the context reaches 159,000 tokens by the end and is re-sent every turn.

Caching changes the shape. In the cached run most of those tokens bill at the cache read rate, which is one twentieth of the input price on the Sonnet, Opus and Sol rows and one tenth on Luna. That is why the agent session drops from $7.30 to $0.92 on Sonnet 5.5.

Doubling the turns makes the uncached bill grow faster than double:

TurnsFinal contextUncachedCached
1051,000$0.74$0.20
2087,000$2.21$0.40
40159,000$7.30$0.92
80303,000$26.11$2.37

The 80 turn row passes OpenAI's 272,000 token threshold, above which its page lists higher long context rates, so it would cost more on gpt models than the table says. Anthropic's page says Claude 4.6 and later models, except Haiku 5.5, include the full 1M token context window at standard pricing.

Measure instead of guessing

Estimates are a starting point. Anthropic's pricing page points to a token counting endpoint for estimating a request in advance, and API responses report the usage for each call. Put your real first week of usage into the calculator in place of the scenarios.

Where the calculator is wrong

  • Cache writes expire. Anthropic's page lists a 5 minute and a 1 hour cache write price. If your turns are slower than the cache lifetime, you pay writes again.
  • Output tokens include any reasoning the model produces, which can be large. Count them from the usage fields.
  • Tool definitions can add thousands of tokens. Anthropic lists the extra system prompt tokens for tool use on its pricing page.
  • Subscription plans are not priced this way at all. Plan limits are message or time based, which is covered in our post on what a Claude plan buys.

Safeguard lets you bring your own model, and its MCP tool calls are unlimited, so the tool calls your agent makes against Safeguard do not add to the per call cost you are estimating here.

Quick check

Why does a 40 turn agent session bill so many more input tokens than it generates?

Each turn re-sends the whole context, which keeps growing. The input billed is the sum of the context over every turn.

What changes if you turn on caching?

Earlier tokens bill at the cache read rate, here one twentieth of the input rate on three of the four models and one tenth on the fourth, and only the new tokens bill at the cache write rate.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.