Add an Agent that greets a name and returns JSON. An Agent Definition declares the work and its Driver runs it. Here the Driver is a plain function, so it runs offline with no model account or credentials.
Before you start
Section titled "Before you start"Complete Installation, check /api/health, and continue in that app with Node.js 24.15 or newer and pnpm.
Prefer to skip the setup? Run this tutorial in your browser with a live editor and preview.
Enable Agents
Section titled "Enable Agents"Install the Agent package:
pnpm add @vite-hub/agentThe file you create next in server/agents/ turns Agents on. You do not need to set agent or workflow for this inline Agent. Keep your other ViteHub options.
export default defineNuxtConfig({
modules: ['vite-hub/nuxt'],
})import { defineConfig } from "vite"
import { nitro } from 'nitro/vite'
import { vitehub } from "vite-hub"
export default defineConfig({
plugins: [
vitehub(),
nitro(),
],
})import { defineConfig } from "vite"
import { vitehub } from "vite-hub"
export default defineConfig({
plugins: [vitehub()],
})Define the greeting Agent
Section titled "Define the greeting Agent"Create server/agents/greeting.ts. ViteHub names the Agent greeting from its file path. runtime: false runs it inline. The function Driver gets the name as prompt and returns text. In a chat or vitehub agent dev, the Driver gets the user message text as prompt.
import { defineAgent } from "vite-hub/agent"
export default defineAgent({
runtime: false,
driver: {
run({ prompt }) {
const name = typeof prompt === "string" ? prompt : "friend"
return {
text: `Hello, ${name}. This result came from an Agent Invocation.`,
}
},
},
})Call the Agent from a route
Section titled "Call the Agent from a route"Create server/api/greet.post.ts. Nitro and Nuxt serve it at POST /api/greet. The explicit H3 imports work without auto-imports.
import { defineEventHandler, readBody } from "h3"
import { runAgent } from "vite-hub/agent"
import greeting from "../agents/greeting"
export default defineEventHandler(async (event) => {
const body = await readBody<{ name?: string }>(event)
const [error, result] = await runAgent(greeting, {
prompt: body?.name?.trim() || "friend",
})
if (error) throw error
return { text: result.text }
})runAgent() creates a memo cache, waits for background work to finish, and returns [undefined, result] on success or [error, undefined] on failure. Check error before reading result.text.
When a run needs request metadata or host bindings, pass the public getRuntimeContext(event) adapter as runAgent(agent, input, { runtimeContext: getRuntimeContext(event) }). This greeting needs only prompt.
Run the Agent
Section titled "Run the Agent"Restart the app to load the new configuration:
pnpm devIn another terminal, send a name:
curl -X POST http://localhost:3000/api/greet \
-H 'content-type: application/json' \
-d '{"name":"Ada"}'The route returns this JSON. It proves that the route called the function Driver without a model request:
{"text":"Hello, Ada. This result came from an Agent Invocation."}Sending {} greets friend.
To check the production server of the Vite app, stop pnpm dev, build and start the server, and repeat the request:
pnpm build
pnpm startIn Nuxt, run pnpm build and node .output/server/index.mjs instead.
Continue with a model
Section titled "Continue with a model"A model Driver needs network access and a billed provider account. To use a model string through Vercel AI Gateway, install ai and set AI_GATEWAY_API_KEY in your server environment. Keep the key out of client code and version control. Each request can cost money.
For model output, replace driver.run with driver.model and driver.instructions. Agent Drivers shows the supported forms. Model replies vary, so the exact greeting above works only as the offline check.
Build a chat app adds a streaming chat UI with an offline Driver and an optional model Driver. Read Capabilities when your Agent needs tools or stored data.
Related guides
Section titled "Related guides"Deploy an Agent on Cloudflare and Vercel checks the same inline Agent on two hosts. Choose a TypeScript Agent framework compares execution and state models.