Sub topic 6
Why Long Conversations Get Expensive
This chapter assumes you can read basic Python and have called a chat API before. You should know what a token is, how a context window limits a request, and how input and output tokens are priced. The goal here is to predict how the bill grows as a conversation gets longer, and to see why that growth is faster than most people expect.
The model has no memory between calls
A chat API is stateless. Each request is independent, and the model only knows what is inside that request. When a chat app seems to "remember" the earlier turns, it is because the application sends those turns again.
A typical chat loop looks like this:
- The app stores a list of messages: the system prompt, then each user message and each assistant reply.
- When the user sends a new message, the app appends it to the list.
- The app sends the whole list to the API.
- The model's reply is appended to the list, and the loop repeats.
This means the input of call number N contains everything from calls 1 to N-1, plus the new message. You are billed for all of those input tokens on every call, not just the new ones.
Simulating the growth
The following function models a chat with a fixed system prompt, a fixed user message size, and a fixed reply size. It tracks the history length and adds it to the total on every call.
def total_input_tokens(system_tokens, user_tokens, reply_tokens, turns):
total = 0
history = system_tokens
for _ in range(turns):
history += user_tokens
total += history # the whole history is sent as input on every call
history += reply_tokens
return total
print(total_input_tokens(500, 100, 300, 10)) # 24000Ten turns with a 500-token system prompt, 100-token user messages, and 300-token replies bill 24,000 input tokens. The user and the model together wrote only 4,000 new tokens (10 × 100 plus 10 × 300). The other 20,000 tokens are the system prompt and earlier turns being sent again.
You can see where the number comes from by listing the input size for each turn:
| Turn | History sent as input | Running total |
|---|---|---|
| 1 | 600 | 600 |
| 2 | 1,000 | 1,600 |
| 3 | 1,400 | 3,000 |
| 4 | 1,800 | 4,800 |
| 5 | 2,200 | 7,000 |
| 6 | 2,600 | 9,600 |
| 7 | 3,000 | 12,600 |
| 8 | 3,400 | 16,000 |
| 9 | 3,800 | 19,800 |
| 10 | 4,200 | 24,000 |
Each turn adds 400 tokens to the history (100 from the user, 300 from the model), so the input grows by 400 every turn. The per-turn input is a straight line, and the running total is the sum of that line.
The growth is quadratic
Let s be the system prompt size and t the tokens added per turn (user message plus reply). Call this u for the user message, so turn k sends s + k·u + (k−1)·r tokens, where r is the reply size. Summing over n turns gives:
- A fixed part:
n · s, which grows linearly with the number of turns. - A history part that grows with
n², because each turn addstmore to every later call.
In short, the cost of the history portion is proportional to the square of the number of turns. Doubling the length of a conversation roughly quadruples the history cost, not doubles it.
You can check this with the function above:
for turns in (10, 20, 40):
print(turns, total_input_tokens(500, 100, 300, turns))This prints 24,000 for 10 turns, 88,000 for 20 turns, and 336,000 for 40 turns. Going from 10 to 20 turns multiplies the input by about 3.7. Going from 20 to 40 multiplies it by about 3.8. The ratio approaches 4 as the history part dominates the fixed system prompt part.
The 30-turn value follows from the same formula: 30 × 500 + 400 × (30 × 31 / 2) − 300 × 30 = 15,000 + 186,000 − 9,000 = 192,000. The first term is the system prompt resent each turn, the second is the sum of the 400-token additions, and the last corrects for the fact that the final reply of each turn is not yet in the history when that turn's input is sent.
What this does to the bill
Input and output are priced separately, and output is usually several times more expensive per token. That can make it look as if output dominates the cost. In a long conversation it often does not. Output tokens are paid for once. Input tokens are paid for again on every later call.
Using the placeholder prices from the previous chapters ($3 per million input tokens, $15 per million output tokens), compare the 10-turn conversation:
def estimate_cost(input_tokens, output_tokens,
input_price_per_million, output_price_per_million):
return (
input_tokens * input_price_per_million
+ output_tokens * output_price_per_million
) / 1_000_000
input_total = total_input_tokens(500, 100, 300, 10) # 24000
output_total = 10 * 300 # 3000
print(f"${estimate_cost(input_total, output_total, 3.0, 15.0):.4f}")The input part is 24,000 × 3 / 1,000,000 = $0.0720. The output part is 3,000 × 15 / 1,000,000 = $0.0450. The total is $0.1170. Input is already 62% of the bill after 10 turns. At 40 turns, input is 336,000 tokens ($1.008) against 12,000 output tokens ($0.18), so input is about 85% of the bill. These prices are made up for the example, so check your provider's current rates before relying on the numbers.
Two other costs scale the same way:
- Latency. More input tokens take longer to process, so later turns respond more slowly.
- Quality. Very long inputs can make it harder for the model to use details from the middle of the conversation, even when everything fits in the window.
The window is a second limit
Cost is not the only constraint. The history also has to fit in the context window together with the output you allow. A conversation that grows by 400 tokens a turn from a 500-token start reaches an 8,000-token window after about 19 turns:
def fits_in_window(input_tokens, max_output_tokens, context_window):
return input_tokens + max_output_tokens <= context_window
history = 500
turn = 0
while fits_in_window(history + 100, 300, 8000):
history += 400
turn += 1
print(turn) # 18The loop checks whether the next user message plus the maximum reply still fits, and stops when it does not. After 18 full turns the history is 7,700 tokens, and a 19th user message would push the request to 8,100 tokens, which is over the limit. What happens next depends on the provider and the app. The API may return an error, or the app may silently drop old messages. Silent dropping is worse, because the model loses context without anyone being told.
A large window delays this problem but does not remove the cost curve. A model with a window of one million tokens can hold a very long chat, but each call that sends 500,000 tokens of history is billed for 500,000 input tokens.
Common sources of extra growth
The simple model above assumes small, uniform messages. Real applications often grow faster for these reasons:
- Large system prompts. A 3,000-token system prompt is sent on every turn. Over 40 turns that is 120,000 tokens before any conversation happens.
- Pasted documents. A document the user pastes in turn 2 stays in the history for every later call, even if it is never mentioned again.
- Tool and function results. An agent that calls tools adds each tool's output to the history. Search results and file contents can be thousands of tokens each.
- Hidden output. Some models produce reasoning tokens that are billed as output. Whether they are kept in later history depends on the provider, so check the documentation and the usage data.
- Retries. If a call fails after sending the full history, a retry pays for the same input again.
Common mistakes
- Estimating cost from the latest message only. The cost of a turn is the whole history, not the new text.
- Multiplying one turn's cost by the number of turns. Early turns are cheap and late turns are expensive, so this gives the wrong answer in either direction. Sum the per-turn inputs instead.
- Ignoring the system prompt. It is small compared with the history in a long chat, but it dominates the cost of short chats and of many single-call uses.
- Assuming a bigger window fixes cost. A bigger window only moves the point where the request stops fitting.
- Trusting the simulation for real traffic. Message sizes vary. Use the usage data returned by the API for actual figures, and use the simulation to compare designs.
First look at the fix: trimming
The most direct way to flatten the curve is to stop sending old messages. The function below keeps the most recent messages that fit a token budget. It walks backwards from the newest message and stops as soon as one does not fit.
def trim_history(messages, count_tokens, budget):
kept = []
used = 0
for message in reversed(messages):
n = count_tokens(message)
if used + n > budget:
break
kept.append(message)
used += n
return list(reversed(kept))With a budget of 2,000 tokens, the history sent on each call stops growing once it reaches that size, so the total input becomes linear in the number of turns instead of quadratic. The trade-off is that the model no longer sees what was cut. Later chapters cover how to keep the system prompt outside the trimmed part, how to summarize old turns, and how prompt caching lowers the price of the part you do resend.
Exercise
A chat has a 1,000-token system prompt. Each turn, the user writes 200 tokens and the model replies with 600 tokens. Using totalinputtokens, how many input tokens are billed across 5 turns, and how many of them are new text written during the chat?
Show answer
Turn inputs are 1,200, 2,000, 2,800, 3,600 and 4,400 tokens, so totalinputtokens(1000, 200, 600, 5) returns 14,000. New text written during the chat is 5 × 200 + 5 × 600 = 4,000 tokens. The other 10,000 billed tokens are the system prompt and earlier turns being resent.
Exercise
Why does doubling the number of turns in a conversation more than double the input cost, and what change to the application makes the growth linear again?
Show answer
Every call resends all earlier messages, so turn k pays for roughly k turns of history and the total grows with the square of the number of turns. Limiting the history sent on each call to a fixed token budget, for example with trim_history, caps the per-call input, so the total grows linearly with the number of turns.