|
| 1 | +--- |
| 2 | +title: "AI Agents" |
| 3 | +sidebar_position: 100 |
| 4 | +description: "Building AI Agents with Convex" |
| 5 | +--- |
| 6 | + |
| 7 | +# Building AI Agents with Convex |
| 8 | + |
| 9 | +Convex provides a powerful platform for building AI agents through its robust |
| 10 | +set of components. |
| 11 | + |
| 12 | +## Why Convex for AI Agents? |
| 13 | + |
| 14 | +Convex offers several advantages for building AI agents: |
| 15 | + |
| 16 | +1. **Durable Execution**: Long-running workflows that survive server restarts |
| 17 | +2. **Real-time State Management**: Reactive state updates for agent progress |
| 18 | +3. **Built-in Persistence**: Store conversation history and agent state |
| 19 | +4. **Parallel Processing**: Run multiple agent tasks concurrently |
| 20 | +5. **Error Handling**: Robust retry mechanisms for API calls |
| 21 | + |
| 22 | +## Core Components |
| 23 | + |
| 24 | +The [Agent](https://www.convex.dev/components/agent) and |
| 25 | +[Workflow](https://www.convex.dev/components/workflow) components can be used |
| 26 | +together to create powerful long running agents with memory. |
| 27 | + |
| 28 | +Learn more by reading: |
| 29 | +[AI Agents with Built-in Memory](https://stack.convex.dev/ai-agents). |
| 30 | + |
| 31 | +Sample code: |
| 32 | + |
| 33 | +```typescript |
| 34 | +// Define an agent similarly to the AI SDK |
| 35 | +const supportAgent = new Agent(components.agent, { |
| 36 | + chat: openai.chat("gpt-4o-mini"), |
| 37 | + textEmbedding: openai.embedding("text-embedding-3-small"), |
| 38 | + instructions: "You are a helpful assistant.", |
| 39 | + tools: { accountLookup, fileTicket, sendEmail }, |
| 40 | +}); |
| 41 | + |
| 42 | +// Use the agent from within a normal action: |
| 43 | +export const createThread = action({ |
| 44 | + args: { prompt: v.string() }, |
| 45 | + handler: async (ctx, { prompt }) => { |
| 46 | + const { threadId, thread } = await supportAgent.createThread(ctx); |
| 47 | + const result = await thread.generateText({ prompt }); |
| 48 | + return { threadId, text: result.text }; |
| 49 | + }, |
| 50 | +}); |
| 51 | + |
| 52 | +// Pick up where you left off, with the same or a different agent: |
| 53 | +export const continueThread = action({ |
| 54 | + args: { prompt: v.string(), threadId: v.string() }, |
| 55 | + handler: async (ctx, { prompt, threadId }) => { |
| 56 | + // This includes previous message history from the thread automatically. |
| 57 | + const { thread } = await anotherAgent.continueThread(ctx, { threadId }); |
| 58 | + const result = await thread.generateText({ prompt }); |
| 59 | + return result.text; |
| 60 | + }, |
| 61 | +}); |
| 62 | + |
| 63 | +// Or use it within a workflow, specific to a user: |
| 64 | +export const supportAgentStep = supportAgent.asAction({ maxSteps: 10 }); |
| 65 | + |
| 66 | +const workflow = new WorkflowManager(components.workflow); |
| 67 | +const s = internal.example; // where steps are defined |
| 68 | + |
| 69 | +export const supportAgentWorkflow = workflow.define({ |
| 70 | + args: { prompt: v.string(), userId: v.string(), threadId: v.string() }, |
| 71 | + handler: async (step, { prompt, userId, threadId }) => { |
| 72 | + const suggestion = await step.runAction(s.supportAgentStep, { |
| 73 | + threadId, |
| 74 | + generateText: { prompt }, |
| 75 | + }); |
| 76 | + const polished = await step.runAction(s.adaptSuggestionForUser, { |
| 77 | + suggestion, |
| 78 | + userId, |
| 79 | + }); |
| 80 | + await step.runMutation(s.sendUserMessage, { |
| 81 | + userId, |
| 82 | + message: polished.message, |
| 83 | + }); |
| 84 | + }, |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +## Other Components |
| 89 | + |
| 90 | +Convex also provides other components to help you build reliable AI |
| 91 | +applications. |
| 92 | + |
| 93 | +The |
| 94 | +[Persistent Text Streaming](https://www.convex.dev/components/persistent-text-streaming) |
| 95 | +component provides a React hook for streaming text from HTTP actions while |
| 96 | +simultaneously storing the data in the database. This persistence allows the |
| 97 | +text to be accessed after the stream ends or by other users. |
| 98 | + |
| 99 | +The [Action Retrier](https://www.convex.dev/components/retrier) Component is |
| 100 | +useful for one of flaky AI calls that you want to automatically retry. It will |
| 101 | +run the action and retry it on failure, sleeping with exponential backoff, until |
| 102 | +the action succeeds or the maximum number of retries is reached. |
| 103 | + |
| 104 | +The [Workpool](https://www.convex.dev/components/workpool) Component lets you |
| 105 | +create tiers of parallelism to handle large number of external requests. |
0 commit comments