By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every model call re-sends the conversation, so a support thread that runs for twenty turns pays for the first turn twenty times. The memory lesson introduced trimming as a policy; this lab measures the bill. You will build a deterministic 20-turn conversation, watch its estimated token count grow, and compare trimming with summarising against the same budget.
The fixture is deliberately boring: each turn is a short customer question and a short status reply, so every number has one cause. Real tickets grow faster because tool results - order summaries, retrieved policy passages, search results - are larger than prose. If a toy thread crosses the budget this easily, a real one crosses it sooner.
The snippet estimates tokens at four characters per token and prints the prompt size at turns 1, 5, 10, 15 and 20 against a 600-token budget. No provider call is involved; the same fixture runs anywhere.
"""Measure a 20-turn support conversation against a token budget.
Token counts use the rough estimate of 4 characters per token, so the
numbers are deterministic and need no provider or API key.
"""
SYSTEM = (
"You are Harbor Helper, a support agent for an online store. Answer from "
"the tools and retrieved policy, cite sources, and hand off writes for "
"approval."
)
BUDGET = 600
history = []
for turn in range(1, 21):
history.append(("human", f"Turn {turn}: any update on my order ORD-4521?"))
history.append(
(
"ai",
f"Turn {turn}: ORD-4521 is in transit; the carrier scans it "
"daily. Reply STOP to pause updates.",
)
)
def tokens(messages):
return sum(len(role) + len(text) for role, text in messages) // 4 + 1
print("turn | messages | est_tokens | fits")
for turn in (1, 5, 10, 15, 20):
kept = history[: 2 * turn]
size = tokens([("system", SYSTEM), *kept])
print(
f"{turn:>4} | {len(kept):>8} | {size:>10} | "
f"{'yes' if size <= BUDGET else 'no'}"
)
def trim_drop(messages, budget):
kept = []
for message in reversed(messages):
candidate = [message, *kept]
if tokens([("system", SYSTEM), *candidate]) > budget:
break
kept = candidate
return kept
def summarize_old(messages, keep=4):
old = messages[:-keep]
recent = messages[-keep:]
summary = (
f"Summary: {len(old)} earlier messages about ORD-4521, still in "
"transit."
)
return [("ai", summary), *recent]
full = [("system", SYSTEM), *history]
trimmed = [("system", SYSTEM), *trim_drop(history, BUDGET)]
summarised = [("system", SYSTEM), *summarize_old(history)]
for name, messages in (
("full", full),
("drop+trim", trimmed),
("summarize", summarised),
):
size = tokens(messages)
print(
f"{name:>10}: {len(messages):>2} messages, {size:>4} est_tokens, "
f"fits={size <= BUDGET}"
)The growth is close to linear: roughly 34 estimated tokens per turn in this fixture, and the thread crosses the budget around turn 17. The full history is 720 tokens, the trim keeps the newest 32 of 40 history messages for 585 tokens, and the summary collapses everything except the last two turns into one line for 124 tokens. All three keep the same system prompt.
Trimming and summarising answer different questions. Trimming asks which messages are still affordable; summarising asks which facts are still needed. The snippet above shows both against the same 40-message history. Choose per product, not per library default.
| Strategy | What stays in the prompt | What you lose or pay | Use when |
|---|---|---|---|
| Keep the full history | Every turn and tool result, verbatim | Headroom, money and eventually the context window | Very short sessions, or replaying a bug in a trace |
| Drop to a token budget | System prompt plus the newest messages that fit | Old problem statements and old tool results, unless pinned | Routine status threads where the newest turn is the whole question |
| Summarise old turns | System prompt, one summary and the recent turns | Detail the summary omits, plus one model call to write it | Long tickets where early facts - the item, the order, the request - still matter |
| Summarise tool output | The two or three fields the answer needs, not the raw payload | Raw data leaves the prompt, so keep it in the trace | Any agent that looks up orders or retrieves passages |
A budget has fixed costs and growing costs. The fixed part is paid on every single call whether or not the conversation is long; the growing part is the variable you actually control. The diagram walks the same 20 turns from headroom to pressure to a policy.
A token budget is easy to write and easy to get wrong. The policy question is not 'what fits?' but 'what must never be forgotten?'. For Harbor Helper, four things always survive a trim.
The fixture is only useful when it runs in CI. Turn this lab into four assertions and run them without a key.
Key takeaways
Every model call re-sends the conversation, so history growth is a recurring cost, not a one-off.
Estimate with a deterministic fixture for tests, but set the production budget from provider usage metadata and the real tokenizer.
Trimming keeps the newest affordable messages; summarising replaces old turns with the facts that still matter.
Pin the problem statement, identifiers, approvals and the system prompt; trim conversation, not authority or context.
Keep a 20-turn budget test in CI and re-check summary quality in the live evals.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.