Shared Memory Blocks

Enable multi-agent collaboration through shared memory

Overview

Memory blocks can be shared between multiple agents, enabling powerful multi-agent collaboration patterns. When a block is shared, all attached agents can read and write to it, creating a common workspace for coordinating information and tasks.

This tutorial demonstrates how to:

  • Create memory blocks that multiple agents can access
  • Build collaborative workflows where agents contribute different information
  • Use read-only blocks to provide shared context without allowing modifications
  • Understand how memory tools handle concurrent updates

By the end of this guide, you’ll understand how to build simple multi-agent systems where agents work together by sharing memory.

This tutorial 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.

The web_search tool used in this tutorial requires an EXA_API_KEY environment variable when self-hosting. You can learn more about self-hosting here.

What You’ll Learn

  • Creating standalone memory blocks for sharing
  • Attaching the same block to multiple agents
  • Building collaborative workflows with shared memory
  • Using read-only blocks for policies and system information
  • Understanding how memory tools handle concurrent updates

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

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" });

Step 2: Create a Shared Memory Block

Create a standalone memory block that will be shared between multiple agents. This block will serve as a collaborative workspace where both agents can contribute information.

We’re going to give the block the label “organization” to indicate that it contains information about some organization. The starting value of this block is “Organization: Letta” to give the agents a starting point to work from.

1// Create a memory block that will be shared between agents
2// API Reference: https://docs.letta.com/api-reference/blocks/create
3const block = await client.blocks.create({
4 label: "organization",
5 value: "Organization: Letta",
6 limit: 4000,
7});
8
9console.log(`Created shared block: ${block.id}\n`);

Step 3: Create Agents with Shared Block

Create two agents that will both have access to the same memory block. You can attach blocks during creation using block_ids or later using the attach method.

We’ll provide each agent with the web_search tool to search the web for information. This tool is built-in to Letta. If you are self-hosting, you will need to set an EXA_API_KEY environment variable for either the server or the agent to use this tool.

1// Create first agent with block attached during creation
2// API Reference: https://docs.letta.com/api-reference/agents/create
3const agent1 = await client.agents.create({
4 name: "agent1",
5 model: "openai/gpt-4o-mini",
6 blockIds: [block.id],
7 tools: ["web_search"],
8});
9console.log(`Created agent1: ${agent1.id}`);
10
11// Create second agent and attach block afterward
12const agent2 = await client.agents.create({
13 name: "agent2",
14 model: "openai/gpt-4o-mini",
15 tools: ["web_search"],
16});
17console.log(`Created agent2: ${agent2.id}`);
18
19// Attach the shared block to agent2
20// API Reference: https://docs.letta.com/api-reference/agents/blocks/attach
21await client.agents.blocks.attach(agent2.id, { blockId: block.id });
22console.log(`Attached block to agent2\n`);

Step 4: Have Agents Collaborate via Shared Memory

Now let’s have both agents research different topics and contribute their findings to the shared memory block.

  • Agent 1: Searches for information about the connection between memory blocks and Letta.
  • Agent 2: Searches for information about the origin of Letta.

We’re going to ask each agent to search for different information and insert what they learn into the shared memory block, prepended with the agent’s name (either Agent1: or Agent2:).

1// Agent1 searches for information about memory blocks
2// API Reference: https://docs.letta.com/api-reference/agents/messages/create
3const response1 = await client.agents.messages.create(agent1.id, {
4 messages: [{
5 role: "user",
6 content: `Find information about the connection between memory blocks and Letta.
7Insert what you learn into the memory block, prepended with "Agent1: ".`
8 }]
9});
10
11for (const msg of response1.messages) {
12 if (msg.messageType === "assistant_message") {
13 console.log(`Agent1 response: ${msg.content}`);
14 }
15 if (msg.messageType === "tool_call_message") {
16 console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);
17 }
18}
19
20// Agent2 searches for information about Letta's origin
21const response2 = await client.agents.messages.create(agent2.id, {
22 messages: [{
23 role: "user",
24 content: `Find information about the origin of Letta.
25Insert what you learn into the memory block, prepended with "Agent2: ".`
26 }]
27});
28
29for (const msg of response2.messages) {
30 if (msg.messageType === "assistant_message") {
31 console.log(`Agent2 response: ${msg.content}`);
32 }
33 if (msg.messageType === "tool_call_message") {
34 console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);
35 }
36}

Step 5: Inspect the Shared Memory

Let’s retrieve the shared memory block to see both agents’ contributions:

1// Retrieve the shared block to see what both agents learned
2// API Reference: https://docs.letta.com/api-reference/blocks/retrieve
3const updatedBlock = await client.blocks.retrieve(block.id);
4
5console.log("==== Updated block ====");
6console.log(updatedBlock.value);
7console.log("=======================\n");

The output should be something like this:

Organization: Letta

Agent1: Memory blocks are integral to the Letta framework for managing context in large language models (LLMs). They serve as structured units that enhance an agent’s ability to maintain long-term memory and coherence across interactions. Specifically, Letta utilizes memory blocks to organize context into discrete categories, such as “human” memory (user preferences and facts) and “persona” memory (the agent’s self-concept and traits). This structured approach allows agents to edit and persist important information, improving performance, personalization, and controllability. By effectively managing the context window through these memory blocks, Letta enhances the overall functionality and adaptability of its LLM agents.

Agent2: Letta originated as MemGPT, a research project focused on building stateful AI agents with long-term memory capabilities. It evolved into a platform for building and deploying production-ready agents.

Note that each agent has placed their information into the block, prepended with their name. This is a simple way to identify who contributed what to the block. You don’t have to prepend agent identifiers to the block, we only did this for demonstration purposes.

Understanding concurrent updates: Memory tools handle concurrent updates differently:

  • memory_insert is additive and the most robust for multi-agent systems. Multiple agents can insert content simultaneously without conflicts, as each insert simply appends to the block.
  • memory_replace validates that the exact old content exists before replacing it. If another agent modifies the content being replaced, the tool call fails with a validation error, preventing accidental overwrites.
  • memory_rethink performs a complete rewrite of the entire block and follows “most recent write wins.” This is a destructive operation - use cautiously in multi-agent systems as it can overwrite other agents’ contributions.

Step 6: Using Read-Only Blocks

Read-only blocks are useful for sharing policies, system information, or terms of service that agents should reference but not modify.

1// Create a read-only block for policies or system information
2// API Reference: https://docs.letta.com/api-reference/blocks/create
3const readOnlyBlock = await client.blocks.create({
4 label: "read_only_block",
5 value: "This is a read-only block.",
6 readOnly: true,
7});
8
9// Attach the read-only block to an agent
10const readOnlyAgent = await client.agents.create({
11 name: "read_only_agent",
12 model: "openai/gpt-4o-mini",
13 blockIds: [readOnlyBlock.id],
14});
15
16console.log(`Created read-only agent: ${readOnlyAgent.id}`);

Agents can see read-only blocks in their context but cannot modify them using memory tools. This is useful for organizational policies, system configuration, or any information that should be reference-only.

Complete Example

Here’s the full code in one place that you can run:

1import { LettaClient } from '@letta-ai/letta-client';
2
3// Initialize client
4const client = new LettaClient({ token: process.env.LETTA_API_KEY });
5
6// Create shared block
7const block = await client.blocks.create({
8 label: "organization",
9 value: "Organization: Letta",
10 limit: 4000,
11});
12
13console.log(`Created shared block: ${block.id}\n`);
14
15// Create agents with shared block
16const agent1 = await client.agents.create({
17 name: "agent1",
18 model: "openai/gpt-4o-mini",
19 blockIds: [block.id],
20 tools: ["web_search"],
21});
22
23const agent2 = await client.agents.create({
24 name: "agent2",
25 model: "openai/gpt-4o-mini",
26 tools: ["web_search"],
27});
28
29await client.agents.blocks.attach(agent2.id, { blockId: block.id });
30
31console.log(`Created agents: ${agent1.id}, ${agent2.id}\n`);
32
33// Agent1 contributes information
34const response1 = await client.agents.messages.create(agent1.id, {
35 messages: [{
36 role: "user",
37 content: `Find information about the connection between memory blocks and Letta.
38Insert what you learn into the memory block, prepended with "Agent1: ".`
39 }]
40});
41
42// Agent2 contributes information
43const response2 = await client.agents.messages.create(agent2.id, {
44 messages: [{
45 role: "user",
46 content: `Find information about the origin of Letta.
47Insert what you learn into the memory block, prepended with "Agent2: ".`
48 }]
49});
50
51// Inspect the shared memory
52const updatedBlock = await client.blocks.retrieve(block.id);
53console.log("==== Updated block ====");
54console.log(updatedBlock.value);
55console.log("=======================\n");
56
57// Create read-only block
58const readOnlyBlock = await client.blocks.create({
59 label: "policies",
60 value: "Company Policy: Always be helpful and respectful.",
61 readOnly: true,
62});
63
64const readOnlyAgent = await client.agents.create({
65 name: "policy_agent",
66 model: "openai/gpt-4o-mini",
67 blockIds: [readOnlyBlock.id],
68});
69
70console.log(`Created read-only agent: ${readOnlyAgent.id}`);

Key Concepts

Shared Memory

Multiple agents can access the same memory block, enabling collaboration and information sharing

Flexible Attachment

Blocks can be attached during agent creation with block_ids or later using the attach method

Concurrent Updates

Memory tools handle concurrent updates differently - insert is additive, replace validates, rethink overwrites

Read-Only Blocks

Prevent agent modifications while still providing shared context like policies or system information

Use Cases

Have multiple agents research different topics and contribute findings to a shared knowledge base.

Create read-only blocks with company policies, terms of service, or system guidelines that all agents reference.

Use shared blocks as a coordination layer where agents update task status and communicate progress.

Enable agents with different specializations to work together by sharing context and intermediate results.

Next Steps