| title | Memory |
|---|---|
| description | Give your agents persistent memory across conversations. |
| icon | bookmark |
While threads maintain conversation history within a single interaction, Marvin's memory feature allows agents to store and recall information across multiple conversations. This is useful for:
- Remembering user preferences across sessions
- Building up knowledge bases over time
- Sharing information between different agents
- Maintaining context across multiple interactions
- Creating more personalized user experiences
Marvin memories are implemented as vector stores that allow agents to store and retrieve information using natural language. Each memory has a unique key that identifies it and instructions that guide how it should be used.
To create a memory module, you need to provide a key and instructions:
import marvin
# Create a memory for user preferences
preferences = marvin.Memory(
key="user_preferences",
instructions="Store and retrieve user preferences and settings."
)The key uniquely identifies a memory module and must be consistent across uses to access the same stored information. Choose descriptive, unique keys for different types of information:
# Different memories for different purposes
user_prefs = marvin.Memory(key="user_preferences")
knowledge_base = marvin.Memory(key="documentation")
project_info = marvin.Memory(key="project_details")The instructions tell agents how to use the memory - what to store, when to access it, and how to format information:
weather_memory = marvin.Memory(
key="weather",
instructions="""
Store daily weather information including:
- Temperature
- Conditions
- Location
Format: 'Location: temp, conditions'
"""
)Attach memories to tasks to provide context:
import marvin
# Create a memory module
weather = marvin.Memory(key="weather")
# Use it in a task
task = marvin.Task(
instructions="What was yesterday's weather?",
memories=[weather]
)Give agents persistent memory across all their tasks:
import marvin
# Create an agent with memory
agent = marvin.Agent(
name="WeatherBot",
memories=[weather]
)
# The agent can access weather memory in any task
marvin.run("Record today's weather", agents=[agent])
marvin.run("Compare to yesterday's weather", agents=[agent])Here's a complete example of using memory to track weather information:
import marvin
# Create a memory module
weather = marvin.Memory(
key="weather",
instructions="Store and retrieve daily weather information."
)
# Record today's weather
marvin.run(
"Record that it's 72°F and sunny today",
memories=[weather]
)
# Later, retrieve the information
result = marvin.run(
"What was the weather like today?",
memories=[weather]
)
print(result)Today's weather was 72°F and sunny.
Marvin supports several vector store backends for memory storage:
ChromaDB is the default provider, offering a simple, file-based vector store:
```bash pip pip install "marvin[chroma]" ```uv add "marvin[chroma]"Configure Chroma:
import marvin
from marvin.memory.providers import chroma
provider = chroma.ChromaEphemeralMemory()
# or provider = chroma.ChromaPersistentMemory()
# or provider = chroma.ChromaCloudMemory()
memory = marvin.Memory(
key="knowledge",
provider=provider
)LanceDB provides a fast, efficient vector store with columnar storage:
pip install "marvin[lance]"uv add "marvin[lance]"Configure LanceDB:
import marvin
from marvin.memory.providers import LanceProvider
provider = LanceProvider(uri="data/memories.lance")
memory = marvin.Memory(
key="knowledge",
provider=provider
)Use PostgreSQL with pgvector for scalable, production-ready vector storage:
pip install "marvin[postgres]"uv add "marvin[postgres]"Configure PostgreSQL:
import marvin
from marvin.memory.providers import PostgresProvider
provider = PostgresProvider(
connection_string="postgresql://user:pass@localhost/db"
)
memory = marvin.Memory(
key="knowledge",
provider=provider
)Qdrant is a high-performance vector search engine built with Rust.
pip install "marvin" "qdrant-client[fastembed]"uv add marvin "qdrant-client[fastembed]"Configure Qdrant:
import marvin
from marvin.memory.providers import QdrantProvider
from qdrant_client import AsyncQdrantClient
provider = QdrantProvider(client=AsyncQdrantClient())
memory = marvin.Memory(
key="knowledge",
provider=provider
)Install all memory providers at once:
```bash pip pip install "marvin[memory]" # Includes chroma, lance, postgres, qdrant ```uv add "marvin[memory]" # Includes chroma, lance, postgres and qdrant