Sub topic 6
Why long conversations get expensive
This chapter assumes you can read basic Python and know what a token and a context window are. It also assumes you know that providers charge per input and output token. Here you will see why the cost of a chat grows faster than its length.
The model has no memory between calls
A chat API is stateless. The model does not remember your earlier messages. To keep a conversation going, your application sends the whole conversation again on every call: the system prompt, every earlier user message, and every earlier reply, followed by the new message.
Each call is therefore billed for all of that history as input tokens. The model's own earlier replies count as input too, even though you already paid for them once as output.
Simulating the growth
The function below tracks the history and adds up the input tokens billed across a conversation.
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)) # 24000The setup is a 500-token system prompt, 100-token user messages, and 300-token replies. Over ten turns, the user and the model write only 4,000 new tokens (10 × 100 + 10 × 300). The bill covers 24,000 input tokens, six times as many.
On turn k, the input is 200 + 400k tokens. The first call sends 600 tokens. The tenth sends 4,200.
Quadratic total cost
Each turn adds a fixed amount to the history, so the per-turn input grows linearly. The total is the sum of those per-turn amounts, and that sum grows roughly with the square of the number of turns.
| Turns | Total input tokens | Input cost at a placeholder $3 per million |
|---|---|---|
| 5 | 7,000 | $0.021 |
| 10 | 24,000 | $0.072 |
| 20 | 88,000 | $0.264 |
Doubling the turns from 10 to 20 multiplies the input by about 3.7. A long session costs far more than the same number of short ones. Two ten-turn chats use 48,000 input tokens, while one twenty-turn chat uses 88,000.
The prices in the table are made up. Only the shape of the growth matters.
Two limits grow with the history
The history hits two separate limits:
- Cost. Every resent token is billed again.
- The context window. The history plus the requested output must fit in the window. A conversation that grows without bound will eventually fail, usually with an error from the API.
Latency also tends to rise with input size, because the model has more text to process before it starts replying.
Common mistakes
- Estimating cost from the latest message only. A 100-token question on turn 30 may carry 12,000 tokens of history.
- Ignoring the system prompt. It is resent on every turn, so a 2,000-token prompt over 50 turns adds 100,000 input tokens.
- Forgetting tool output. Large documents or tool results pasted into the conversation stay in the history and are resent on every later turn.
- Assuming replies are free after they are generated. A 300-token reply is output once, then input on every later turn.
Controlling the growth
The next chapters cover these in detail. In short:
- Drop or summarize old messages once they are no longer needed.
- Keep only the most recent messages that fit a token budget.
- Cache the stable prefix, such as the system prompt, if your provider supports it.
- Retrieve relevant snippets instead of keeping whole documents in the history.
Here is a simple trimming function. It walks backwards from the newest message and keeps messages until the budget runs out.
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))Trimming caps the per-turn input at about the budget, so total cost becomes roughly linear in the number of turns. The trade-off is that the model no longer sees the dropped messages. Anything it needs from them must be kept some other way.
Exercise
A chat has a 1,000-token system prompt. Each user message is 200 tokens and each reply is 400 tokens. Using the logic of totalinputtokens, how many input tokens does the third call send, and how many input tokens are billed in total over three turns?
Show answer
Turn 1 sends 1,000 + 200 = 1,200 tokens. Turn 2 sends 1,200 + 400 + 200 = 1,800 tokens. Turn 3 sends 1,800 + 400 + 200 = 2,400 tokens. The third call sends 2,400 input tokens. The total over three turns is 1,200 + 1,800 + 2,400 = 5,400 input tokens.
Exercise
Why does trim_history make total cost grow roughly linearly instead of quadratically, and what is the cost of doing so?
Show answer
It limits the history sent on each call to about the budget, so each turn's input stops growing. Each turn then costs about the same, and the total grows in proportion to the number of turns. The cost is that the model loses the dropped messages, so important facts must be kept some other way, for example by summarizing them.