AI
AI Usage
Generate text, stream responses, get structured output and create embeddings with the Vercel AI SDK and the VitNode model registry.
VitNode uses the Vercel AI SDK. Resolve a model from c.get("ai") and call the native SDK functions with it.
Building a feature users can trigger? Use an AI action
Calling generateText with c.get("ai").model() bypasses permissions,
budgets, AI points and run history. Nobody watches the bill until it arrives.
For user-facing features, define an AI action and run
it with c.get("ai").run(). Keep direct SDK calls for internal tooling you
fully control.
Model resolvers
| Resolver | Returned type | Used by |
|---|---|---|
c.get("ai").model(id?) | LanguageModel | generateText, streamText, generateObject |
c.get("ai").embeddingModel(id?) | EmbeddingModel | embed, embedMany |
c.get("ai").imageModel(id?) | ImageModel | generateImage |
1. Text generation
Generate text in a Hono API route:
import { buildRoute } from '@vitnode/core/api/lib/route'
import { generateText } from 'ai'
import { z } from 'zod'
export const summarizeRoute = buildRoute({
pluginId: 'blog',
route: {
method: 'post',
path: '/summarize',
request: {
body: {
content: {
'application/json': { schema: z.object({ text: z.string().min(1) }) },
},
},
},
responses: {
200: { description: 'Text summary' },
},
},
handler: async (c) => {
const { text } = c.req.valid('json')
const { text: summary } = await generateText({
model: c.get('ai').model(),
system: 'You are a concise summary assistant.',
prompt: `Summarize the following content:
${text}`,
})
return c.json({ summary })
},
})2. Streaming responses
Stream the model's answer to the client as it arrives:
import { streamText } from 'ai'
handler: async (c) => {
const result = streamText({
model: c.get('ai').model(),
prompt: 'Write an introduction to WebSockets.',
})
return result.toDataStreamResponse()
}3. Structured output (generateObject)
Get typed JSON back from the model, shaped by a Zod schema:
import { generateObject } from 'ai'
import { z } from 'zod'
const postMetadataSchema = z.object({
title: z.string(),
tags: z.array(z.string()),
estimatedReadingMinutes: z.number(),
})
const { object } = await generateObject({
model: c.get('ai').model(),
schema: postMetadataSchema,
prompt: 'Generate SEO metadata for an article on Postgres indexing.',
})4. Generating embeddings
Turn text into a vector embedding for semantic search:
import { embed } from 'ai'
const { embedding } = await embed({
model: c.get('ai').embeddingModel(),
value: 'How to configure Redis caching in VitNode',
})