Attaching and Detaching Memory Blocks
Overview
Memory blocks are structured sections of an agent’s context window that persist across all interactions. They’re always visible to the agent while they are attached. This makes them perfect for storing information that agents need constant access to, like organizational policies, user preferences, or working memory.
One of the most powerful features of memory blocks is that they can be created independently and attached to or detached from agents at any time.
This allows you to:
- Dynamically control what information an agent has access to
- Share memory across multiple agents by attaching the same block to different agents
- Temporarily grant access to sensitive information, then revoke it when no longer needed
- Switch contexts by swapping out blocks as an agent moves between different tasks
By the end of this guide, you’ll understand how to create standalone memory blocks, attach them to agents, detach them to remove access, and re-attach them when needed.
For a comprehensive overview of memory blocks and their capabilities, see the memory blocks guide.
This example uses Letta Cloud. Generate an API key at app.letta.com/api-keys and set it as LETTA_API_KEY in your environment. Self-hosted servers only need an API key if authentication is enabled. You can learn more about self-hosting here.
What You’ll Learn
- Creating standalone memory blocks
- Attaching blocks to agents
- Testing agent access to attached blocks
- Detaching blocks to revoke access
- Re-attaching blocks to restore access
Prerequisites
You will need to install letta-client to interface with a Letta server:
Steps
Step 1: Initialize Client and Create Agent
Expected Output
Step 2: Create a Standalone Memory Block
Memory blocks can be created independently of any agent. This allows you to share the same block across multiple agents or attach/detach blocks as needed.
In this example, we’ll create a standalone memory block storing information about Letta. We’ll include a code that you can use to get the agent to respond to indicate that it has access to information in the block.
When the block is attached, writing “The code is TimberTheDog1234!” will cause the agent to respond with “Access granted”. If the block is not attached, the agent will not have access to any content in the block and will likely be confused by the code.
Expected Output
Step 3: Attach Block to Agent
Now let’s attach the block to our agent. Attached blocks are injected into the agent’s context window and are available to the agent to use in its responses.
Step 4: Test Agent Access to Block
The agent can now see what’s in the block. Let’s ask it about Letta to verify that it can see the general information in the block — the description, website, and organization name.
The agent will respond with general information about Letta:
Agent response: Letta is a platform designed for building and running stateful agents. You can find more information about it on their website: https://www.letta.com
Step 5: Detach Block from Agent
Blocks can be detached from an agent, removing them from the agent’s context window. Detached blocks are not deleted and can be re-attached to an agent later.
Step 6: Verify Block is Detached
Let’s test the code that was in the block. The agent should no longer have access to it.
Expected Output
The agent doesn’t recognize the code because the block containing that information has been detached.
Step 7: Re-attach Block and Test Again
Let’s re-attach the block to restore the agent’s access to the information.
Expected Output
The agent now recognizes the code because we’ve re-attached the block containing that information.
Complete Example
Here’s the full code in one place that you can run:
Key Concepts
Memory blocks can be created independently and shared across multiple agents
Attach and detach blocks to control what information an agent can access
Detached blocks are not deleted and can be re-attached at any time
The same block can be attached to multiple agents, enabling shared knowledge
Use Cases
Temporary Access to Sensitive Information
Attach a block with credentials or sensitive data only when needed, then detach it to prevent unauthorized access.
Context Switching
Detach blocks related to one task and attach blocks for another, allowing an agent to switch contexts efficiently.
Role-Based Access
Give different agents access to different blocks based on their roles or permissions.