How to Keep Last K Turns in a Memory Buffer in Python
A TurnBuffer class using deque with maxlen to keep only the most recent k conversation turns in memory for LLM context.
Python code
21 linesfrom collections import deque
class TurnBuffer:
def __init__(self, k):
self.k = k
self.turns = deque(maxlen=k)
def add(self, turn):
self.turns.append(turn)
def last_k(self):
return list(self.turns)
if __name__ == "__main__":
buffer = TurnBuffer(3)
buffer.add("turn 1")
buffer.add("turn 2")
buffer.add("turn 3")
buffer.add("turn 4")
print(buffer.last_k())
Output
['turn 2', 'turn 3', 'turn 4']
How it works
The deque(maxlen=k) automatically discards the oldest item when a new one is appended, maintaining exactly the last k turns. The add method wraps the append operation, and last_k converts the deque to a list for a clean snapshot. This makes it O(1) for both adding and accessing the recent window, which is ideal for chat context trimming. The buffer ensures you never exceed memory or send an oversized prompt to an LLM API.
Common mistakes
- Using a plain list and forgetting to pop the oldest item, causing unbounded growth
- Assuming the deque is indexable directly without converting to a list for output
- Setting maxlen to a non-positive integer, which raises a ValueError
- Not handling the case where k is larger than the number of turns added
Variations
- Use a list with manual slicing: self.turns = self.turns[-self.k:] after each append
- Use a numpy array or a queue.Queue with explicit get() for thread-safe access
Real-world use cases
- Maintaining a rolling window of user messages to keep LLM prompts within token limits.
- Storing recent chat history in a chatbot service for context-aware follow-up responses.
- Capping the conversation state for a multi-turn agent that retries with truncated context.
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.