### Build and Run Application (Bash)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/test-apps/letta-ai-sdk-example/README.md
Steps to build the Letta AI SDK Provider and then run the example application. This involves navigating directories, running build commands, and starting the development server.
```bash
cd ../..
npm run build
cd test-apps/letta-ai-sdk-example
npm run dev
```
--------------------------------
### Install and Configure Letta Provider
Source: https://context7.com/letta-ai/vercel-ai-sdk-provider/llms.txt
Installation command and environment variable setup for Letta Cloud or local development instances.
```bash
npm install @letta-ai/vercel-ai-sdk-provider
# For Letta Cloud (required)
export LETTA_API_KEY=your-letta-api-key
export LETTA_BASE_URL=https://api.letta.com
# For local Letta instance (no API key required)
export LETTA_BASE_URL=http://localhost:8283
```
--------------------------------
### Letta Provider Setup
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Guides on setting up the Letta AI provider for both cloud and local instances, including environment variable configurations.
```APIDOC
## Letta Cloud Setup
### Description
Configure the Letta AI provider for cloud usage. This requires setting the `LETTA_API_KEY` environment variable.
### Method
N/A (Configuration)
### Endpoint
N/A
### Parameters
#### Environment Variables
- **LETTA_API_KEY** (string) - Required - Your Letta API key.
- **LETTA_BASE_URL** (string) - Optional - The base URL for Letta API. Defaults to `https://api.letta.com`.
### Request Example
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
// Requires LETTA_API_KEY environment variable
const model = lettaCloud();
```
## Local Letta Instance Setup
### Description
Configure the Letta AI provider for local development. Local instances do not require an API key and typically run on `http://localhost:8283`.
### Method
N/A (Configuration)
### Endpoint
N/A
### Parameters
#### Environment Variables
- **LETTA_BASE_URL** (string) - Optional - The base URL for the local Letta instance. Defaults to `http://localhost:8283`.
### Request Example
```typescript
import { lettaLocal } from '@letta-ai/vercel-ai-sdk-provider';
// Works without LETTA_API_KEY for local development
const model = lettaLocal();
```
### Custom Local URL Configuration
Set the `LETTA_BASE_URL` environment variable:
```bash
# .env file
LETTA_BASE_URL=http://localhost:8283
```
Or export directly:
```bash
export LETTA_BASE_URL=http://localhost:8283
```
## Custom Letta Configuration
### Description
Provides flexibility to configure the Letta client with a custom base URL and token for both cloud and remote instances.
### Method
N/A (Configuration)
### Endpoint
N/A
### Parameters
#### Configuration Object
- **baseUrl** (string) - Required - The custom base URL for the Letta endpoint.
- **token** (string) - Required - Your access token for authentication.
### Request Example
```typescript
import { createLetta } from '@letta-ai/vercel-ai-sdk-provider';
const letta = createLetta({
baseUrl: 'https://your-custom-letta-endpoint.com',
token: 'your-access-token'
});
const model = letta();
```
```
--------------------------------
### Example Application Management
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/agents/docs/WARP.md
Commands to navigate to and run the example application for testing the provider integration.
```bash
cd test-apps/letta-ai-sdk-example
npm install
npm run dev
npm run build
```
--------------------------------
### Install Letta Vercel AI SDK Provider
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Command to install the necessary npm package for the Letta provider.
```bash
npm install @letta-ai/vercel-ai-sdk-provider
```
--------------------------------
### Environment Variables Setup (Bash)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/test-apps/letta-ai-sdk-example/README.md
Configuration of environment variables for the Letta AI SDK Provider, including API keys, agent IDs, test modes, and base URL overrides. Cloud mode requires an API key, while local mode does not.
```bash
# Required for cloud mode: Your Letta API token
LETTA_API_KEY=your-letta-api-token
# Required: Your Letta agent ID
LETTA_AGENT_ID=your-agent-id
# Optional: Test mode (local or cloud)
TEST_MODE=cloud # Use "local" for local development (no API key required)
# Optional: Custom base URL for local development
BASE_URL_OVERRIDE=http://localhost:8283
```
--------------------------------
### Install Dependencies (Bash)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/test-apps/letta-ai-sdk-example/README.md
Command to install project dependencies using npm. This is a standard step before building or running the application.
```bash
npm install
```
--------------------------------
### Working with Letta Agents
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Guides on creating new Letta agents and using existing messages from Letta agents within the Vercel AI SDK.
```APIDOC
## Creating a New Letta Agent
### Description
This section details how to create a new agent using the Letta client, specifying its name, model, and embedding configuration.
### Method
POST
### Endpoint
`/api/agents` (Conceptual, actual endpoint handled by `@letta-ai/letta-client`)
### Parameters
#### Request Body (for `client.agents.create`)
- **name** (string) - Required - The name of the agent.
- **model** (string) - Required - The model to be used by the agent (e.g., `openai/gpt-4o-mini`).
- **embedding** (string) - Optional - The embedding model to be used by the agent (e.g., `openai/text-embedding-3-small`).
### Request Example
```typescript
import { LettaClient } from "@letta-ai/letta-client";
const client = new LettaClient({
token: process.env.LETTA_API_KEY,
project: "your-project-id" // optional param
});
const agent = await client.agents.create({
name: 'My Assistant',
model: 'openai/gpt-4o-mini',
embedding: 'openai/text-embedding-3-small'
});
console.log('Created agent:', agent.id);
```
### Response
#### Success Response (200)
- **id** (string) - The unique identifier of the created agent.
- **name** (string) - The name of the agent.
- **model** (string) - The model used by the agent.
- **embedding** (string) - The embedding model used by the agent.
#### Response Example
```json
{
"id": "agent-12345",
"name": "My Assistant",
"model": "openai/gpt-4o-mini",
"embedding": "openai/text-embedding-3-small"
}
```
## Using Existing Messages from Letta Agent
### Description
Demonstrates how to retrieve messages from a Letta agent and convert them into the AI SDK message format for use in the Vercel AI SDK.
### Method
GET (Conceptual, actual method handled by `client.agents.getMessages`)
### Endpoint
`/api/agents/{agentId}/messages` (Conceptual)
### Parameters
#### Path Parameters
- **agentId** (string) - Required - The ID of the Letta agent.
### Request Example
```typescript
import { convertToAiSdkMessage } from '@letta-ai/vercel-ai-sdk-provider';
import { LettaClient } from "@letta-ai/letta-client";
const client = new LettaClient({ token: process.env.LETTA_API_KEY });
const agentId = 'your-agent-id';
// Load messages from Letta agent
const lettaMessages = await client.agents.getMessages(agentId);
// Convert to AI SDK format
const uiMessages = convertToAiSdkMessage(lettaMessages);
console.log(uiMessages);
```
### Response
#### Success Response (200)
- **lettaMessages** (array) - An array of message objects in Letta's format.
#### Response Example
```json
[
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there! How can I help you today?"}
]
```
```
--------------------------------
### Page Setup for Streaming Chat with Letta AI
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Configures the main page to display a streaming chat interface with a Letta AI agent. It loads existing messages and passes the agent ID to the Chat component. Requires environment variables `LETTA_AGENT_ID` and `LETTA_API_KEY`.
```typescript
// app/page.tsx - Streaming chat page
import { LettaClient } from '@letta-ai/letta-client';
import { convertToAiSdkMessage } from '@letta-ai/vercel-ai-sdk-provider';
import { Chat } from './Chat';
export default async function HomePage() {
const agentId = process.env.LETTA_AGENT_ID;
if (!agentId) {
throw new Error('LETTA_AGENT_ID environment variable is required');
}
// Load existing messages
const client = new LettaClient({
token: process.env.LETTA_API_KEY
});
const lettaMessages = await client.agents.getMessages(agentId);
const existingMessages = convertToAiSdkMessage(lettaMessages);
return (
Streaming Chat with Letta Agent
);
}
```
--------------------------------
### Define Tool Placeholders
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/agents/docs/WARP.md
Example of defining tools for the AI SDK that act as placeholders for Letta's backend-executed tools.
```typescript
const tools = {
web_search: lettaCloud.tool("web_search"),
memory_insert: lettaCloud.tool("memory_insert", {
description: "Insert into agent memory",
inputSchema: z.object({ content: z.string() })
})
};
```
--------------------------------
### Implement Next.js Streaming API Route
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Provides an example of a Next.js API route handler for real-time streaming with Letta agents.
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { streamText, convertToModelMessages } from 'ai';
export async function POST(req: Request) {
const { messages, agentId } = await req.json();
const result = streamText({
model: lettaCloud(),
providerOptions: { letta: { agent: { id: agentId } } },
messages: convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse({ sendReasoning: true });
}
```
--------------------------------
### AI SDK v5 Message Part Handling (Tools and Files)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Provides utility functions and examples for handling new message part structures introduced in AI SDK v5. This includes identifying and extracting information from tool invocation parts and file attachments within UI messages.
```typescript
// Tool parts (v5): type is "tool-"
const isToolPart = (part: { type: string }): boolean => part.type.startsWith('tool-');
// File parts (v5): standard file attachment with URL and media type
const isFilePart = (part: any): part is { type: 'file'; url: string; mediaType: string } => part?.type === 'file' && typeof part.url === 'string';
message.parts?.forEach((part) => {
if (isToolPart(part)) {
// state can be: 'input-available' | 'output-available' | 'output-error'
if ('toolCallId' in part) console.log('Tool call:', part.toolCallId);
if ('input' in part && part.input != null) console.log('Input:', part.input);
if ('output' in part && part.output != null) console.log('Output:', part.output);
if ('errorText' in part && part.errorText) console.error('Tool Error:', part.errorText);
}
if (isFilePart(part)) {
console.log('File URL:', part.url, 'Media Type:', part.mediaType);
}
});
```
--------------------------------
### Initialize Letta Provider Instances
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Shows how to initialize the Letta provider for cloud, local, or custom endpoints.
```typescript
import { lettaCloud, lettaLocal, createLetta } from '@letta-ai/vercel-ai-sdk-provider';
// Cloud setup
const cloudModel = lettaCloud();
// Local setup
const localModel = lettaLocal();
// Custom setup
const customLetta = createLetta({
baseUrl: 'https://your-custom-letta-endpoint.com',
token: 'your-access-token'
});
```
--------------------------------
### lettaCloud() Provider Initialization
Source: https://context7.com/letta-ai/vercel-ai-sdk-provider/llms.txt
Initializes a pre-configured provider instance for Letta Cloud, requiring the LETTA_API_KEY environment variable.
```APIDOC
## lettaCloud()
### Description
Creates a provider instance for Letta Cloud. It automatically uses the LETTA_API_KEY environment variable for authentication.
### Method
N/A (Factory Function)
### Parameters
- **None**
### Request Example
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { generateText } from 'ai';
const result = await generateText({
model: lettaCloud(),
providerOptions: {
letta: { agent: { id: 'agent-abc123' } }
},
prompt: 'Write a recipe.',
});
```
```
--------------------------------
### Build and Test Commands
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/agents/docs/WARP.md
Standard npm scripts for managing the lifecycle of the Letta provider, including building, testing, and linting.
```bash
npm run build
npm run build:watch
npm run test
npm run test:node
npm run test:node:watch
npm run test:e2e
npm run test:e2e:local
npm run type-check
npm run lint
npm run prettier-check
```
--------------------------------
### Create and Use Letta Agents with AI SDK
Source: https://context7.com/letta-ai/vercel-ai-sdk-provider/llms.txt
Demonstrates how to create a new agent using the Letta client and then integrate it with the Vercel AI SDK's `generateText` function. This involves initializing the Letta client, creating an agent with specified model and embedding, and then using the agent's ID within the `providerOptions` for the AI SDK.
```typescript
import { LettaClient } from "@letta-ai/letta-client";
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { generateText } from 'ai';
const client = new LettaClient({
token: process.env.LETTA_API_KEY,
project: "your-project-id" // optional
});
// Create a new agent
const agent = await client.agents.create({
name: 'My Assistant',
model: 'openai/gpt-4o-mini',
embedding: 'openai/text-embedding-3-small'
});
console.log('Created agent:', agent.id);
// List existing agents
const agents = await client.agents.list();
console.log('Available agents:', agents.map(a => ({ id: a.id, name: a.name })));
// Use the new agent with AI SDK
const result = await generateText({
model: lettaCloud(),
providerOptions: {
letta: { agent: { id: agent.id } }
},
prompt: 'Hello, I just created you!',
});
console.log(result.text);
```
--------------------------------
### Environment Configuration
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/agents/docs/WARP.md
Configuration variables for setting up the provider in either Cloud or Local modes.
```bash
# Cloud Mode
LETTA_API_KEY=your-api-key
LETTA_AGENT_ID=your-agent-id
TEST_MODE=cloud
# Local Mode
LETTA_AGENT_ID=your-local-agent-id
TEST_MODE=local
BASE_URL_OVERRIDE=http://localhost:8283
```
--------------------------------
### Initialize Letta Cloud Provider
Source: https://context7.com/letta-ai/vercel-ai-sdk-provider/llms.txt
Configures the provider for Letta Cloud usage. Requires the LETTA_API_KEY environment variable to be set.
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { generateText } from 'ai';
const result = await generateText({
model: lettaCloud(),
providerOptions: {
letta: {
agent: { id: 'agent-abc123' }
}
},
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(result.text);
```
--------------------------------
### List Available Agents with Letta Client (TypeScript)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Demonstrates how to list available agents using the Letta client in TypeScript. It requires the `LETTA_API_KEY` environment variable for authentication. The output is a list of agent IDs and names.
```typescript
import {
LettaClient
} from '@letta-ai/letta-client';
const client = new LettaClient({ token: process.env.LETTA_API_KEY });
const agents = await client.agents.list();
console.log('Available agents:', agents.map(a => ({ id: a.id, name: a.name })));
```
--------------------------------
### Local Development Configuration
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/test-apps/letta-ai-sdk-example/README.md
Illustrates how to configure the Letta provider for local development using lettaLocal, which connects to a local instance without requiring an API key.
```typescript
import { lettaLocal } from '@letta-ai/vercel-ai-sdk-provider';
import { streamText } from 'ai';
const result = streamText({
model: lettaLocal(),
tools: {
memory_insert: lettaLocal.tool("memory_insert"),
memory_replace: lettaLocal.tool("memory_replace"),
},
providerOptions: {
letta: {
agent: { id: 'local-agent-123' }
}
},
prompt: 'Hello!',
});
```
--------------------------------
### Configure Local Development Environment (Bash)
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Sets the `LETTA_BASE_URL` environment variable for local development. This is useful when running a local instance of Letta instead of using the cloud service.
```bash
# Set environment for local development
LETTA_BASE_URL=http://localhost:8283
```
--------------------------------
### Streaming Chat Implementation with streamText
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/test-apps/letta-ai-sdk-example/README.md
Demonstrates how to initialize the Letta provider and use the streamText function to handle real-time streaming chat responses. It includes tool definitions and provider-specific agent configuration.
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { streamText } from 'ai';
const result = streamText({
model: lettaCloud(),
tools: {
memory_insert: lettaCloud.tool("memory_insert"),
memory_replace: lettaCloud.tool("memory_replace"),
},
providerOptions: {
letta: {
agent: { id: 'your-agent-id' }
}
},
prompt: 'Hello!',
});
return result.toUIMessageStreamResponse({
sendReasoning: true,
});
```
--------------------------------
### createLetta() Custom Configuration
Source: https://context7.com/letta-ai/vercel-ai-sdk-provider/llms.txt
Factory function to create a custom Letta provider instance with specific base URLs and authentication tokens.
```APIDOC
## createLetta()
### Description
Allows manual configuration of the Letta provider, useful for custom deployments or specific authentication requirements.
### Parameters
#### Options
- **baseUrl** (string) - Optional - The base URL of the Letta instance.
- **token** (string) - Optional - The authentication token for the Letta API.
### Request Example
```typescript
import { createLetta } from '@letta-ai/vercel-ai-sdk-provider';
const letta = createLetta({
baseUrl: 'https://your-custom-letta-endpoint.com',
token: 'your-access-token'
});
```
```
--------------------------------
### Configure Letta Streaming with Agent Options
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Demonstrates how to use streamText with Letta-specific provider options, including agent ID, max steps, and timeout configuration.
```typescript
import { lettaCloud } from '@letta-ai/vercel-ai-sdk-provider';
import { streamText } from 'ai';
const result = streamText({
model: lettaCloud(),
providerOptions: {
letta: {
agent: {
id: 'your-agent-id',
maxSteps: 100,
background: true,
includePings: true,
},
timeoutInSeconds: 300
}
},
prompt: 'Tell me a story about a robot learning to paint.',
});
for await (const textPart of result.textStream) {
console.log(textPart);
}
```
--------------------------------
### Controlling Agent Execution Steps
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Demonstrates the correct way to limit agent execution steps using maxSteps within providerOptions, rather than relying on client-side stop conditions.
```typescript
const result = await generateText({
model: lettaCloud(),
prompt: 'Help me with a task',
providerOptions: {
letta: {
agent: {
id: 'your-agent-id',
maxSteps: 5
}
}
},
});
```
--------------------------------
### Reasoning Support: Streaming and Non-Streaming
Source: https://github.com/letta-ai/vercel-ai-sdk-provider/blob/main/README.md
Explains how to utilize AI reasoning tokens with both streaming and non-streaming responses from Letta agents.
```APIDOC
## Reasoning Support
### Description
This section details how to incorporate AI reasoning tokens into your application using both streaming and non-streaming methods with the Letta AI Provider SDK. It covers how to access and distinguish between model reasoning and agent reasoning.
### Method
`streamText`, `generateText` (from `ai` SDK)
### Endpoint
N/A (Client-side SDK usage)
### Parameters
#### Request Body (Implicit via function arguments)
- **model**: `lettaCloud()` - Specifies the Letta AI model.
- **providerOptions.letta.agent.id**: (string) - Required - The unique identifier for the Letta agent.
- **messages**: (Array