andAgent
Add AI to your workflow. Get structured, typed responses from language models.
Quick Start
import { createWorkflowChain, Agent } from "@voltagent/core";
import { z } from "zod";
import { openai } from "@ai-sdk/openai";
// Create an agent
const agent = new Agent({
name: "Assistant",
// Pass an ai-sdk model directly
model: openai("gpt-4o-mini"),
instructions: "Be concise and helpful",
});
// Use it in a workflow
const workflow = createWorkflowChain({
id: "analyze-text",
input: z.object({ text: z.string() }),
}).andAgent(({ data }) => `Analyze this text: ${data.text}`, agent, {
schema: z.object({
sentiment: z.enum(["positive", "negative", "neutral"]),
summary: z.string(),
}),
});
const result = await workflow.run({ text: "I love this!" });
// Result: { sentiment: "positive", summary: "Expression of enthusiasm" }
How It Works
andAgent = AI prompt + structured output schema:
.andAgent(
prompt, // What to ask the AI
agent, // Which AI to use
{ schema }, // What shape the answer should be
map? // Optional: merge/shape output with existing data
)
Important: andAgent uses generateText with Output.object under the hood, which means:
- ✅ You get structured, typed responses based on your schema
- ✅ The agent can use tools during this step
- ❌ Streaming is not supported (response returns when complete)
By default, the step result replaces the workflow data with the agent output. If you need to keep previous data, use the optional mapper (4th argument) to merge or reshape the output.
Need streaming or custom tool handling? Use andThen to call the agent directly with streamText or generateText.
Function Signature
// Simple prompt (string)
.andAgent("Summarize this", agent, { schema })
// Dynamic prompt from data (string)
.andAgent(({ data }) => `Analyze: ${data.text}`, agent, { schema })
// Advanced: pass ai-sdk v5 ModelMessage[] (multimodal)
.andAgent(
({ data }) => [
{ role: 'user', content: [{ type: 'text', text: `Hello ${data.name}` }] },
],
agent,
{ schema }
)
// Advanced: pass UIMessage[]
.andAgent(
({ data }) => [
{ id: crypto.randomUUID(), role: 'user', parts: [{ type: 'text', text: data.prompt }] },
],
agent,
{ schema }
)
// Merge agent output with existing data
.andAgent(
({ data }) => `Classify: ${data.email}`,
agent,
{ schema: z.object({ type: z.enum(["support", "sales", "spam"]) }) },
(output, { data }) => ({ ...data, emailType: output })
)
Common Patterns
Text Analysis
.andAgent(
({ data }) => `Analyze sentiment of: ${data.review}`,
agent,
{
schema: z.object({
sentiment: z.enum(["positive", "negative", "neutral"]),
score: z.number().min(0).max(1),
keywords: z.array(z.string())
})
}
)
Content Generation
.andAgent(
({ data }) => `Write a ${data.tone} email about ${data.topic}`,
agent,
{
schema: z.object({
subject: z.string(),
body: z.string(),
suggestedSendTime: z.string()
})
}
)
Data Extraction
.andAgent(
({ data }) => `Extract key information from: ${data.document}`,
agent,
{
schema: z.object({
people: z.array(z.string()),
dates: z.array(z.string()),
locations: z.array(z.string()),
mainTopic: z.string()
})
}
)