Getting Started with Agent
To install the agent component, you'll need an existing Convex project. New to Convex? Go through the tutorial.
Run npm create convex
or follow any of the
quickstarts to set one up.
Installation
Install the component package:
npm install @convex-dev/agent
Create a convex.config.ts
file in your app's convex/
folder and install the
component by calling use
:
// convex/convex.config.ts
import { defineApp } from "convex/server";
import agent from "@convex-dev/agent/convex.config";
const app = defineApp();
app.use(agent);
export default app;
Then run npx convex dev
to generate code for the component. This needs to
successfully run once before you start defining Agents.
Defining your first Agent
import { components } from "./_generated/api";
import { Agent } from "@convex-dev/agent";
import { openai } from "@ai-sdk/openai";
const agent = new Agent(components.agent, {
name: "My Agent",
languageModel: openai.chat("gpt-4o-mini"),
instructions: "You are a weather forecaster.",
tools: { getWeather, getGeocoding },
maxSteps: 3,
});
Basic usage
import { action } from "./_generated/server";
import { v } from "convex/values";
export const helloWorld = action({
args: { city: v.string() },
handler: async (ctx, { city }) => {
const threadId = await createThread(ctx, components.agent);
const prompt = `What is the weather in ${city}?`;
const result = await agent.generateText(ctx, { threadId }, { prompt });
return result.text;
},
});
If you get type errors about components.agent
, ensure you've run
npx convex dev
to generate code for the component.
That's it! Check out Agent Usage to see more details and options.