Building Your First Agent
Let's build a simple research agent that can search the web and summarize findings.
Prerequisites
- Node.js 18+
- OpenAI API key
- Basic TypeScript knowledge
Step 1: Setup
npm install ai @ai-sdk/openai
Step 2: Define Tools
const tools = {
searchWeb: {
description: 'Search the web for information',
parameters: z.object({
query: z.string(),
}),
execute: async ({ query }) => {
// Implementation
return results
},
},
}
Step 3: Create the Agent
import { generateText } from 'ai'
const result = await generateText({
model: 'openai/gpt-4.1',
prompt: 'Research the latest developments in quantum computing',
tools,
maxSteps: 5,
})
What's Happening?
The agent will:
- Analyze the goal
- Decide to use the searchWeb tool
- Execute searches
- Synthesize findings
- Return a comprehensive summary
This is your first autonomous agent!