Skip to main content

A lightweight library to add memory to stateless LLM APIs

Project description

MemoChain

MemoChain is a lightweight Python library for adding memory to stateless LLM APIs. It allows developers to easily store and retrieve conversation history across sessions with minimal setup.

Key features:

  • Supports any LLM API (OpenAI, DeepSeek, Claude, Mistral, etc.)
  • Built-in Groq integration
  • Local JSON-based storage (no external DB or backend)
  • Session-based memory tracking
  • Clean, modular design for easy use in any project

Installation

pip install memochain

Quickstart

Use with your own LLM (e.g. OpenAI)

from memochain.memory_store import load_context, append_to_history
import openai

openai.api_key = "your-openai-api-key"
session_id = "demo-session"

append_to_history(session_id, "user", "Tell me a joke.")
history = load_context(session_id)

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=history
)

assistant_msg = response["choices"][0]["message"]["content"]
append_to_history(session_id, "assistant", assistant_msg)

print("Assistant:", assistant_msg)

Use with built-in Groq support

from memochain.groq_client import GroqLLMClient
from memochain.memory_store import append_to_history, get_session_history

session_id = "groq-session"
client = GroqLLMClient()

append_to_history(session_id, "user", "Who won the World Cup in 2022?")
history = get_session_history(session_id)
response = client.chat(history)

append_to_history(session_id, "assistant", response)
print("Assistant:", response)

How It Works

MemoChain stores all message history locally in:

  • .memochain_history.json — stores conversation history per session
  • .memochain_sessions.json — tracks active session IDs

This allows memory persistence across runs without a backend or database.


Example Scripts

See the examples/ folder:

  • custom_llm_openai_example.py — OpenAI API usage
  • groq_chat_example.py — Chat example using Groq integration

To test locally:

python examples/groq_chat_example.py

License

MIT


Author

Yash Thapliyal – GitHub

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memochain-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memochain-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file memochain-0.1.0.tar.gz.

File metadata

  • Download URL: memochain-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for memochain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3b721e7d7cf3df4db605a3c97dc314c03e719d39f81d211f7dc0e54ccbd46b5c
MD5 159ee628dbd6f1162efb6383d667d180
BLAKE2b-256 6c1538a46b68762458d7f7910803f27cefed78cd939f0a4984b06f1f1ae8b6d5

See more details on using hashes here.

File details

Details for the file memochain-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: memochain-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for memochain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a78eb2d1e426742d6d4b6a28a4aca05ae8e2f734cc0fd6c54c5aacd73f8e835
MD5 dda0e089ac9b3508d117129e7985f89b
BLAKE2b-256 7547573eb338f9096f0044a61edc2caf5bb2ad0311361d62163d9cda306385ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page