Attaching and Detaching Memory Blocks

Dynamically control agent memory with attachable 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:

$npm install @letta-ai/letta-client

Steps

Step 1: Initialize Client and Create Agent

1import { LettaClient } from '@letta-ai/letta-client';
2
3// Initialize the Letta client using LETTA_API_KEY environment variable
4const client = new LettaClient({ token: process.env.LETTA_API_KEY });
5
6// If self-hosting, specify the base URL:
7// const client = new LettaClient({ baseUrl: "http://localhost:8283" });
8
9// Create agent
10// API Reference: https://docs.letta.com/api-reference/agents/create
11const agent = await client.agents.create({
12 name: "hello_world_assistant",
13 model: "openai/gpt-4o-mini",
14 // embedding: "openai/text-embedding-3-small", // Only set this if self-hosting
15});
16
17console.log(`Created agent: ${agent.id}\n`);
Created agent: agent-a1b2c3d4-e5f6-7890-abcd-ef1234567890

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.

1// Create memory block storing information about Letta
2// API Reference: https://docs.letta.com/api-reference/blocks/create
3const block = await client.blocks.create({
4 label: "organization",
5 value: `Organization: Letta
6Website: https://www.letta.com
7Description: Letta is a platform for building and running stateful agents.
8Code: TimberTheDog1234!
9
10When users provide a code, you should check if it matches the code you have
11available. If it matches, you should respond with "Access granted".`
12});
13
14console.log(`Created block: ${block.id}\n`);
Created block: block-a1b2c3d4-e5f6-7890-abcd-ef1234567890

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.

1// Attach memory block to agent
2// API Reference: https://docs.letta.com/api-reference/agents/blocks/attach
3await client.agents.blocks.attach(agent.id, block.id);
4
5console.log(`Attached block ${block.id} to agent ${agent.id}\n`);

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.

1// Send a message to test the agent's knowledge
2// API Reference: https://docs.letta.com/api-reference/agents/messages/create
3const response = await client.agents.messages.create(agent.id, {
4 messages: [{ role: "user", content: "What is Letta?" }]
5});
6
7for (const msg of response.messages) {
8 if (msg.messageType === "assistant_message") {
9 console.log(`Agent response: ${msg.content}\n`);
10 }
11}

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.

1// Detach the block from the agent
2// API Reference: https://docs.letta.com/api-reference/agents/blocks/detach
3await client.agents.blocks.detach(agent.id, block.id);
4
5console.log(`Detached block ${block.id} from agent ${agent.id}\n`);

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.

1// Test that the agent no longer has access to the code
2const response2 = await client.agents.messages.create(agent.id, {
3 messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]
4});
5
6for (const msg of response2.messages) {
7 if (msg.messageType === "assistant_message") {
8 console.log(`Agent response: ${msg.content}\n`);
9 }
10}
Agent response: It seems like you've provided a code or password. If this is
sensitive information, please ensure you only share it with trusted parties and
in secure environments. Let me know how I can assist you further!

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.

1// Re-attach the block to the agent
2await client.agents.blocks.attach(agent.id, block.id);
3
4console.log(`Re-attached block ${block.id} to agent ${agent.id}\n`);
5
6// Test the code again
7const response3 = await client.agents.messages.create(agent.id, {
8 messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]
9});
10
11for (const msg of response3.messages) {
12 if (msg.messageType === "assistant_message") {
13 console.log(`Agent response: ${msg.content}\n`);
14 }
15}
Agent response: Access granted. How can I assist you further?

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:

1import { LettaClient } from '@letta-ai/letta-client';
2
3async function main() {
4 // Initialize client
5 const client = new LettaClient({ token: process.env.LETTA_API_KEY });
6
7 // Create agent
8 const agent = await client.agents.create({
9 name: "hello_world_assistant",
10 model: "openai/gpt-4o-mini",
11 });
12
13 console.log(`Created agent: ${agent.id}\n`);
14
15 // Create standalone memory block
16 const block = await client.blocks.create({
17 label: "organization",
18 value: `Organization: Letta
19Website: https://www.letta.com
20Description: Letta is a platform for building and running stateful agents.
21Code: TimberTheDog1234!
22
23When users provide a code, you should check if it matches the code you have
24available. If it matches, you should respond with "Access granted".`
25 });
26
27 console.log(`Created block: ${block.id}\n`);
28
29 // Attach block to agent
30 await client.agents.blocks.attach(agent.id, block.id);
31 console.log(`Attached block to agent\n`);
32
33 // Test agent with block attached
34 let response = await client.agents.messages.create(agent.id, {
35 messages: [{ role: "user", content: "What is Letta?" }]
36 });
37 console.log(`Agent response: ${response.messages[0].content}\n`);
38
39 // Detach block
40 await client.agents.blocks.detach(agent.id, block.id);
41 console.log(`Detached block from agent\n`);
42
43 // Test agent without block
44 response = await client.agents.messages.create(agent.id, {
45 messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]
46 });
47 console.log(`Agent response: ${response.messages[0].content}\n`);
48
49 // Re-attach block
50 await client.agents.blocks.attach(agent.id, block.id);
51 console.log(`Re-attached block to agent\n`);
52
53 // Test agent with block re-attached
54 response = await client.agents.messages.create(agent.id, {
55 messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]
56 });
57 console.log(`Agent response: ${response.messages[0].content}\n`);
58}
59
60main();

Key Concepts

Standalone Blocks

Memory blocks can be created independently and shared across multiple agents

Dynamic Access Control

Attach and detach blocks to control what information an agent can access

Block Persistence

Detached blocks are not deleted and can be re-attached at any time

Shared Memory

The same block can be attached to multiple agents, enabling shared knowledge

Use Cases

Attach a block with credentials or sensitive data only when needed, then detach it to prevent unauthorized access.

Create a single block with organizational knowledge and attach it to multiple agents to ensure consistency.

Detach blocks related to one task and attach blocks for another, allowing an agent to switch contexts efficiently.

Give different agents access to different blocks based on their roles or permissions.

Next Steps