Skip to main content

An experimental SDK for using Letta subagents for pluggable memory management

Project description

Memory SDK

An experimental SDK for using Letta agents for long-term memory and learning in a pluggable way. When messages are added, subconsious memory agents process them to generate learned context that can be plugged into your system prompt, such as a user profile or a conversational summary.

+========================================+
|         SYSTEM PROMPT                  |
+========================================+
|      LEARNED CONTEXT (HUMAN)           | <- memory agent (learning from message history)
+========================================+
|           MESSAGES                     |
|  * User -> Assistant                   |
|  * User -> Assistant                   |
|  * User -> Assistant                   |
|  * ...                                 |
+========================================+

For a specific user, the memory agent will learn a summary block and a human block, formatted as follows:

<conversation_summary>
Sarah introduced herself and asked the assistant to tell about itself. The assistant provided a brief self-description and offered further help.
</conversation_summary>

<human description="Details about the human user you are speaking to.">
Name: Sarah
Interests: Likes cats (2025-09-03)
</human>

You can customize the prompt format by getting the raw summary or user block string with prompt_formatted=False.

Quickstart

  1. Create an Letta API Key
  2. Run export LETTA_API_KEY=...
  3. Install: pip install letta-memory

Usage: Conversational Memory

You can save conversation histories using the Memory SDK, and later retrieve the learned context block to place into your system prompt. This allows your agents to have an evolving understand of the user. Example: Create a basic OpenAI gpt-4o-mini chat agent with memory

from openai import OpenAI
from letta_memory import Memory

openai_client = OpenAI()
memory = Memory()

def chat_with_memories(message: str, user_id: str = "default_user") -> str:

    # get the user memory 
    user_memory = memory.get_user_memory(user_id)
    if not user_memory:
        memory.initialize_user_memory(user_id, reset=True)
        user_memory = memory.get_user_memory(user_id)
    
    # format the user memory 
    user_memory_prompt= memory.get_user_memory(user_id, prompt_formatted=True)

    # generate the assistant response
    system_prompt = f"<system>You are a helpful AI assistant</system>"
    system_prompt += f"\n{user_memory_prompt}"
    messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]
    response = openai_client.chat.completions.create(model="gpt-4o-mini", messages=messages)
    assistant_response = response.choices[0].message.content

    # Create new memories from the conversation
    messages.append({"role": "assistant", "content": assistant_response})
    memory.add_messages(user_id, messages)

    return assistant_response

def main():
    print("Chat with AI (type 'exit' to quit)")
    while True:
        user_input = input("You: ").strip()
        if user_input.lower() == 'exit':
            print("Goodbye!")
            break
        print(f"AI: {chat_with_memories(user_input)}")

if __name__ == "__main__":
    main()

SDK Reference

You can initialize the memory SDK with memory = Memory().

Adding memories

Save messages by adding them to memory:

run = memory.add_messages("user_id", [{"role": "user", "content": "hi"}])

This will send the messages to the memory agent for processing. Note that each time you add messages, this will trigger an invocation of the memory agent. To reduce costs, you may want to batch together multiple messages (recommended 5-10).

Waiting for learning completition

Messages are processed asynchronously, so to ensure all memory updates are reflected you should wait for the agent learning to complete.

memory.wait_for_run(run)

This will block until the memory agent has completed processing.

Retrieving memories for a user

You can retrieve the summary and/or user memory for a given user with:

summary = memory.get_summary("user_id", prompt_formatted=True)
user_memory = memory.get_user_memory("user_id", prompt_formatted=True)

Retrieving the memory agent

Memories are formed by Letta agents using the sleeptime architecture. You can get the agent's ID with:

agent_id = memory.get_memory_agent_id("user_id")

The agent can be viewed at https://app.letta.com/agents/<AGENT_ID>.

Deleting user memories

All memories and data associated with a user can be deleted with:

memory.delete_user("user_id")

Roadmap

  • TypeScript support
  • Learning from files
  • Query historical messages
  • Save messages as archival memories
  • Query archival memory
  • Add "sleep" (offline collective revisioning of all data)

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

ai_memory_sdk-0.1.6.tar.gz (89.8 kB view details)

Uploaded Source

Built Distribution

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

ai_memory_sdk-0.1.6-py3-none-any.whl (99.3 kB view details)

Uploaded Python 3

File details

Details for the file ai_memory_sdk-0.1.6.tar.gz.

File metadata

  • Download URL: ai_memory_sdk-0.1.6.tar.gz
  • Upload date:
  • Size: 89.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for ai_memory_sdk-0.1.6.tar.gz
Algorithm Hash digest
SHA256 97691e47569d47d59dfd2ff4bb3dc22e865bf50294beef6fa15265ebf8d126be
MD5 b9643cae1c47bb13866dc580a8e7318f
BLAKE2b-256 d826df3dce175036da55c2a1fd898614e903883fdc5cf9f1bcfd0e3867f02892

See more details on using hashes here.

File details

Details for the file ai_memory_sdk-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_memory_sdk-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0ff65373c4e0baab795992241906337af2874e73a8c6b13f6fabeeff8c07ef32
MD5 31f21bbc13b0feac189ae3584f1a3f58
BLAKE2b-256 84a03e3b6c656e24ac59fac2cdfdf3816c3836cc68428b0963d538c4a915395b

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