You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
A single API call is stateless — the model has no memory of previous interactions. To build conversational agents that remember context, you need explicit memory management. This lesson covers conversation history, windowing strategies, summarisation, and persistent memory stores.
LLMs are stateless. Every API call is independent. The model only "remembers" what you include in the messages array.
Call 1: "My name is Alice." → "Hello, Alice!"
Call 2: "What's my name?" → "I don't know your name."
(no context from Call 1)
To maintain conversation continuity, you must manage the message history yourself.
The simplest approach: keep all messages in a list.
from openai import OpenAI
client = OpenAI()
class ChatBot:
def __init__(self, system_prompt: str):
self.messages = [
{"role": "system", "content": system_prompt}
]
def chat(self, user_input: str) -> str:
self.messages.append({"role": "user", "content": user_input})
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=self.messages,
)
assistant_msg = response.choices[0].message.content
self.messages.append({"role": "assistant", "content": assistant_msg})
return assistant_msg
bot = ChatBot("You are a helpful maths tutor.")
print(bot.chat("What is 2 + 2?")) # "4"
print(bot.chat("Now multiply by 10.")) # "40" — remembers previous context
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.