Skip to content

§ Tutorial

Build a chatbot that remembers. With the Python SDK.

A minimal, framework-free chatbot that recalls relevant context before replying and stores new facts after each turn — persistent memory in three calls.

SDK

octamem (Python)

Model

any (OpenAI shown)

Est. time

~10 min

The recipe for a chatbot with memory is the same regardless of stack: recall before you answer, store after you answer. This tutorial builds it from scratch with the OctaMem Python SDK and any chat model — no vector database or embeddings to manage.

Prerequisites

  • Python 3.9+ and an OpenAI API key (or any chat model).
  • A free OctaMem API key from platform.octamem.com.

Step 1: Install

terminal
pip install octamem openai

Set OCTAMEM_API_KEY and OPENAI_API_KEY in your environment.

Step 2: Recall, answer, store

Each turn does three things: get relevant memory, call the model with that context, then add the new exchange.

chatbot.py
import os
from octamem import OctaMem
from openai import OpenAI

memory = OctaMem()          # reads OCTAMEM_API_KEY
llm = OpenAI()              # reads OPENAI_API_KEY

TOPIC = "chat with user #1001"   # scopes memory to this conversation/user

def reply(user_message: str) -> str:
    # 1. Recall relevant memory before answering.
    recalled = memory.get(query=user_message, previous_context=TOPIC)

    # 2. Build the prompt with recalled context.
    system = (
        "You are a helpful assistant. Use the following remembered context "
        f"if relevant:\n{recalled}"
    )
    completion = llm.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": system},
            {"role": "user", "content": user_message},
        ],
    )
    answer = completion.choices[0].message.content

    # 3. Store the exchange so it can be recalled next time.
    memory.add(
        content=f"User said: {user_message}\nAssistant replied: {answer}",
        previous_context=TOPIC,
    )
    return answer

if __name__ == "__main__":
    print(reply("My name is Priya and I prefer email over calls."))
    # ...later, even in a new process:
    print(reply("How do I prefer to be contacted?"))

Where to go next

FAQ

How do I add memory to a chatbot?

Before generating each reply, search your memory store for context relevant to the user's message and include it in the prompt. After replying, store the new exchange. With OctaMem this is two calls — client.get(query=...) to recall and client.add(content=...) to store — so the chatbot remembers across sessions without you managing a vector database.

Why not just keep the whole conversation in the prompt?

Stuffing the full history into every prompt burns tokens, hits the context-window limit, and is lost when the session ends. A persistent memory layer stores only what matters, retrieves it on demand by relevance, and survives across sessions and processes.

What does previous_context do?

previous_context scopes a memory to a conversation, user, or topic. Using a stable value like a user ID keeps each user's memory separate, so recall returns only that user's relevant history.