A chat model remembers nothing between requests. Each time the user sends a message, your app sends the whole conversation again, and every token of it counts against the model’s context window and your bill. Long chats eventually hit the limit, and something has to go. A token budget is a choice about what the model is allowed to forget. Here you’ll squeeze a real support chat into less and less room, and read what a real model answers each time.
Every message resends the conversation#
A request to a chat model carries a system prompt (the app’s standing instructions), the conversation so far, the new message, and room for the reply, all counted in tokens. The model’s context window is the most it can take in one request; the price is usually per token, in and out. Today’s models take hundreds of thousands of tokens, so one chat rarely overflows them, but long sessions, big documents and retrieved passages add up, and every token costs time and money on every single request.
Our chat is a made-up support conversation with Hearth, a smart-home company: 20 turns, then a final question that needs two facts the customer gave at the very start, their order number and their address. To make the squeeze visible, the budget below is a few hundred tokens rather than a few hundred thousand; the trade-offs are the same at any size.
Predict first
You drop the oldest turns until the chat fits. The customer gave their address in turn 3 of 20. Asked where the replacement will go, what does the model do?
Dropping turns fails quietly. Keep the last 14, 10, 6 or 4 turns and the answers read well but never mention the address; keep only 2 and the model says it will post the thermostat from Leeds. Summarising does much better here. The summary, about 90 tokens, keeps the name, the order number and the address, and every answer built on it sends the replacement to 14 Larch Road. Summaries lose things too: this one kept the address because our summary prompt asked for “names, order numbers, addresses and the problem”. (We capped the summaries at 90 tokens, which is why they stop mid-sentence.)
One more thing to notice: most answers promise a delivery time, from “2-3 working days” to “5-7 business days”, and nobody in the chat ever gave one. Trimming can’t fix that. It’s the model filling a gap, a different problem, covered in the hallucinations explainer.
Three ways to make it fit#
- Send everything works until it doesn’t. Past the limit the API refuses the request, or, worse, some frameworks cut the start off silently.
- Drop the oldest turns (a sliding window) is simple and cheap, and loses whatever was said early on, which in support chats is often the most important part: who, which order, what’s wrong.
- Summarise the oldest turns keeps the gist for a fraction of the tokens, but costs an extra model call each time you summarise, and the summary only keeps what the summariser thought mattered.
A fourth option often beats all three: keep the facts the conversation depends on (who the customer is, which order, which address) as data in your own app, and put them into the system prompt of every request. They cost a few dozen tokens and never get trimmed. Real apps often combine these: a fixed system prompt, a short block of known facts, a running summary, and the latest turns in full.
The bill grows faster than the chat#
Because each request resends everything before it, the tokens you pay for don’t grow in step with the conversation: the tenth message carries the nine before it, and the whole chat costs roughly the square of its length.
The customer’s last message alone carries 527 tokens, seven times the first, and the eleven requests add up to 3,433 input tokens for a chat whose text is about 480. Keep only the last six turns and the total falls to 1,983, 42% less, and the cost of each message stops growing. Real chats run far longer than eleven messages, so the gap widens: double the length of a chat and resending everything roughly quadruples its cost. Prompt caching softens this, because many providers charge much less for the repeated start of a prompt they’ve seen recently (see caching LLM calls).
In code#
Count tokens before you send, and trim from the oldest end while keeping the system prompt and the new question. This uses OpenAI’s open-source tokenizer library, free and local (pip install tiktoken):
import tiktoken
enc = tiktoken.get_encoding("o200k_base") # the tokenizer GPT-4o uses; count with your own model's tokenizer
count = lambda m: len(enc.encode(m["content"])) + 4 # a few extra tokens per message for the chat format (approximate)
def fit(system, history, question, budget, reserve_for_answer=200):
"""Keep the system prompt and the question, then as many recent turns as fit."""
room = budget - reserve_for_answer - count(system) - count(question)
kept = []
for m in reversed(history):
if count(m) > room:
break
kept.insert(0, m)
room -= count(m)
return [system, *kept, question], len(history) - len(kept)
system = {"role": "system", "content": "You are Hearth's support assistant. Be friendly and brief."}
history = [
{"role": "user", "content": "Hi, I'm Robin. Order HX-48213 arrived and the thermostat won't turn on."},
{"role": "assistant", "content": "Sorry to hear that, Robin. Is the ring on the front glowing?"},
{"role": "user", "content": "No glow at all. My address is 14 Larch Road, Bristol BS7 2QE if you need it."},
{"role": "assistant", "content": "Thanks. It sounds faulty, so I can send a replacement."},
{"role": "user", "content": "Great. Also, can I use the P1 plug for my 300 watt dehumidifier?"},
{"role": "assistant", "content": "Yes, 300 watts is fine for the P1, which handles up to 13 amps."},
]
question = {"role": "user", "content": "When will the replacement arrive, and where will you send it?"}
for budget in [400, 330, 300]:
messages, dropped = fit(system, history, question, budget)
has_address = any("Larch Road" in m["content"] for m in messages)
print(f"budget {budget}: {sum(map(count, messages))} tokens sent, {dropped} turns dropped, address kept: {has_address}")
# budget 400: 168 tokens sent, 0 turns dropped, address kept: True
# budget 330: 127 tokens sent, 2 turns dropped, address kept: True
# budget 300: 98 tokens sent, 3 turns dropped, address kept: FalseAt 300 tokens the third turn has to go, and the address goes with it. The code can tell you that; the model won’t. That’s the case for keeping facts like the address in your own app, where trimming can’t reach them.
What to remember
- Every request resends the conversation, so long chats push against the context window and the bill on every message.
- Dropping the oldest turns is cheap but forgets quietly: the model answered without the address and never said it was missing. A 90-token summary kept it.
- Budget on purpose: count tokens before sending, keep the system prompt and the newest turns, and keep facts the conversation depends on in a summary or in structured state.