Skip to main content

memory-agent

View on GitHub
GitHub stars
GitHub forks

The library allows managing both persistence and memory for a LangGraph agent.

memory-agent uses Redis as the backend for short‑term memory and long‑term persistence and semantic search.

memory-agent


🔑 Key Features

  • Dual-layer memory system
    • Short-term memory with Redis → fast, volatile storage with TTL for active sessions.
    • Long-term persistence with Qdrant → semantic search, embeddings, and cross‑session retrieval.
  • Integration with LangGraph → stateful LLM agents with checkpoints and memory tools.
  • Multi-LLM
    • OpenAI (via AgentOpenAI)
    • Ollama (via AgentOllama) for local inference
  • Flexible embeddings
    • OpenAI embeddings (default)
    • Ollama embeddings (e.g., nomic-embed-text)
  • Automatic memory management
    • Summarization and reflection to compress context
  • Observability
    • Structured logging, compatible with Grafana/Loki
  • Easy installation & deployment

🧠 Memory vs 🗃️ Persistence

Function Database Why
Memory Redis Performance, TTL, fast session context
Persistence Redis Vector search, long‑term storage

📦 Installation

pip install memory-agent

For local use with Ollama or local embeddings:


▶️ Usage examples (repository root)

The examples show how to configure the agent, send messages (including streaming) and share memory between different agents.

1) demo.py — Quick start with Ollama + memory

What it does:

  1. Saves to context: "My name is Giuseppe. Remember that."
  2. Asks a factoid: "What is the capital of France?" (streaming)
  3. Retrieves from short‑term memory: "What is my name?" (streaming)

Essential snippet (simplified):

from memory_agent.agent.ollama import AgentOllama
from demo_config import thread_id, user_id, session_id, model_ollama, redis_config,     model_embedding_vs_config, model_embedding_config, qdrant_config, collection_config

agent = AgentOllama(
    thread_id=thread_id,
    user_id=user_id,
    session_id=session_id,
    model_config=model_ollama,
    redis_config=redis_config,
    qdrant_config=qdrant_config,
    collection_config=collection_config,
    embedding_store_config=model_embedding_vs_config,
    embedding_model_config=model_embedding_config,
)

# Non-streaming call
text = await agent.invoke("My name is Giuseppe. Remember that.")

# Streaming call
async for token in agent.invoke_stream("What is the capital of France?"):
    print(token, end="")

# Retrieve from context
async for token in agent.invoke_stream("What is my name?"):
    print(token, end="")

Run:

python demo.py

What to expect:

  • On the first request the agent stores the information (“Giuseppe”).
  • On the third request the agent should answer with the previously provided name.

2) demo_config.py — Centralized configuration

This file defines all parameters used by the examples:

  • Session identifiers:
    thread_id = "thread_demo"
    user_id = "user_demo"
    session_id = "session_demo"
    
  • LLM model (Ollama):
    model_ollama = {
        "model": "llama3.1",
        "model_provider": "ollama",
        "api_key": None,
        "base_url": "http://localhost:11434",
        "temperature": 0.5,
    }
    
  • Qdrant:
    qdrant_config = {
        "url": "http://localhost:6333",
    }
    
  • Embeddings (via Ollama):
    model_embedding_config = {
        "name": "nomic-embed-text",
        "url": "http://localhost:11434"
    }
    
  • Vector Store / Collection (example): COSINE distance with qdrant_client.http.models.Distance.COSINE.
  • Redis: connection/TTL parameters for short‑term memory.

Modify these values to point to your Redis/Qdrant/Ollama instances. Other examples import directly from demo_config.py.


3) demo_mem_shared.py — Shared memory between two agents (LangGraph)

This example shows how two distinct agents can share the same memory.
The idea is to create two AgentOllama instances (e.g., agent_1 and agent_2) that use the same backends (Redis + Qdrant) and the same relevant identifiers (e.g., collection, user, thread), so that what the first agent stores is available to the second.

Flow:

  1. agent_1 receives: "My name is Giuseppe. Remember that." and stores it.
  2. agent_2 receives: "What is my name?" and retrieves the answer from shared memory.

Essential snippet (simplified):

agent_1 = AgentOllama(... shared ...)
agent_2 = AgentOllama(... shared ...)

await agent_1.invoke("My name is Giuseppe. Remember that.")

# The other agent pulls from the same memory
answer = await agent_2.invoke("What is my name?")
print(answer)  # → "Your name is Giuseppe" (expected)

Run:

python demo_mem_shared.py

This pattern is useful when multiple services/workers collaborate on the same user or conversation, leveraging Redis for short‑term state and Qdrant for persistence/semantic search across sessions.


⚙️ Prerequisites

  • Redis running (used for short‑term memory)
  • Ollama running (LLM and optionally embeddings)
  • OpenAI API KEY to make request to OpenAI API
  • Correct variables/URLs in demo_config.py

Docker


🧪 Tips

  • For multi‑worker environments, ensure thread_id, user_id and session_id and collection keys are consistent across processes that need to share memory.
  • To separate memories of different agents, use distinct session/thread IDs or different collections in Qdrant.
  • Tune model temperature and pruning/summarization parameters to balance cost/quality/context.

🛠️ Troubleshooting

  • Doesn't retrieve memory → check Redis reachability and that IDs (thread/user/session) are consistent between requests.
  • Semantic search not effective → verify embeddings are enabled (e.g., nomic-embed-text) and that Qdrant has the correct collection.
  • Streaming prints nothing → ensure you iterate the invoke_stream(...) generator and do print(token, end="").

📄 License MIT

Metadata

Release files for memory-agent 2.0.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for memory-agent 2.0.18
File Size Uploaded Provenance Actions
memory_agent-2.0.18.tar.gz 41.3 kB Without provenance Details for memory_agent-2.0.18.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for memory-agent 2.0.18
File Interpreter ABI Platform Actions
memory_agent-2.0.18-py3-none-any.whl Python 3 none any Details for memory_agent-2.0.18-py3-none-any.whl

Total release size: 85.2 kB

Release files / memory_agent-2.0.18.tar.gz

Download URL memory_agent-2.0.18.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7f851ff5eb90d489fd3dde85ece55654546b2b4db0c7e5cdf99ef09223a4c7c5
BLAKE2b-256 checksum
How to use checksums
5a6f3f62febd55a7ba0ee72f3b613ccfd1a86825e9373109463240270dc64298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / memory_agent-2.0.18-py3-none-any.whl

Download URL memory_agent-2.0.18-py3-none-any.whl
Size 43.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bce1e4a4aff7412a9f6127368337edd7b290b1f15f4683aeaa51308fbc1b9c47
BLAKE2b-256 checksum
How to use checksums
6dbac50e006172fa664b7dbff7b227839e6f1b1013c403ba3e4ebee120a3fba0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Security for memory-agent 2.0.18

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.18 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page