download

The conversation no longer fits the context window. What do you cut?

System design~12 minReviewed 1 Nov 2026Free · No sign-in

Question

A shopping assistant uses a model with a context window of 32,000 tokens. The window counts everything the model reads and everything it writes. For the next turn, the app wants to send:

parttokens
system prompt1,500
tool definitions2,500
the user's new message200
room for the answer4,000
8 retrieved product chunks, 600 each4,800
chat history: 60 turns, about 400 each24,000

That is 37,000 tokens, 5,000 over the limit. What do you cut, in what order, and what must stay? Where in the prompt do you put the most important parts?

Show hints (3)
  1. "WHAT STAYS"

    Without the system prompt or the tools, the bot breaks. And the window includes the answer. What happens if the input leaves no room for it?

  2. "OLD AND WEAK FIRST"

    Of the 8 chunks, the lowest-scoring ones help least. Of the 60 turns, the oldest matter least, but they may hold a fact like the user's shoe size.

  3. "DO THE MATH"

    Keep the 5 best chunks. Replace the oldest turns with a summary of about 600 tokens. How many of the newest 400-token turns still fit?

Show checked explanation

Never cut the system prompt, the tools, the new message or the room for the answer. Keep the 5 best chunks of 8. Replace the oldest turns with a 600-token summary that keeps key facts, and keep the newest 50 turns word for word. That plans 31,800 of 32,000 tokens. Put instructions first and the question last.

"SOLUTION · PYTHON"
WINDOW = 32_000
fixed = {"system": 1_500, "tools": 2_500, "new message": 200, "answer": 4_000}
chunks = [600] * 8  # retrieved, best first
turns = [400] * 60  # chat history, oldest first
SUMMARY = 600  # replaces the turns we drop

left = WINDOW - sum(fixed.values())
kept_chunks = chunks[:5]
left -= sum(kept_chunks) + SUMMARY
recent = []
for size in reversed(turns):  # newest first
    if size > left:
        break
    recent.append(size)
    left -= size
print("needed without cuts:", sum(fixed.values()) + sum(chunks) + sum(turns))
print("chunks kept:", len(kept_chunks), "of", len(chunks))
print("turns kept:", len(recent), "of", len(turns), "+ summary")
print("tokens planned:", WINDOW - left, "of", WINDOW)
"OUTPUT"
needed without cuts: 37000
chunks kept: 5 of 8
turns kept: 50 of 60 + summary
tokens planned: 31800 of 32000

Code run on CPython 3.12 against the example above. Checked by an editor other than the author.

Walkthrough

The window holds the input and the output together. If the input fills it, the answer has no room: it gets cut off, or the request fails. So reserve room for the answer first, then cut by value.

partbeforeafterrule
system prompt, tools, new message4,2004,200never cut
room for the answer4,0004,000never cut
retrieved chunks4,8003,000keep the 5 best
summary of old turns0600new
recent turns24,00020,000newest 50 of 60
total37,00031,800

Weak chunks go first. The three lowest-scoring chunks are the least likely to help. Extra chunks can also distract the model.

Old turns become a summary. The newest turns explain what "that one" or "the cheaper one" means, so keep them word for word. Summarize the oldest 10 turns. Keep facts the user stated, like a shoe size or an order number, in a short list of key facts, so the summary can't lose them.

Order matters too. Models use information at the start and the end of a long context better than in the middle ("Lost in the Middle", Liu et al., TACL 2024). Put the instructions first. Put the best chunk near the end, right before the user's question. Don't leave a key fact at turn 30 of 60.

Leave a small margin. This plan keeps 200 tokens spare, because the counts are only estimates until the model's tokenizer counts them.

Common mistakes

  • Forgetting the answer. If the input fills the window, the reply is cut off mid-sentence, or the API rejects the request.
  • Dropping the oldest messages blindly. The first messages often hold the user's goal or a key fact. Keep those, and drop old small talk instead.
  • Counting words or characters instead of tokens. Code, numbers and non-English text often take more tokens than you'd guess. Count with the model's tokenizer.
  • Summarizing the whole history on every turn. That costs an extra model call each time. Summarize when you hit the limit, then extend the summary.

Follow-up questions

  • "Why not just use a model with a bigger window?" It helps, but it doesn't remove the problem. You pay for every input token on every turn, and a longer input delays the first word of the answer. Models also miss facts in the middle of long contexts. A good budget beats a full window.
  • "The user asks about something from turn 5, which is now only in the summary." Store the full history outside the prompt, in a database. When the new message refers to something old, search past turns the way you search documents, and add the best matches. That is retrieval over the chat history.

What a strong answer covers

Reserves room for the answer and never cuts the system prompt, tools or new message. Cuts weak chunks first, then replaces old turns with a summary that keeps key facts. Puts key content at the start and end, because models use the middle worse.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design with AI Interview Guide (Rohit Bhardwaj, 2026); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.