How to Add Conversation Memory to a Chatbot

LLM chat is stateless, so a bot only remembers what you resend. Memory is two things: send the conversation history on every request, and cap it with a sliding window so long chats don't overflow context. Here's how.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
AI Engineerpythonflaskllm

Why the bot forgets

Each LLM call is independent - the model has no memory of prior messages unless you include them. If you only send the latest user message, the bot can't recall your name from a turn ago. The fix is to send the running history every time.

Send the full history each turn

Keep a list of messages per session and pass the whole thing:

conversations = {}   # session_id -> [ {role, content}, ... ]

def chat(session_id, user_message):
    history = conversations.setdefault(session_id, [])
    history.append({"role": "user", "content": user_message})

    messages = [{"role": "system", "content": SYSTEM_PROMPT}] + history
    reply = call_llm(messages)                       # full context sent

    history.append({"role": "assistant", "content": reply})
    return reply

Now the model sees the whole conversation and can refer back to earlier turns.

Cap it with a sliding window

History that grows forever has two problems: it eventually overflows the context window (errors) and it gets slower and more expensive every turn (you resend more tokens). Keep only the last N messages:

WINDOW = 20   # last 10 user/assistant pairs
if len(history) > WINDOW:
    history[:] = history[-WINDOW:]

The trade-offs

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free

What you'll practice

FAQ

How do I give a chatbot memory of the conversation?

Store the messages per session and send the full history (plus the system prompt) on every LLM call. The model is stateless, so it only 'remembers' what you resend each turn.

Why does a long chatbot conversation slow down or fail?

History that grows unbounded keeps increasing the tokens sent each turn, which raises latency and cost and eventually overflows the model's context window. Cap it with a sliding window of the last N messages.

How does a chatbot remember things beyond the context window?

Summarize older turns into a running summary, or store facts/messages in a vector database and retrieve the relevant ones per turn - so the bot recalls earlier information without resending the entire history.

How do you make a chatbot remember previous messages?

Because LLM chat is stateless, you resend the prior turns on every call - keep a list of messages and pass it each time. For long chats, use a sliding window of the last N turns or summarize older turns so you do not overflow the context.

Keep learning

Restore a Broken LLM API IntegrationAI/ML projectHarden an LLM Pipeline Against API FailuresAI/ML projectParse JSON From an LLM (Strip Markdown Fences)AI/ML projectAI/ML roadmapStep by step to hiredAI/ML interview questionsSTAR answersAll AI/ML projectsProjects hub

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free
Start this project - free