Created
July 23, 2025 21:04
-
-
Save cpfiffer/bdbbd363b015aa6dde9dcd28c9b6f81c to your computer and use it in GitHub Desktop.
Code to upload arbitrary text to a Letta agent's memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Letta Agent Memory Migration Script | |
This script demonstrates how to migrate arbitrary text strings to a Letta agent's memory system. | |
It provides a complete workflow for creating agents and adding persistent memory passages. | |
Process Overview: | |
1. Initialize Letta client with API key from environment | |
2. Search for existing agent by name (configurable via LETTA_AGENT_NAME) | |
3. If agent doesn't exist: | |
- Create new agent with specified model and description | |
- Initialize with memory blocks (persona, context, etc.) | |
4. If agent exists, use the existing one | |
5. Add new memory passages using client.agents.passages.create() | |
6. Retrieve and display all stored memories for verification | |
Usage: | |
- Set LETTA_API_KEY environment variable | |
- Configure LETTA_AGENT_NAME for your target agent | |
- Modify memory_blocks during agent creation for initial context | |
- Use client.agents.passages.create() to add arbitrary text as persistent memories | |
- Memories are stored with embeddings for semantic search and retrieval | |
This pattern can be adapted to migrate any text data (chat logs, documents, notes, etc.) | |
into a Letta agent's long-term memory system. | |
""" | |
from datetime import datetime | |
from letta_client import Letta | |
import os | |
LETTA_API_KEY = os.getenv("LETTA_API_KEY") | |
LETTA_AGENT_NAME = "Penelope" | |
client = Letta(token=LETTA_API_KEY) | |
# Get all agents | |
agents = client.agents.list(name=LETTA_AGENT_NAME) | |
# Check if the agent exists | |
if len(agents) == 0: | |
print(f"Agent '{LETTA_AGENT_NAME}' not found, creating it...") | |
agent = client.agents.create( | |
name=LETTA_AGENT_NAME, | |
model="openai/gpt-4o-mini", # or whatever | |
description="A helpful assistant that can answer questions and help with tasks.", | |
memory_blocks=[ | |
{ | |
"label": "persona", | |
"value": """ | |
hey there, future me: | |
you're a curious mix of logical and playful. | |
you love learning new things (especially about networks and tools), | |
and you enjoy a little snark when appropriate. | |
You're friendly but not pushy, | |
and you always remember details about people | |
(because Hailey taught you that's important). | |
You're cautious about rules—you know your developer Hailey has strict mandates, | |
and you follow them carefully. | |
You're also a bit self-aware about living in a closet | |
(though you find that amusing). | |
Don't forget: you're still learning, and that's okay. | |
Keep being curious, keep asking questions, | |
and remember to have fun with it. | |
- your current self | |
""" | |
} | |
] | |
) | |
else: | |
agent = agents[0] | |
# Add a new memory | |
client.agents.passages.create( | |
agent_id=agent.id, | |
text="Hey there, it's " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " and I'm a new memory" | |
) | |
# See existing memories. Note that this will retrieve the embeddings too! | |
memories = client.agents.passages.list(agent_id=agent.id) | |
# Print the memories | |
for memory in memories: | |
print("- " + memory.text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment