
You Pay for the Whole Conversation Every Turn: How LLM Costs Add Up
- كاتب موثّق
محتويات المقال
The common misconception
A chat UI shows one new message per turn, so that feels like the unit of work. Pricing pages list a price per million tokens, which invites "message size × price". The first turns of a session are cheap, so early tests confirm the estimate. Chat products and SDK helpers manage history for you, and message threads (in beta) even let a request send only the new messages.
What actually happens
Every turn re-reads the conversation
What's in each request's bill
The system prompt, every time. Tool definitions, plus a tool-use system prompt the API adds when you send tools(286 tokens on Claude Opus 5.5 with automatic tool choice).Every earlier user and assistant message, including tool results, which are often the bulkiest part. Earlier thinking. Thinking is billed as output when it's generated, even when its text isn't returned to you, and on models that keep prior thinking in context, such as recent Opus models, it's billed as input on later turns too.
The arithmetic: linear per turn, quadratic per session
PRICE = {"in": 4.00, "write": 5.00, "read": 0.20, "out": 20.00} # $/MTokdef chat_cost(turns, system=3_000, ask=200, answer=400, cached=False): cost, cached_prefix = 0.0, 0 for turn in range(1, turns + 1): sent = system + turn * ask + (turn - 1) * answer # full history if cached: # earlier turns are cache reads, the new tail a write cost += cached_prefix * PRICE["read"] cost += (sent - cached_prefix) * PRICE["write"] cached_prefix = sent else: cost += sent * PRICE["in"] cost += answer * PRICE["out"] return cost / 1_000_000print(f"uncached ${chat_cost(30):.2f}") # uncached $1.67print(f"cached ${chat_cost(30, cached=True):.2f}") # cached $0.41Caching changes the price of the repeat, not its size
tools, then system, then messages) and reuses it when a later request starts with exactly the same content. With automatic caching, one top-level cache_control field moves the cache point to the end of each request as the chat grows, so every turn reads the earlier conversation from the cache and writes only the new part.
Read the real numbers from usage
usageinput_tokens counts only the tokens after the last cache breakpoint. Total input is cache_read_input_tokens + cache_creation_input_tokens + input_tokens. A meter that estimates from the new message misses almost all of it.
def ask(text): history.append({"role": "user", "content": text}) reply = client.messages.create( model="claude-opus-5-5", max_tokens=16000, system=SYSTEM, messages=history, ) history.append({"role": "assistant", "content": reply.content}) new_tokens = len(text) // 4 # only the new message: the bill disagrees meter.add_usd((new_tokens * 4.00 + reply.usage.output_tokens * 20.00) / 1e6) return replyask("Why does the CSV export fail with E413?")def ask(text): history.append({"role": "user", "content": text}) reply = client.messages.create( model="claude-opus-5-5", max_tokens=16000, system=SYSTEM, messages=history, cache_control={"type": "ephemeral"}, ) history.append({"role": "assistant", "content": reply.content}) u = reply.usage # all input = input + cache writes + cache reads meter.add_usd((u.input_tokens * 4.00 + (u.cache_creation_input_tokens or 0) * 5.00 + (u.cache_read_input_tokens or 0) * 0.20 + u.output_tokens * 20.00) / 1e6) return replyask("Why does the CSV export fail with E413?")How to apply it in your project
Log usage per turn Record all four usagefields with the conversation ID and turn number. Chart cost by turn, not just per day, and the long sessions show up.Turn on caching and keep the prefix stable Add a top-level cache_control, put fixed content first and volatile content last, and checkcache_read_input_tokensin your logs.Resend less Trim tool results to the fields the model needs and summarize or drop old turns. The Building with LLMs path returns to this in its context engineering article. Budget sessions, not messages Set a token or dollar ceiling per conversation and decide what happens when it's reached: summarize, start fresh or hand off. Move offline work to batches For jobs nobody waits on, the Batch API halves the price: $2 input and $10 output per million tokens on Claude Opus 5.5.
Key takeaways
Every request bills the full context again: system prompt, tool definitions, all earlier turns and their tool results. Per-turn cost grows linearly, so a long session costs far more than a per-message estimate. Thinking is billed as output, and on recent models earlier thinking is billed again as input. Prompt caching makes the repeated prefix much cheaper, but it can't reduce output or what the model has to read. Measure with the usagefields and count tokens before sending, then optimize what the numbers show.
التعليقات (0)