Agent runs return RunResult or StreamResult, carrying the typed output, full message history, new messages, and token usage.
agent.run() returns a RunResult<TOutput> once the run completes. agent.stream() and agent.runStreamEvents() return a StreamResult<TOutput> immediately and resolve the final values lazily. Both carry the structured output, complete message history, new messages added in this run, and token usage.
Define a Zod schema and the agent will parse the model’s response and return it as a typed value.
import { Agent } from "@vibesjs/sdk";import { anthropic } from "@ai-sdk/anthropic";import { z } from "zod";const Schema = z.object({ answer: z.string(), confidence: z.number(),});const agent = new Agent<undefined, z.infer<typeof Schema>>({ model: anthropic("claude-sonnet-4-6"), outputSchema: Schema,});const result = await agent.run("What is the capital of France?");console.log(result.output.answer); // "Paris"console.log(result.output.confidence); // 0.99
result.output is fully typed as { answer: string; confidence: number }.
const agent = new Agent({ model, outputSchema: Schema, outputMode: "tool", // default - uses final_result tool // outputMode: "native", // provider JSON mode // outputMode: "prompted", // schema in system prompt outputTemplate: true, // default - injects schema description into system prompt});
outputTemplate is a boolean, not a string. Setting outputTemplate: true (the default) tells Vibes to inject the JSON schema description into the system prompt. You cannot customize the injected text.
Pass an array of Zod schemas to let the model choose which one applies. This is useful for agents that can return different structured shapes depending on the task.
resultValidators are post-parse validation functions that run after Vibes parses the output. Return the (optionally modified) output to accept it, or throw an error to reject it and trigger a retry.
const agent = new Agent<undefined, z.infer<typeof Schema>>({ model, outputSchema: Schema, maxRetries: 3, resultValidators: [ (ctx, output) => { if (output.confidence < 0.5) { throw new Error("Confidence too low - please try again."); } return output; // return the output to accept it (or return a modified copy) }, ],});
maxRetries controls how many times the agent retries on validation failure before throwing MaxRetriesError.
Metadata attached by tools during the run, keyed by tool call ID
const result = await agent.run("Summarize this document.");console.log(result.output); // TOutputconsole.log(result.messages); // full historyconsole.log(result.newMessages); // messages from this run onlyconsole.log(result.usage); // { inputTokens, outputTokens, totalTokens, requests }console.log(result.retryCount); // number of validation retriesconsole.log(result.runId); // unique run identifier