Skip to main content

Framework-agnostic memory infrastructure for AI agents

Project description

memharness

Framework-agnostic memory infrastructure for AI agents

PyPI Python 3.13+ License: MIT CI

memharness provides the memory layer for AI agents — persistent, searchable, typed memory with lifecycle management. Works with any agent framework.

📖 Documentation · 📦 PyPI · 🐛 Issues

Install

pip install memharness

# With PostgreSQL
pip install memharness[postgres]

# With HuggingFace embeddings
pip install memharness[embeddings]

Quick Start

from memharness import MemoryHarness

async with MemoryHarness("sqlite:///memory.db") as harness:
    # Store memories
    await harness.add_conversational("thread-1", "user", "I work at SAP")
    await harness.add_knowledge("Python 3.13 has free-threading", source="docs")
    await harness.add_entity("Alice", "PERSON", "Engineer at Acme Corp")

    # Search semantically
    results = await harness.search_knowledge("concurrent programming")

    # Assemble context for any LLM
    from memharness.agents import ContextAssemblyAgent
    ctx = ContextAssemblyAgent(harness)
    context = await ctx.assemble("Tell me about Python", thread_id="thread-1")
    messages = context.to_messages()  # list[BaseMessage] for LangChain

Memory Types

8 types covering the full agent memory taxonomy:

Type Storage What it stores
Conversational SQL Chat history per thread
Knowledge Base Vector Facts, documents, reference material
Workflow Vector Reusable multi-step task playbooks
Toolbox Vector Tool definitions (semantic retrieval)
Entity Vector People, organizations, systems
Summary Vector Compressed older conversations
Tool Log SQL Tool execution audit trail
Persona Vector Agent identity and style

7 Self-Awareness Tools

Give any agent the ability to manage its own memory:

from memharness.tools import get_memory_tools

tools = get_memory_tools(harness)  # Returns 7 LangChain BaseTool instances
Tool What the agent can do
memory_search Search across all memory types
memory_read Read a specific memory by ID
memory_write Write to any memory type
expand_summary Expand compacted summary to full content
summarize_conversation Compress a long conversation thread
assemble_context Full context assembly (BEFORE-loop)
toolbox_search Discover available tools

Embedded Agents

4 meta-agents that manage memory autonomously:

Agent Purpose
ContextAssemblyAgent Assembles optimal context before each LLM call
SummarizerAgent Compresses long conversations (heuristic or LLM)
EntityExtractorAgent Extracts entities from conversations (regex or LLM)
ConsolidatorAgent Merges duplicate entities

Fast Path / Slow Path

from memharness.core.fast_path import FastPath
from memharness.core.slow_path import SlowPath

# Fast path: user-facing (low latency)
fast = FastPath(harness)
ctx = await fast.process_user_message("thread-1", "How do I deploy?")
# → saves message + assembles context (no extraction, no summarization)

await fast.process_assistant_response("thread-1", response)

# Slow path: background workers (entity extraction, summarization, consolidation)
slow = SlowPath(harness)
results = await slow.run_all()

Summarization (Course-Aligned)

After summarization, context loads summary + recent messages only — not all messages:

Before: [msg1, msg2, ... msg50]           ← 50 messages in context
After:  [Summary of msg1-40] + [msg41-50] ← 1 summary + 10 recent

Configurable via max_tokens (default 4000) and summarize_threshold (default 80%).

Backends

Backend Best for Setup
SQLite Development, testing MemoryHarness("sqlite:///memory.db")
PostgreSQL + pgvector Production MemoryHarness("postgresql://...")
In-memory Unit tests MemoryHarness("memory://")

Docker

# PostgreSQL + pgvector
docker compose up -d

# With background workers (entity extraction, summarization, consolidation)
docker compose -f docker-compose.yml -f docker-compose.workers.yml up -d

Workers run SlowPath.run_all() every 5 minutes (configurable via WORKER_INTERVAL).

Use with LangChain

from langchain.agents import create_agent
from memharness.tools import get_memory_tools

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=get_memory_tools(harness),
)

See the full LangChain usage guide with middleware examples.

Configuration

harness = MemoryHarness(
    backend="sqlite:///memory.db",
    embedding_fn=my_embedding_function,  # default: hash-based
)

# Or with HuggingFace embeddings
from memharness.core.embedding import create_huggingface_embedding_fn
harness = MemoryHarness(
    backend="sqlite:///memory.db",
    embedding_fn=create_huggingface_embedding_fn("all-MiniLM-L6-v2"),
)

Documentation

Full docs at ayushsonuu.github.io/memharness

Contributing

Contributions welcome! See CONTRIBUTING.md.

License

MIT — see LICENSE.

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

memharness-1.0.1.tar.gz (104.6 kB view details)

Uploaded Source

Built Distribution

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

memharness-1.0.1-py3-none-any.whl (104.5 kB view details)

Uploaded Python 3

File details

Details for the file memharness-1.0.1.tar.gz.

File metadata

  • Download URL: memharness-1.0.1.tar.gz
  • Upload date:
  • Size: 104.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for memharness-1.0.1.tar.gz
Algorithm Hash digest
SHA256 aa3d5dc3bce04e7ee3384aa3ada21c2e583f644614f529989ea804b1f6a87d02
MD5 d9c87050e39b0b54b5b3efe5d0546f53
BLAKE2b-256 5e694b9579ab6613094071b233eb38f7e128fb80fec132a8f10fcaeab83cb814

See more details on using hashes here.

File details

Details for the file memharness-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: memharness-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 104.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for memharness-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d0520a1f4a75c35ae4ecb8936c72b81b4e01fd0a0d4a37b159e01cfd5964d4
MD5 c4712806543dc8c9e913360ed43a0d4d
BLAKE2b-256 85446dd603eeb6fa6f7bf66004af4f35b05bb3bb924f8affde6628b6d555da87

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