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.

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

Python code

21 lines
Python 3.9+
from 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

stdout
['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

  1. Use a list with manual slicing: self.turns = self.turns[-self.k:] after each append
  2. 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

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.