{"slug": "add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db", "title": "Add long-term memory to a LangChain chatbot in Python (15 lines, no vector DB)", "summary": "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.", "body_md": "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.\n\n`pip install langchain-belcore`\n\n```\nexport BELCORE_API_KEY=sk_...\n```\n\n`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.\n\n``` python\nimport json, pathlib\nfrom langchain_core.messages import AIMessage, HumanMessage, SystemMessage\nfrom langchain_belcore import ChatBelcore\n\nDB = pathlib.Path(\"conversation.json\")   # stand-in for your database\nllm = ChatBelcore(model=\"openai/gpt-4o-mini\")\n\ndef load():\n    return json.loads(DB.read_text()) if DB.exists() else {\"messages\": [], \"emotion\": None}\n\ndef turn(text):\n    state = load()\n    state[\"messages\"].append({\"role\": \"user\", \"content\": text})\n\n    history = [SystemMessage(\"You are a friendly assistant.\")] + [\n        HumanMessage(m[\"content\"]) if m[\"role\"] == \"user\" else AIMessage(m[\"content\"])\n        for m in state[\"messages\"]\n    ]\n    opts = {\"emotion\": state[\"emotion\"]} if state[\"emotion\"] else {}\n    res = llm.invoke(history, belcore=opts)\n\n    state[\"messages\"].append({\"role\": \"assistant\", \"content\": res.content})\n    state[\"emotion\"] = res.response_metadata[\"belcore\"][\"emotion\"]\n    DB.write_text(json.dumps(state, indent=2))\n    return res.content\n```\n\nThree things to notice:\n\n`response_metadata[\"belcore\"][\"emotion\"]`; pass it in on the next call.\n\n```\nturn(\"My daughter's name is Soo-ah and she starts piano lessons on Tuesdays.\")\nturn(\"Give me a tip for staying focused while working from home.\")\nprint(turn(\"What day are my daughter's piano lessons?\"))\n# Your daughter's piano lessons are on Tuesdays.\n```\n\nRestart Python between the calls; the answer still works because the history lives in `conversation.json`, not in the process.\n\n`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.\n\nText only. No tool calling, no token-by-token streaming, and no `temperature` / `max_tokens` options yet. `ainvoke` works for async apps.\n\n*Drafted with AI assistance; code was run against the live API before publishing.*", "url": "https://wpnews.pro/news/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db", "canonical_source": "https://dev.to/belcore/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db-i3i", "published_at": "2026-11-28 06:34:48+00:00", "updated_at": "2026-11-28 06:43:29.501924+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "ai-tools", "developer-tools"], "entities": ["LangChain", "Belcore", "ChatBelcore", "Python", "GPT-4o mini"], "also_reported_by": [], "takeaway": "Persisting a small emotion state from response_metadata and replaying it alongside full history lets a LangChain chat model recall facts across process restarts without a vector database.", "recommendation": {"advice": "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.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db", "markdown": "https://wpnews.pro/news/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db.md", "text": "https://wpnews.pro/news/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db.txt", "jsonld": "https://wpnews.pro/news/add-long-term-memory-to-a-langchain-chatbot-in-python-15-lines-no-vector-db.jsonld"}}