runAgent is a convenience wrapper around runPipeline that drives a multi-turn LLM conversation. Each turn in the conversation is a single pipeline phase named "turn". If the process crashes mid-conversation, the message history is preserved in the checkpoint and the agent resumes from the last completed turn — no messages are lost and no API calls are duplicated.
When to use runAgent
Use runAgent when…
- You need a multi-turn tool loop that must survive crashes or timeouts
- You want per-turn log entries and crash-safe resume with message history intact
- You’re building on Convex or another durable backend and want the simplest path
Use something else when…
- One-shot prompt with no tools → call
generateTextdirectly from the AI SDK - Custom phase graph with LLM calls → use
buildAgentPhaseand compose phases manually - You need parallel tool execution or branching → build phases explicitly
Installation
You need the core package, the Vercel AI SDK, andzod for tool schemas:
Quickstart
The following example wires up a calculator tool and runs a single-question agent conversation end-to-end:Turn lifecycle
Each call toadvancePhase processes one turn. Within a turn, the library:
- Calls
generateText({ model, messages, tools })with the full message history from the checkpoint - Appends
result.response.messages(model response + any tool results) to the history - Checks
finishReason:"tool-calls"→ returns{ kind: "next", nextPhase: "turn" }— the same phase runs again with the updated history"stop"→ returns{ kind: "done" }— pipeline completesturn >= maxTurns→ logs a warning and returns{ kind: "done" }(status:"complete", not"error")
RunAgentArgs
string
required
Unique identifier for this agent run.
LanguageModel
required
Any AI SDK v6
LanguageModel. Use gateway(), openai(), anthropic(), or any compatible provider.string
System prompt prepended to every turn’s message array.
AgentTurn[]
required
The starting message history, typically
[{ role: "user", content: "..." }]. On "resume", this is ignored — the checkpoint’s message history is used instead.number
Maximum number of turns before the agent stops. Defaults to
10. When the limit is reached, the pipeline completes normally (not as an error).StorageAdapter<AgentCheckpoint>
required
Storage adapter typed to
AgentCheckpoint.SchedulerAdapter
required
Scheduler adapter that triggers each turn advance.
RetryMode
"resume" keeps message history intact; "full" discards it and starts fresh from initialMessages.AgentCheckpoint
The state type used by the agent loop:pendingToolCalls is reserved for a future version that will support interrupting the agent to execute long-running tools as separate pipeline phases. In v0.1, all tool execution is synchronous within a single turn.