Your First Letta Agent

Create an agent, send messages, and understand basic memory

This example walks you through creating your first Letta agent from scratch. Unlike traditional chatbots that forget everything between conversations, Letta agents are stateful - they maintain persistent memory and can learn about you over time.

By the end of this guide, you’ll understand how to create an agent, send it messages, and see how it automatically updates its memory based on your interactions.

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

  • Initializing the Letta client
  • Creating an agent with memory blocks
  • Sending messages and receiving responses
  • How agents update their own memory
  • Inspecting memory tool calls and block contents

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

A client is a connection to a Letta server. It’s used to create and interact with agents, as well as any of Letta’s other features.

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 Agent

Now that we have a client, let’s create an agent with memory blocks that define what the agent knows about itself and you. Memory blocks can be used for any purpose, but we’re building a simple chatbot that stores information about its personality (persona) and you (human).

1// Create your first agent
2// API Reference: https://docs.letta.com/api-reference/agents/create
3const agent = await client.agents.create({
4 name: "hello_world_assistant",
5
6 // Memory blocks define what the agent knows about itself and you.
7 // Agents can modify these blocks during conversations using memory
8 // tools like memory_replace, memory_insert, memory_rethink, and memory.
9 memoryBlocks: [
10 {
11 label: "persona",
12 value: "I am a friendly AI assistant here to help you learn about Letta."
13 },
14 {
15 label: "human",
16 value: "Name: User\nFirst interaction: Learning about Letta"
17 }
18 ],
19
20 // Model configuration
21 model: "openai/gpt-4o-mini",
22 // embedding: "openai/text-embedding-3-small", // Only set this if self-hosting
23});
24
25console.log(`Created agent: ${agent.id}`);
Created agent: agent-a1b2c3d4-e5f6-7890-abcd-ef1234567890

Memory blocks are the foundation of Letta agents. The persona block defines the agent’s identity and behavior, while the human block stores information about the user. Learn more in the Memory Blocks guide.

Step 3: Send Your First Message

Now let’s send a message to the agent to see what it can do.

1// Send a message to your agent
2// API Reference: https://docs.letta.com/api-reference/agents/messages/create
3const response = await client.agents.messages.create(agent.id, {
4 messages: [{
5 role: "user",
6 content: "Hello! What's your purpose?"
7 }]
8});
9
10// Extract and print the assistant's response
11for (const message of response.messages) {
12 if (message.messageType === "assistant_message") {
13 console.log(`Assistant: ${message.content}`);
14 }
15}
Assistant: Hello! I'm here to help you learn about Letta and answer any questions
you might have. Letta is a framework for building stateful AI agents with long-term
memory. I can explain concepts, provide examples, and guide you through using the
platform. What would you like to know?

Step 4: Provide Information for the Agent to Remember

Now let’s give the agent some information about yourself. If prompted correctly, the agent can add this information to a relevant memory block using one of its default memory tools. Unless tools are modified during creation, new agents usually have memory_insert and memory_replace tools.

1// Send information about yourself
2const response2 = await client.agents.messages.create(agent.id, {
3 messages: [{
4 role: "user",
5 content: "My name is Cameron. Please store this information in your memory."
6 }]
7});
8
9// Print out tool calls and the assistant's response
10for (const msg of response2.messages) {
11 if (msg.messageType === "assistant_message") {
12 console.log(`Assistant: ${msg.content}\n`);
13 }
14 if (msg.messageType === "tool_call_message") {
15 console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);
16 }
17}
Tool call: memory_replace({"block_label": "human", "old_content": "Name: User", "new_content": "Name: Cameron"})
Assistant: Got it! I've updated my memory with your name, Cameron. How can I assist you today?

Notice the tool_call_message showing the agent using the memory_replace tool to update the human block. This is how Letta agents manage their own memory.

Step 5: Inspect Agent Memory

Let’s see what the agent remembers. We’ll print out both the summary and the full content of each memory block:

1// Retrieve the agent's current memory blocks
2// API Reference: https://docs.letta.com/api-reference/agents/blocks/list
3const blocks = await client.agents.blocks.list(agent.id);
4
5console.log("Current Memory:");
6for (const block of blocks) {
7 console.log(` ${block.label}: ${block.value.length}/${block.limit} chars`);
8 console.log(` ${block.value}\n`);
9}

The persona block should have:

I am a friendly AI assistant here to help you learn about Letta.

The human block should have something like:

Name: Cameron

Notice how the human block now contains “Name: Cameron” instead of “Name: User”. The agent used the memory_replace tool to update its memory based on the information you provided.

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 using LETTA_API_KEY environment variable
5 const client = new LettaClient({ token: process.env.LETTA_API_KEY });
6
7 // If self-hosting, specify the base URL:
8 // const client = new LettaClient({ baseUrl: "http://localhost:8283" });
9
10 // Create agent
11 const agent = await client.agents.create({
12 name: "hello_world_assistant",
13 memoryBlocks: [
14 {
15 label: "persona",
16 value: "I am a friendly AI assistant here to help you learn about Letta."
17 },
18 {
19 label: "human",
20 value: "Name: User\nFirst interaction: Learning about Letta"
21 }
22 ],
23 model: "openai/gpt-4o-mini",
24 // embedding: "openai/text-embedding-3-small", // Only set this if self-hosting
25 });
26
27 console.log(`Created agent: ${agent.id}\n`);
28
29 // Send first message
30 let response = await client.agents.messages.create(agent.id, {
31 messages: [{ role: "user", content: "Hello! What's your purpose?" }]
32 });
33
34 for (const msg of response.messages) {
35 if (msg.messageType === "assistant_message") {
36 console.log(`Assistant: ${msg.content}\n`);
37 }
38 }
39
40 // Send information about yourself
41 response = await client.agents.messages.create(agent.id, {
42 messages: [{ role: "user", content: "My name is Cameron. Please store this information in your memory." }]
43 });
44
45 // Print out tool calls and the assistant's response
46 for (const msg of response.messages) {
47 if (msg.messageType === "assistant_message") {
48 console.log(`Assistant: ${msg.content}\n`);
49 }
50 if (msg.messageType === "tool_call_message") {
51 console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);
52 }
53 }
54
55 // Inspect memory
56 const blocks = await client.agents.blocks.list(agent.id);
57 console.log("Current Memory:");
58 for (const block of blocks) {
59 console.log(` ${block.label}: ${block.value.length}/${block.limit} chars`);
60 console.log(` ${block.value}\n`);
61 }
62}
63
64main();

Key Concepts

Stateful Agents

Letta agents maintain memory across conversations, unlike stateless chat APIs

Memory Blocks

Modular memory components that agents can read and update during conversations

Persistent Context

Agents remember user preferences, conversation history, and learned information

Automatic Updates

Agents intelligently update their memory as they learn more about you

Next Steps