Loading...
Loading...
Build applications with the Letta API — a model-agnostic, stateful API for building persistent agents with memory and long-term learning. Covers SDK patterns for Python and TypeScript. Includes 24 working code examples.
npx skill4agent add letta-ai/skills letta-api-clientletta-client==1.7.1@letta-ai/letta-client@1.7.1type: "tool"clientos.getenv()LETTA_AGENT_IDinclude_pingsenable_sleeptime=True.afInitToolRuleChildToolRuleTerminalToolRulepip install letta-clientfrom letta_client import Letta
# Cloud
client = Letta(api_key="LETTA_API_KEY")
# Self-hosted
client = Letta(base_url="http://localhost:8283")npm install @letta-ai/letta-clientimport { Letta } from "@letta-ai/letta-client";
// Cloud
const client = new Letta({ apiKey: process.env.LETTA_API_KEY });
// Self-hosted
const client = new Letta({ baseUrl: "http://localhost:8283" });examples/01_basic_client.py02_create_agent.py03_custom_tool_simple.py04_custom_tool_secrets.py05_send_message.py06_send_message_stream.py07_multi_user.py08_archival_memory.py09_shared_blocks.py10_conversations.py11_client_injection.py12_tool_rules.py13_client_side_tools.py01_basic_client.ts02_create_agent.ts03_send_message.ts04_send_message_stream.ts05_nextjs_singleton.ts06_multi_user.ts07_conversations.ts08_custom_tool.ts09_archival_memory.ts10_shared_blocks.ts11_client_injection.ts12_tool_rules.ts13_client_side_tools.ts| Error | Cause | Fix |
|---|---|---|
| 401 Unauthorized | Invalid or missing API key | Check |
| 422 Validation Error | Missing required field | Add |
| Tool not found | Tool not attached to agent | |
| Secret not configured | Add to agent via |
| 524 Timeout | Long operation without pings | Add |
| Agent not responding | Model issue or empty response | Check for |
| Memory block not updating | Looking at wrong agent | Verify |
| Import error in tool | Top-level import | Move imports inside function body |
os.getenv()clientLetta()include_pings=True.update().modify()LETTA_AGENT_IDinclude_base_tools=Truememory_insert// Client initialization uses baseURL (not baseUrl)
const client = new Letta({ apiKey: "...", baseURL: "http://localhost:8283" });
// Block API: positional args changed
client.agents.blocks.attach(blockId, { agent_id }); // blockId is first
client.agents.blocks.retrieve(blockLabel, { agent_id }); // label is first
// Passages.create returns array
const passages = await client.agents.passages.create(agentId, { text: "..." });
const passage = passages[0];
// Content can be string | array - use type guard
const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
// Conversations API returns streams by default
const stream = await client.conversations.messages.create(convId, { messages: [...] });
for await (const chunk of stream) { ... }
// Tool rule types
{ type: "run_first", tool_name: "..." } // InitToolRule
{ type: "constrain_child_tools", tool_name: "...", children: [...] } // ChildToolRule
{ type: "exit_loop", tool_name: "..." } // TerminalToolRule# Client
client = Letta(api_key=os.getenv("LETTA_API_KEY"))
# Create agent
agent = client.agents.create(
model="anthropic/claude-sonnet-4-5-20250929",
embedding="openai/text-embedding-3-small",
memory_blocks=[{"label": "persona", "value": "..."}],
include_base_tools=True, # archival memory tools
enable_sleeptime=True, # background memory processing
)
# Send message
response = client.agents.messages.create(
agent_id=agent.id,
messages=[{"role": "user", "content": "Hello"}]
)
# Stream response
stream = client.agents.messages.stream(
agent_id=agent.id,
messages=[{"role": "user", "content": "Hello"}],
stream_tokens=True,
include_pings=True, # prevent timeout
)
# Create tool
tool = client.tools.create(source_code="def my_tool(x: str) -> str: ...")
client.agents.tools.attach(agent_id=agent.id, tool_id=tool.id)
# Memory blocks
client.agents.blocks.retrieve(agent_id=agent.id, block_label="persona")
client.agents.blocks.update(agent_id=agent.id, block_label="persona", value="...")
# Folders
folder = client.folders.create(name="docs")
client.folders.files.upload(file=f, folder_id=folder.id)
client.agents.folders.attach(agent_id=agent.id, folder_id=folder.id)
# Conversations (parallel sessions)
conv = client.conversations.create(agent_id=agent.id)
stream = client.conversations.messages.create(conv.id, messages=[...])
# Agent secrets (for tools)
client.agents.update(agent_id=agent.id, secrets={"API_KEY": "..."})pip install letta-clientnpm install @letta-ai/letta-client