TECHNOLOGIESTieInn Technologies

Multi-turn chat: history, context limits and summarising

LLM Fundamentals & Prompting · 10 questions

Short notes

Key idea

A chat is a list of turns that grows with every message, and the whole list is sent on each call. As it grows it costs more and can overflow the context window, so long chats need trimming or summarising.

Syntax

history = []
def ask(text):
    history.append(types.Content(role="user", parts=[types.Part(text=text)]))
    r = client.models.generate_content(model=MODEL, contents=history, config=config)
    history.append(types.Content(role="model", parts=[types.Part(text=r.text)]))
    return r.text

Strategies when history gets long:

  • Keep the last N turns.
  • Summarise older turns into one short note and keep it at the top.
  • Store key facts (name, order number) separately and add them each call.

Example

After 40 turns a support chat is 30,000 tokens. Replace turns 1–30 with a 150-word summary ("customer Asha, order 4471, damaged item, refund agreed"), keep the last 10 turns: the bot still remembers what matters, at a fraction of the cost.

Traps

  • Forgetting to append the model's reply makes the next turn confusing.
  • Cost grows with every turn because the history is resent.
  • Summaries lose details; keep IDs and amounts exactly.
  • Very long contexts get less attention in the middle; put key facts near the end.

Want the full lesson with step-by-step examples, a project and a certificate? LLM Fundamentals & Prompting course, ₹180 →

Questions

Open in Code Lab5 free questions
5 questions

    Multi-turn chat: history, context limits and summarising

  1. Question 1easy

    Who remembers the earlier turns of a chat with an LLM?

  2. Multi-turn chat: history, context limits and summarising

  3. Question 2medium

    Every message is 40 tokens. On the 5th user turn, how many tokens does the call send (all earlier messages plus the new one)?

    turn = 5
    print((2 * turn - 1) * 40)
  4. Multi-turn chat: history, context limits and summarising

  5. Question 3medium

    What does this print?

    history = [
        {"role": "user", "content": "Hi, I'm Ravi"},
        {"role": "model", "content": "Hello Ravi"},
        {"role": "user", "content": "Track order 9"},
        {"role": "model", "content": "It shipped"},
        {"role": "user", "content": "Thanks"},
        {"role": "model", "content": "Welcome"},
    ]
    keep = history[-4:]
    print(keep[0]["content"])
  6. Multi-turn chat: history, context limits and summarising

  7. Question 4hard

    Write trim(messages, budget): walk from the newest message backwards, keeping messages while the total of count_tokens(m["content"]) stays within budget; then drop messages from the front until the first one has role user. Store trim(history, 12) in result.

    Store your answer in result.

  8. Multi-turn chat: history, context limits and summarising

  9. Question 5medium

    You replace old turns with a summary. What should the summary instruction stress?

5 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.