Skip to main content
A Restate AI application has two main components:
  • Restate Server: Sits in front of your agents and takes care of orchestration and resiliency
  • Agent Services: Your agent logic using the Restate SDK for durability
Application Structure Your agent is a regular function, a handler, that makes LLM calls, executes tools, and coordinates work. Restate wraps this handler in durable execution: every step is recorded in a journal, so if the process crashes, the agent picks up exactly where it left off. You can use the Restate SDK alone or in combination with an Agent SDK.

Creating a durable agent

Follow the Agent Quickstart to run a durable agent end-to-end. A Restate agent has three building blocks:
  1. The handler: An HTTP handler containing your agent logic, exposed in a Restate service
  2. LLM calls: Persisted so responses are not re-fetched on recovery
  3. Tool executions: Wrapped in durable steps so side effects are not duplicated
To implement a durable agent, you use the Restate SDK in combination with the Vercel AI.Here’s a weather agent that looks up the weather for a city:
agent.ts
import * as restate from "@restatedev/restate-sdk";
import { durableCalls } from "@restatedev/vercel-ai-middleware";
import { openai } from "@ai-sdk/openai";
import { generateText, stepCountIs, tool, wrapLanguageModel } from "ai";
import { z } from "zod";

// TOOL
async function getWeather(ctx: restate.Context, city: string) {
  // Do durable steps using the Restate context
  return ctx.run(`get weather ${city}`, () => {
    // Simulate calling the weather API
    return {temperature: 23, description: `Sunny and warm.`}
  })
}

// AGENT
const run = async (ctx: restate.Context, { prompt }: { prompt: string }) => {
  const model = wrapLanguageModel({
    model: openai("gpt-5.4"),
    // Persist LLM responses
    middleware: durableCalls(ctx, { maxRetryAttempts: 3 }),
  });

  const { text } = await generateText({
    model,
    system: "You are a helpful agent that provides weather updates.",
    prompt,
    tools: {
      getWeather: tool({
        description: "Get the current weather for a given city.",
        inputSchema: z.object({ city: z.string() }),
        execute: async ({ city }) => getWeather(ctx, city),
      }),
    },
    stopWhen: [stepCountIs(5)],
    providerOptions: { openai: { parallelToolCalls: false } },
  });

  return text;
};

// AGENT SERVICE
const agent = restate.service({
  name: "agent",
  handlers: {
    run: restate.createServiceHandler({
      input: restate.serde.schema(z.object({
        prompt: z.string().default("What's the weather in San Francisco?"),
      })),
    }, run),
  },
});

restate.serve({ services: [agent] });
The agent logic lives in a handler of a Restate service (here the run handler).The main difference compared to a standard Vercel AI agent is the use of the Restate Context at key points:
  1. Restate service handler: The agent runs inside a Restate service handler, giving it a durable execution context. Restate exposes the handler as an HTTP endpoint you can call via curl, the Restate UI, or any HTTP client.
  2. Persisting LLM responses: Wrap the model with durableCalls(ctx) middleware so every LLM response is saved in the Restate Server and replayed during recovery. The middleware is provided via @restatedev/vercel-ai-middleware.
  3. Resilient tool execution: Tools use ctx.run() to make steps durable. The result is persisted and retried until it succeeds.

Observing your agent

The Restate UI (http://localhost:9070) shows the step-by-step execution trace of your agent, with detailed traces of every LLM call, tool execution, and state change:
Agent execution trace in Restate UI
Learn more.

How durable execution works

When your agent runs, Restate records each step’s result in a journal. If the process crashes mid-execution:
  1. Restate detects the failure and restarts the handler
  2. Completed steps are replayed from the journal (no re-execution)
  3. Execution resumes from the first incomplete step
This means:
  • LLM calls are not repeated (saving cost and time)
  • Tool side effects are not duplicated (no double bookings, no duplicate emails)
  • Multi-step workflows recover their full progress automatically
Durable AI Agent Execution
Try it yourself: follow the Agent Quickstart to run a durable agent and see how it recovers from a failure.