Utility: Sync Primary Agent Archive to Sleeptime Agent

When using sleeptime agents, you may want your primary agent and sleeptime agent to share the same archival memory. This utility handles the sync automatically.

The Problem

By default, sleeptime agents may have their own archive or no archive attached. If you want sleeptime to write to the same archival store as your primary agent, you need to:

  1. Ensure the primary agent has an archive
  2. Detach any existing archive from the sleeptime agent
  3. Attach the primary’s archive to the sleeptime agent

The Utility

from letta_client import Letta
import os

def sync_sleeptime_archive(
    primary_id: str,
    archive_name: str = "sleeptime_shared_archive",
    auto_create: bool = True,
    auto_detach: bool = False,
    client: Letta = None
) -> str:
    """
    Sync a primary agent's archive to its sleeptime agent(s).
    
    Args:
        primary_id: The primary agent's ID
        archive_name: Name for new archive if created
        auto_create: Create archive automatically if none exists
        auto_detach: Detach existing sleeptime archives without prompting
        client: Letta client (creates one if not provided)
    
    Returns:
        The shared archive ID
    
    Raises:
        ValueError: If agent isn't in a sleeptime group or no archive available
    """
    client = client or Letta(api_key=os.getenv("LETTA_API_KEY"))
    primary = client.agents.retrieve(primary_id)
    
    # Verify sleeptime group
    if primary.managed_group.manager_type != "sleeptime":
        raise ValueError(f"Agent {primary_id} is not in a sleeptime group")
    
    if not primary.managed_group.agent_ids:
        raise ValueError("No sleeptime agents found in group")
    
    # Get or create archive
    archives = client.archives.list(agent_id=primary_id)
    if archives.items:
        archive_id = archives.items[0].id
    elif auto_create:
        archive = client.archives.create(
            name=archive_name,
            description="Shared archive between primary and sleeptime agents"
        )
        archive_id = archive.id
    else:
        raise ValueError("No archive attached and auto_create=False")
    
    # Sync to sleeptime agents
    for agent_id in primary.managed_group.agent_ids:
        existing = client.archives.list(agent_id=agent_id)
        
        if existing.items:
            if not auto_detach:
                raise ValueError(f"Agent {agent_id} has existing archive. Use auto_detach=True")
            client.agents.archives.detach(existing.items[0].id, agent_id=agent_id)
        
        client.agents.archives.attach(archive_id, agent_id=agent_id)
    
    return archive_id


# Usage
if __name__ == "__main__":
    archive_id = sync_sleeptime_archive(
        "agent-aab12188-2a42-4fa3-a5a8-a35fb7518db2",
        auto_detach=True
    )
    print(f"Shared archive: {archive_id}")

Usage Options

Basic (interactive mode):

archive_id = sync_sleeptime_archive("agent-xxx")

Automated (no prompts):

archive_id = sync_sleeptime_archive(
    "agent-xxx",
    auto_create=True,
    auto_detach=True
)

With existing client:

client = Letta(api_key="...")
archive_id = sync_sleeptime_archive("agent-xxx", client=client)

What It Does

  1. Verifies the agent is in a sleeptime group
  2. Gets existing archive or creates one if auto_create=True
  3. For each sleeptime agent in the group:
    • Detaches existing archive if present (when auto_detach=True)
    • Attaches the primary agent’s archive

After running, both your primary and sleeptime agents will read/write to the same archival memory store.