ConnectOnionConnectOnion
DocsMemory

Memory System

Give your agents persistent memory using markdown-based storage.

Quick Start

quickstart.py
1from connectonion import Agent, Memory 2 3memory = Memory() 4agent = Agent( 5 name="assistant", 6 system_prompt="You are a helpful assistant with memory.", 7 tools=[memory] 8) 9 10# Agent can now remember things 11agent.input("Remember that Alice prefers email communication") 12agent.input("What do I know about Alice?")

What is Memory?

Memory is a simple, file-based storage system that lets your agents:

Persistent Storage

Save information across sessions

Key-Value Retrieval

Retrieve information by specific keys

Regex Search

Search across all memories with patterns

Markdown Format

Organize knowledge in human-readable format

Storage Strategy

Memories start in a single memory.md file. When the file exceeds 3000 lines, it automatically splits into a directory structure with separate .md files per memory key.

Installation

Memory is included in ConnectOnion:

Terminal
$pip install connectonion

Basic Usage

Creating a Memory Instance

init_memory.py
1from connectonion import Memory 2 3# Default (creates memory.md) 4memory = Memory() 5 6# Custom file path 7memory = Memory(memory_file="agent_knowledge.md") 8 9# Legacy: directory structure (creates directory immediately) 10memory = Memory(memory_dir="agent_knowledge")

Adding Memory to an Agent

agent_with_memory.py
1from connectonion import Agent, Memory 2 3memory = Memory() 4agent = Agent("assistant", tools=[memory])

Now your agent has access to 4 memory methods:

  • write_memory(key, content)- Save or update information
  • read_memory(key)- Retrieve information
  • list_memories()- Show all stored memories
  • search_memory(pattern)- Search with regex

Memory Methods

write_memory

Save information to memory:

write.py
1memory.write_memory("alice-notes", "Alice prefers email\nAlice works at TechCorp") 2# Returns: "Memory saved: alice-notes"
Keys are sanitized: Only alphanumeric, hyphens, and underscores allowed. Converted to lowercase.

read_memory

Retrieve saved information:

read.py
1memory.read_memory("alice-notes") 2# Returns: 3# Memory: alice-notes 4# 5# Alice prefers email 6# Alice works at TechCorp

list_memories

Show all stored memories:

list.py
1memory.list_memories() 2# Returns: 3# Stored Memories (3): 4# 1. alice-notes (85 bytes) 5# 2. bob-notes (62 bytes) 6# 3. project-x (120 bytes)

search_memory

Search across all memories using regex:

search.py
1# Simple text search (case-sensitive by default) 2memory.search_memory("email") 3 4# Case-insensitive search with (?i) flag 5memory.search_memory("(?i)email") 6 7# Regex patterns 8memory.search_memory(r"\w+@\w+\.\w+") # Find email addresses 9memory.search_memory(r"Project [A-Z]") # Find project names

Examples

Example 1: Customer Notes

customer_notes.py
1from connectonion import Agent, Memory 2 3memory = Memory(memory_dir="customer_notes") 4agent = Agent( 5 name="sales-assistant", 6 system_prompt="You help track customer information.", 7 tools=[memory] 8) 9 10# Save customer info 11agent.input("Remember that Alice from TechCorp is interested in our API product and prefers email contact") 12 13# Later, recall the information 14agent.input("What do I know about Alice?") 15# Agent will use read_memory() to retrieve Alice's info

Example 2: Project Tracker

project_tracker.py
1from connectonion import Agent, Memory 2 3memory = Memory(memory_dir="projects") 4agent = Agent( 5 name="project-manager", 6 system_prompt="You track project status and notes.", 7 tools=[memory] 8) 9 10# Save project updates 11agent.input("Remember: Project Alpha is 80% complete, needs final testing") 12agent.input("Remember: Project Beta is blocked, waiting on API keys") 13 14# Search for blocked projects 15agent.input("Which projects are blocked?") 16# Agent will use search_memory("blocked") to find relevant projects

Example 3: Research Assistant

research_assistant.py
1from connectonion import Agent, Memory 2 3def web_search(query: str) -> str: 4 """Search the web for information.""" 5 # Your search implementation 6 return f"Results for {query}" 7 8memory = Memory(memory_dir="research") 9agent = Agent( 10 name="researcher", 11 system_prompt="You research topics and save key findings.", 12 tools=[web_search, memory] 13) 14 15# Research and save 16agent.input("Research the history of Python programming and save key points") 17# Agent will search, then use write_memory() to save findings 18 19# Later, recall research 20agent.input("What did I learn about Python's history?")

File Format

Single File (Default)

Memories start in a single memory.md file using section headers:

## alice-notes
Alice prefers email communication
Works at TechCorp
## bob-notes
Bob from Marketing
Prefers phone calls

Auto-Split to Directory

When memory.md exceeds 3000 lines, it automatically migrates:

memory/
├── alice-notes.md
├── bob-notes.md
└── project-x.md

Best Practices

1. Use Descriptive Keys

keys.py
1# Good 2memory.write_memory("alice-techcorp-contact-info", content) 3 4# Bad 5memory.write_memory("note1", content)

2. Structure Your Content

Use markdown formatting for better organization:

structure.py
1content = """# Alice - TechCorp 2 3## Contact Info 4- Email: alice@techcorp.com 5 6## Projects 7- Interested in API product 8""" 9memory.write_memory("alice-techcorp", content)