Using the Agent Class
Overview
The Agent class uses an internal AI SDK ToolLoopAgent through composition with Output prompt files and the skills system. It does not inherit from ToolLoopAgent. Use it when you need multi-step tool execution, conversation history, or a reusable agent instance. For single-shot LLM calls without tools, generateText is simpler.
When to Use This Skill
- Building multi-step agents that call tools in a loop
- Using skills (lazy-loaded instructions) with an agent
- Creating agents with structured output via
aiSdk.Output.object() - Implementing stateful conversations with
messageStore - Streaming Agent progress with
onChunk - Deciding between
AgentandgenerateText
Import Pattern
Agent comes from @outputai/llm. Use aiSdk.Output for structured output. Import z from @outputai/core (never from zod directly). MessageStore is the type for a pluggable getMessages / addMessages store; implement it yourself.
Construction
The prompt file is loaded and rendered at construction time. Variables and tools are fixed at construction. Skills and maxSteps come from the prompt file. The agent is ready to call generate(), generateWithStreaming(), or stream() immediately.
Constructor Options
generate()
Run the agent and return when complete:
The result has the same shape as generateText: text, result (alias for text), output, usage, finishReason, toolCalls, etc.
Passing Additional Messages
Extend the conversation with extra messages:
Messages are appended after the initial prompt messages (and any message-store history). You can also pass abortSignal and toolChoice.
generateWithStreaming()
Use generateWithStreaming() when you need progress callbacks and a complete result:
The method behaves like generate() while using streaming internally. It returns the complete response, rejects on stream errors, and automatically appends messages to the configured message store. It accepts the same messages, abortSignal, and toolChoice as generate(), plus onChunk. Prefer it over stream() in Temporal activity steps unless direct access to the stream result is required.
stream()
Use stream() when direct control over textStream or stream is required. It accepts the same messages, abortSignal, and toolChoice as generate(), plus onChunk, onEnd, and onError:
Like streamText, the stream result provides textStream and stream iterables, plus promise-based properties (text, usage, finishReason) that resolve on completion.
stream() appends messages to the message store in its wrapped onEnd when finishReason is not 'error'. See output-dev-llm-streaming for streaming and error-handling guidance.
Structured Output
Use aiSdk.Output.object() to get typed responses:
Use .describe() on schema fields instead of .min()/.max() for number constraints. Anthropic does not support minimum/maximum JSON Schema constraints in tool definitions.
Message Store
By default, Agent is stateless. Each generate() call starts fresh with only the initial prompt messages. Pass a messageStore to maintain history across calls:
MessageStore is:
ModelMessage is an AI SDK type available through the aiSdk namespace or as an import from ai. There is no built-in store. Implement the interface in memory for a single process, or with your database for durable history.
Using Agent in Workflow Steps
In workflow steps, construct a new Agent per invocation. Variables come from the step input:
This is the standard pattern. Each step invocation is independent, and Agent construction is cheap.
Using Agent with Skills
List skill paths in the prompt frontmatter. See output-dev-skill-file for the full skills guide.
When to Use Agent vs generateText
Start with generateText. Move to Agent when you need conversation state or a reusable instance with a fixed configuration.
generateText Example (for comparison)
Verification Checklist
- Import
Agentfrom@outputai/llm(not fromaidirectly) - Import
zfrom@outputai/core(never fromzod) - Prompt file exists in
prompts/folder - Variables match
{{ variable }}placeholders in the prompt - Prompt frontmatter sets
maxStepswhen skills or tools need a ceiling other than 10 -
aiSdk.Output.object({ schema })uses.describe()not.min()/.max()on numbers -
messageStoreis only used when multi-turn history is needed - Agent is constructed inside the step
fn(not at module level) for workflow steps - Prefer
generateWithStreaming()when callbacks are sufficient
Related Skills
output-dev-skill-file- Creating skill files for agentsoutput-dev-llm-streaming- Streaming progress and Temporal-safe error handlingoutput-dev-prompt-file- Creating .prompt files used by agentsoutput-dev-step-function- Using agents in step functionsoutput-dev-types-file- Defining Zod schemas for structured outputoutput-dev-workflow-function- Orchestrating agent-powered steps


