cd /news/ai-agents/add-long-term-memory-to-a-langchain-… · home › topics › ai-agents › article
[ARTICLE · art-177615] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Add long-term memory to a LangChain chatbot in Python (15 lines, no vector DB)

A developer published a 15-line Python pattern for adding long-term memory to a LangChain chatbot using the langchain-belcore package, without a vector database. The approach keeps conversation history in the app's own JSON store, sends the full history to the ChatBelcore model each turn, and persists a small emotion state returned in response_metadata that is passed back on the next call. The demo shows the bot recalling a daughter's Tuesday piano lessons after a Python restart, with a memory field reporting whether recent or long-term context was used.

recommendation

Store conversation history and the returned emotion state in your own database, then pass both back on each turn so the model can select long-term context without a vector store.

more in playbook: Designing agent systems
by read2 min views9 publishedNov 28, 2026

Most chat apps already store the conversation in their own database. The hard part is deciding which old messages to send to the model when the history gets long. Here is a small Python setup that keeps your history where it is and lets Belcore pick the turns that matter.

pip install langchain-belcore

export BELCORE_API_KEY=sk_...

ChatBelcore is a normal LangChain chat model. You send the full history you keep; it returns the reply plus a small emotion state you save and send back next turn.

import json, pathlib
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_belcore import ChatBelcore

DB = pathlib.Path("conversation.json")   # stand-in for your database
llm = ChatBelcore(model="openai/gpt-4o-mini")

def load():
    return json.loads(DB.read_text()) if DB.exists() else {"messages": [], "emotion": None}

def turn(text):
    state = load()
    state["messages"].append({"role": "user", "content": text})

    history = [SystemMessage("You are a friendly assistant.")] + [
        HumanMessage(m["content"]) if m["role"] == "user" else AIMessage(m["content"])
        for m in state["messages"]
    ]
    opts = {"emotion": state["emotion"]} if state["emotion"] else {}
    res = llm.invoke(history, belcore=opts)

    state["messages"].append({"role": "assistant", "content": res.content})
    state["emotion"] = res.response_metadata["belcore"]["emotion"]
    DB.write_text(json.dumps(state, indent=2))
    return res.content

Three things to notice:

response_metadata["belcore"]["emotion"]; pass it in on the next call.

turn("My daughter's name is Soo-ah and she starts piano lessons on Tuesdays.")
turn("Give me a tip for staying focused while working from home.")
print(turn("What day are my daughter's piano lessons?"))

Restart Python between the calls; the answer still works because the history lives in conversation.json, not in the process.

res.response_metadata["belcore"]["memory"] tells you what was used: "recent" means only the last messages were needed (short chats), "long-term" means context was selected by searching the whole history. In this three-turn demo you will see "recent"; the long-term path matters once the conversation is long.

Text only. No tool calling, no token-by-token streaming, and no temperature / max_tokens options yet. ainvoke works for async apps.

Drafted with AI assistance; code was run against the live API before publishing.

── more in #ai-agents 4 stories · sorted by recency
── more on @langchain 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/add-long-term-memory…] indexed:0 read:2min 2026-11-28 · —