Integrating VoltAgent with Next.js
This guide walks you through setting up VoltAgent in a Next.js application. We'll build a simple AI calculator example using Server Actions and the Vercel AI SDK.
Quick Start with Example
If you prefer to start directly with the completed example project, you can create it using the following command:
npm create voltagent-app@latest -- --example with-nextjs
This command will scaffold an entire Next.js example for you. You can also peek at the full source code right here: examples/with-nextjs.
Create a New Next.js Project
Start by creating a new Next.js project using the latest version:
npx create-next-app@latest my-voltagent-app
cd my-voltagent-app
Follow the prompts, selecting TypeScript and App Router.
Install VoltAgent Dependencies
Install the necessary VoltAgent packages and dependencies:
npm install @voltagent/core @ai-sdk/react ai zod@^3.25.76
@voltagent/core: The core VoltAgent library.@ai-sdk/react: React hooks for AI SDK integration.ai: Core AI SDK library for streaming and chat functionality.zod: Used when working with structured outputs.
VoltAgent Built-in Server Dependencies (Required for Debugging)
Install the VoltAgent built-in server dependencies (local REST API on http://localhost:3141) so you can
debug and inspect agent runs during development through https://console.voltagent.dev:
npm install @voltagent/server-hono
npm install -D tsx
Configure next.config.ts
Next.js might try to bundle server-side packages by default. To prevent issues with certain packages, you need to mark them as external in your next.config.ts file:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
serverExternalPackages: [
// Externalize only what's needed at runtime.
// LibSQL client is safe to externalize; native platform packages are optional.
"@libsql/client",
],
};
export default nextConfig;
Note: The property used to be experimental.serverComponentsExternalPackages in older Next.js versions, but is now serverExternalPackages. Ensure you use the correct one for your Next.js version.
Environment Variables
Set up your environment variables (e.g., OPENAI_API_KEY) in a .env.local file:
OPENAI_API_KEY="your-openai-api-key-here"
# Add other environment variables if needed
If you run the built-in server as a separate process, make sure it loads the same env file (see the server script below).
Define the Agent
Create the agent in voltagent/agents.ts:
import { Agent, createTool } from "@voltagent/core";
import { z } from "zod";
// Simple calculator tool
const calculatorTool = createTool({
name: "calculate",
description: "Perform basic mathematical calculations",
parameters: z.object({
expression: z
.string()
.describe("Mathematical expression to evaluate (e.g., '2 + 2', '10 * 5')"),
}),
execute: async (args) => {
try {
// Simple evaluation - in production, use a proper math parser
const result = eval(args.expression);
return { result, expression: args.expression };
} catch {
return { error: "Invalid mathematical expression", expression: args.expression };
}
},
});
export const agent = new Agent({
name: "CalculatorAgent",
instructions:
"You are a helpful calculator assistant. When users ask you to calculate something, use the calculate tool to perform the math and then explain the result clearly.",
model: "openai/gpt-4o-mini",
tools: [calculatorTool],
});
Re-export it from voltagent/index.ts:
export { agent } from "./agents";