How to Summarize Old Conversation Turns in Python

Compress old conversation turns into a brief summary while keeping recent turns intact for LLM context management.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 14 views 0 copies

Python code

43 lines
Python 3.9+
from datetime import datetime, timedelta


def summarize_old_turns(conversation, max_turns=5):
    """Compress turns older than max_turns into a brief summary."""
    if len(conversation) <= max_turns:
        return conversation, ""

    old_turns = conversation[:-max_turns]
    recent_turns = conversation[-max_turns:]

    # Build a compact summary of old turns
    speaker_counts = {}
    for speaker, text in old_turns:
        speaker_counts[speaker] = speaker_counts.get(speaker, 0) + 1

    summary = "Earlier: " + ", ".join(
        f"{speaker} spoke {count} time{'s' if count > 1 else ''}"
        for speaker, count in speaker_counts.items()
    ) + "."

    return recent_turns, summary


if __name__ == "__main__":
    mock_conversation = [
        ("alice", "Hello!"),
        ("bob", "Hi alice"),
        ("alice", "How are you?"),
        ("bob", "Fine, thanks"),
        ("alice", "Great to hear"),
        ("bob", "What about the project?"),
        ("alice", "It's on track"),
        ("bob", "Perfect"),
    ]

    recent, summary = summarize_old_turns(mock_conversation, max_turns=3)

    print("Summary of old turns:")
    print(summary)
    print("\nKept recent turns:")
    for speaker, text in recent:
        print(f"{speaker}: {text}")

Output

stdout
Summary of old turns:
Earlier: alice spoke 3 times, bob spoke 2 times.

Kept recent turns:
alice: It's on track
bob: Perfect

How it works

The function separates old turns from recent turns using list slicing. It counts how many times each speaker contributed to the old turns and builds a compact summary string. This pattern mimics token-efficient context compression for LLM conversations. The function returns both the recent turns and the summary, which you can prepend to your prompt or messages array.

Common mistakes

  • Forgetting that slicing keeps the original list order — old turns must come before recent turns
  • Using the same list variable after slicing and accidentally mutating the original
  • Assuming the summary length is always bounded — speaker counts can grow with many speakers

Variations

  1. Use a summarizer LLM call to generate a natural-language summary instead of the count-based heuristic
  2. Store summaries as a rolling buffer so you only summarize once instead of repeatedly

Real-world use cases

  • Trimming long chat histories before sending them to an LLM API to stay within token limits and reduce cost.
  • Maintaining a rolling context window in a customer-support bot while preserving key facts from earlier messages.
  • Building multi-turn agent memory systems where older exchanges get condensed into a short state snapshot.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.