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.
Python code
43 linesfrom 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
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
- Use a summarizer LLM call to generate a natural-language summary instead of the count-based heuristic
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.