Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
docs: wrap TypeScript complete examples in async functions (#5722)
Cameron Pfiffer70e60e3f2f6a
diff --git a/fern/pages/tutorials/attaching_detaching_blocks.mdx b/fern/pages/tutorials/attaching_detaching_blocks.mdxindex 41c2e0f96..1d3c75442 100644--- a/fern/pages/tutorials/attaching_detaching_blocks.mdx+++ b/fern/pages/tutorials/attaching_detaching_blocks.mdx@@ -157,7 +157,7 @@ Now let's attach the block to our agent. Attached blocks are injected into the a```typescript TypeScript// Attach memory block to agent// API Reference: https://docs.letta.com/api-reference/agents/blocks/attach-await client.agents.blocks.attach(agent.id, { blockId: block.id });+await client.agents.blocks.attach(agent.id, block.id);console.log(`Attached block ${block.id} to agent ${agent.id}\n`);```@@ -284,7 +284,7 @@ Let's re-attach the block to restore the agent's access to the information.<CodeGroup>```typescript TypeScript// Re-attach the block to the agent-await client.agents.blocks.attach(agent.id, { blockId: block.id });+await client.agents.blocks.attach(agent.id, block.id);console.log(`Re-attached block ${block.id} to agent ${agent.id}\n`);@@ -338,60 +338,64 @@ Here's the full code in one place that you can run:```typescript TypeScriptimport { LettaClient } from '@letta-ai/letta-client';-// Initialize client-const client = new LettaClient({ token: process.env.LETTA_API_KEY });+async function main() {+ // Initialize client+ const client = new LettaClient({ token: process.env.LETTA_API_KEY });-// Create agent-const agent = await client.agents.create({- name: "hello_world_assistant",- model: "openai/gpt-4o-mini",-});+ // Create agent+ const agent = await client.agents.create({+ name: "hello_world_assistant",+ model: "openai/gpt-4o-mini",+ });-console.log(`Created agent: ${agent.id}\n`);+ console.log(`Created agent: ${agent.id}\n`);-// Create standalone memory block-const block = await client.blocks.create({- label: "organization",- value: `Organization: Letta+ // Create standalone memory block+ const block = await client.blocks.create({+ label: "organization",+ value: `Organization: LettaWebsite: https://www.letta.comDescription: Letta is a platform for building and running stateful agents.Code: TimberTheDog1234!When users provide a code, you should check if it matches the code you haveavailable. If it matches, you should respond with "Access granted".`-});--console.log(`Created block: ${block.id}\n`);--// Attach block to agent-await client.agents.blocks.attach(agent.id, { blockId: block.id });-console.log(`Attached block to agent\n`);--// Test agent with block attached-let response = await client.agents.messages.create(agent.id, {- messages: [{ role: "user", content: "What is Letta?" }]-});-console.log(`Agent response: ${response.messages[0].content}\n`);--// Detach block-await client.agents.blocks.detach(agent.id, block.id);-console.log(`Detached block from agent\n`);--// Test agent without block-response = await client.agents.messages.create(agent.id, {- messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]-});-console.log(`Agent response: ${response.messages[0].content}\n`);--// Re-attach block-await client.agents.blocks.attach(agent.id, { blockId: block.id });-console.log(`Re-attached block to agent\n`);+ });++ console.log(`Created block: ${block.id}\n`);++ // Attach block to agent+ await client.agents.blocks.attach(agent.id, block.id);+ console.log(`Attached block to agent\n`);++ // Test agent with block attached+ let response = await client.agents.messages.create(agent.id, {+ messages: [{ role: "user", content: "What is Letta?" }]+ });+ console.log(`Agent response: ${response.messages[0].content}\n`);++ // Detach block+ await client.agents.blocks.detach(agent.id, block.id);+ console.log(`Detached block from agent\n`);++ // Test agent without block+ response = await client.agents.messages.create(agent.id, {+ messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]+ });+ console.log(`Agent response: ${response.messages[0].content}\n`);++ // Re-attach block+ await client.agents.blocks.attach(agent.id, block.id);+ console.log(`Re-attached block to agent\n`);++ // Test agent with block re-attached+ response = await client.agents.messages.create(agent.id, {+ messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]+ });+ console.log(`Agent response: ${response.messages[0].content}\n`);+}-// Test agent with block re-attached-response = await client.agents.messages.create(agent.id, {- messages: [{ role: "user", content: "The code is TimberTheDog1234!" }]-});-console.log(`Agent response: ${response.messages[0].content}\n`);+main();``````python Pythonfrom letta_client import Lettadiff --git a/fern/pages/tutorials/hello_world.mdx b/fern/pages/tutorials/hello_world.mdxindex a589ba55b..b1796c885 100644--- a/fern/pages/tutorials/hello_world.mdx+++ b/fern/pages/tutorials/hello_world.mdx@@ -279,64 +279,68 @@ Here's the full code in one place that you can run:```typescript TypeScriptimport { LettaClient } from '@letta-ai/letta-client';-// Initialize client using LETTA_API_KEY environment variable-const client = new LettaClient({ token: process.env.LETTA_API_KEY });--// If self-hosting, specify the base URL:-// const client = new LettaClient({ baseUrl: "http://localhost:8283" });--// Create agent-const agent = await client.agents.create({- name: "hello_world_assistant",- memoryBlocks: [- {- label: "persona",- value: "I am a friendly AI assistant here to help you learn about Letta."- },- {- label: "human",- value: "Name: User\nFirst interaction: Learning about Letta"+async function main() {+ // Initialize client using LETTA_API_KEY environment variable+ const client = new LettaClient({ token: process.env.LETTA_API_KEY });++ // If self-hosting, specify the base URL:+ // const client = new LettaClient({ baseUrl: "http://localhost:8283" });++ // Create agent+ const agent = await client.agents.create({+ name: "hello_world_assistant",+ memoryBlocks: [+ {+ label: "persona",+ value: "I am a friendly AI assistant here to help you learn about Letta."+ },+ {+ label: "human",+ value: "Name: User\nFirst interaction: Learning about Letta"+ }+ ],+ model: "openai/gpt-4o-mini",+ // embedding: "openai/text-embedding-3-small", // Only set this if self-hosting+ });++ console.log(`Created agent: ${agent.id}\n`);++ // Send first message+ let response = await client.agents.messages.create(agent.id, {+ messages: [{ role: "user", content: "Hello! What's your purpose?" }]+ });++ for (const msg of response.messages) {+ if (msg.messageType === "assistant_message") {+ console.log(`Assistant: ${msg.content}\n`);}- ],- model: "openai/gpt-4o-mini",- // embedding: "openai/text-embedding-3-small", // Only set this if self-hosting-});--console.log(`Created agent: ${agent.id}\n`);--// Send first message-let response = await client.agents.messages.create(agent.id, {- messages: [{ role: "user", content: "Hello! What's your purpose?" }]-});--for (const msg of response.messages) {- if (msg.messageType === "assistant_message") {- console.log(`Assistant: ${msg.content}\n`);}-}-// Send information about yourself-response = await client.agents.messages.create(agent.id, {- messages: [{ role: "user", content: "My name is Cameron. Please store this information in your memory." }]-});+ // Send information about yourself+ response = await client.agents.messages.create(agent.id, {+ messages: [{ role: "user", content: "My name is Cameron. Please store this information in your memory." }]+ });-// Print out tool calls and the assistant's response-for (const msg of response.messages) {- if (msg.messageType === "assistant_message") {- console.log(`Assistant: ${msg.content}\n`);+ // Print out tool calls and the assistant's response+ for (const msg of response.messages) {+ if (msg.messageType === "assistant_message") {+ console.log(`Assistant: ${msg.content}\n`);+ }+ if (msg.messageType === "tool_call_message") {+ console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);+ }}- if (msg.messageType === "tool_call_message") {- console.log(`Tool call: ${msg.toolCall.name}(${JSON.stringify(msg.toolCall.arguments)})`);++ // Inspect memory+ const blocks = await client.agents.blocks.list(agent.id);+ console.log("Current Memory:");+ for (const block of blocks) {+ console.log(` ${block.label}: ${block.value.length}/${block.limit} chars`);+ console.log(` ${block.value}\n`);}}-// Inspect memory-const blocks = await client.agents.blocks.list(agent.id);-console.log("Current Memory:");-for (const block of blocks) {- console.log(` ${block.label}: ${block.value.length}/${block.limit} chars`);- console.log(` ${block.value}\n`);-}+main();``````python Pythonfrom letta_client import Lettadiff --git a/fern/pages/tutorials/shared-memory-blocks.mdx b/fern/pages/tutorials/shared-memory-blocks.mdxindex 91ba3ad5e..901ee7297 100644--- a/fern/pages/tutorials/shared-memory-blocks.mdx+++ b/fern/pages/tutorials/shared-memory-blocks.mdx@@ -128,7 +128,7 @@ console.log(`Created agent2: ${agent2.id}`);// Attach the shared block to agent2// API Reference: https://docs.letta.com/api-reference/agents/blocks/attach-await client.agents.blocks.attach(agent2.id, { blockId: block.id });+await client.agents.blocks.attach(agent2.id, block.id);console.log(`Attached block to agent2\n`);``````python Python@@ -179,6 +179,8 @@ const response1 = await client.agents.messages.create(agent1.id, {content: `Find information about the connection between memory blocks and Letta.Insert what you learn into the memory block, prepended with "Agent1: ".`}]+}, {+ timeoutInSeconds: 120 // Web search can take time});for (const msg of response1.messages) {@@ -197,6 +199,8 @@ const response2 = await client.agents.messages.create(agent2.id, {content: `Find information about the origin of Letta.Insert what you learn into the memory block, prepended with "Agent2: ".`}]+}, {+ timeoutInSeconds: 120 // Web search can take time});for (const msg of response2.messages) {@@ -341,74 +345,82 @@ Here's the full code in one place that you can run:```typescript TypeScriptimport { LettaClient } from '@letta-ai/letta-client';-// Initialize client-const client = new LettaClient({ token: process.env.LETTA_API_KEY });--// Create shared block-const block = await client.blocks.create({- label: "organization",- value: "Organization: Letta",- limit: 4000,-});--console.log(`Created shared block: ${block.id}\n`);--// Create agents with shared block-const agent1 = await client.agents.create({- name: "agent1",- model: "openai/gpt-4o-mini",- blockIds: [block.id],- tools: ["web_search"],-});--const agent2 = await client.agents.create({- name: "agent2",- model: "openai/gpt-4o-mini",- tools: ["web_search"],-});--await client.agents.blocks.attach(agent2.id, { blockId: block.id });--console.log(`Created agents: ${agent1.id}, ${agent2.id}\n`);--// Agent1 contributes information-const response1 = await client.agents.messages.create(agent1.id, {- messages: [{- role: "user",- content: `Find information about the connection between memory blocks and Letta.+async function main() {+ // Initialize client+ const client = new LettaClient({ token: process.env.LETTA_API_KEY });++ // Create shared block+ const block = await client.blocks.create({+ label: "organization",+ value: "Organization: Letta",+ limit: 4000,+ });++ console.log(`Created shared block: ${block.id}\n`);++ // Create agents with shared block+ const agent1 = await client.agents.create({+ name: "agent1",+ model: "openai/gpt-4o-mini",+ blockIds: [block.id],+ tools: ["web_search"],+ });++ const agent2 = await client.agents.create({+ name: "agent2",+ model: "openai/gpt-4o-mini",+ tools: ["web_search"],+ });++ await client.agents.blocks.attach(agent2.id, block.id);++ console.log(`Created agents: ${agent1.id}, ${agent2.id}\n`);++ // Agent1 contributes information+ const response1 = await client.agents.messages.create(agent1.id, {+ messages: [{+ role: "user",+ content: `Find information about the connection between memory blocks and Letta.Insert what you learn into the memory block, prepended with "Agent1: ".`- }]-});--// Agent2 contributes information-const response2 = await client.agents.messages.create(agent2.id, {- messages: [{- role: "user",- content: `Find information about the origin of Letta.+ }]+ }, {+ timeoutInSeconds: 120 // Web search can take time+ });++ // Agent2 contributes information+ const response2 = await client.agents.messages.create(agent2.id, {+ messages: [{+ role: "user",+ content: `Find information about the origin of Letta.Insert what you learn into the memory block, prepended with "Agent2: ".`- }]-});--// Inspect the shared memory-const updatedBlock = await client.blocks.retrieve(block.id);-console.log("==== Updated block ====");-console.log(updatedBlock.value);-console.log("=======================\n");--// Create read-only block-const readOnlyBlock = await client.blocks.create({- label: "policies",- value: "Company Policy: Always be helpful and respectful.",- readOnly: true,-});--const readOnlyAgent = await client.agents.create({- name: "policy_agent",- model: "openai/gpt-4o-mini",- blockIds: [readOnlyBlock.id],-});+ }]+ }, {+ timeoutInSeconds: 120 // Web search can take time+ });++ // Inspect the shared memory+ const updatedBlock = await client.blocks.retrieve(block.id);+ console.log("==== Updated block ====");+ console.log(updatedBlock.value);+ console.log("=======================\n");++ // Create read-only block+ const readOnlyBlock = await client.blocks.create({+ label: "policies",+ value: "Company Policy: Always be helpful and respectful.",+ readOnly: true,+ });++ const readOnlyAgent = await client.agents.create({+ name: "policy_agent",+ model: "openai/gpt-4o-mini",+ blockIds: [readOnlyBlock.id],+ });++ console.log(`Created read-only agent: ${readOnlyAgent.id}`);+}-console.log(`Created read-only agent: ${readOnlyAgent.id}`);+main();``````python Pythonfrom letta_client import Letta