AI Agents

Building Durable AI Agents

AI agents are built on the primitive of LLM and tool-call loops, often with additional processes for data fetching, resource provisioning, or reacting to external events.

Workflow DevKit makes your agents production-ready, by turning them into durable, resumable workflows. It transforms your LLM calls, tool executions, and other async operations into retryable, scalable, and observable steps.

chatWorkflow
agent.stream
searchWeb
agent.stream
waitForHumanApproval
agent.stream

This guide walks you through converting a basic AI chat app into a durable AI agent using Workflow DevKit.

Why Durable Agents?

Aside from the usual challenges of getting your long-running tasks to be production-ready, building mature AI agents typically requires solving several additional challenges:

  • Statefulness: Persisting chat sessions and turning LLM and tool calls into async jobs with workers and queues.
  • Observability: Using services to collect traces and metrics, and managing them separately from your messages and user history.
  • Resumability: Resuming streams requires not just storing your messages, but also storing streams, and piping them across services.
  • Human-in-the-loop: Your client, API, and async job orchestration need to work together to create, track, route to, and display human approval requests, or similar webhook operations.

Workflow DevKit provides all of these capabilities out of the box. Your agent becomes a workflow, your tools become steps, and the framework handles interplay with your existing infrastructure.

Getting Started

To make an Agent durable, we first need an Agent, which we'll be setting up here. If you already have an app you'd like to follow along with, you can skip this section.

For our example, we'll need an app with a simple chat interface and an API route calling an LLM, so that we can add Workflow DevKit to it. We'll use the Flight Booking Agent example as a starting point, which comes with a chat interface built using Next.js, AI SDK, and Shadcn UI.

Clone example app

We'll need an app with a simple chat interface and an API route calling an LLM, so that we can add Workflow DevKit to it. For the follow-along steps, we'll use the Flight Booking Agent example as a starting point, which comes with a chat interface built using Next.js, AI SDK, and Shadcn UI.

If you have your own project, you can skip this step, and simply apply the changes of the following steps to your own project.

git clone https://github.com/vercel/workflow-examples -b plain-ai-sdk
cd workflow-examples/flight-booking-app

Set up API keys

In order to connect to an LLM, we'll need to set up an API key. The easiest way to do this is to use Vercel Gateway (works with all providers at zero markup), or you can configure a custom provider.

Get a Gateway API key from the Vercel Gateway page.

Then add it to your .env.local file:

.env.local
GATEWAY_API_KEY=...

This is an example of how to use the OpenAI provider for AI SDK. For details on other providers and more details, see the AI SDK provider guide.

npm i @ai-sdk/openai

Set your OpenAI API key in your environment variables:

.env.local
OPENAI_API_KEY=...

Then modify your API endpoint to use the OpenAI provider:

app/api/chat/route.ts
// ...
import { openai } from "@workflow/ai/openai"; 

export async function POST(req: Request) {
  // ...
  const agent = new Agent({
    // This uses the OPENAI_API_KEY environment variable by default, but you
    // can also pass { apiKey: string } as an option.
    model: openai("gpt-5.1"), 
    // ...
  });

Get familiar with the code

Let's take a moment to see what we're working with. Run the app with npm run dev and open http://localhost:3000 in your browser. You should see a simple chat interface to play with. Go ahead and give it a try.

The core code that makes all of this happen is quite simple. Here's a breakdown of the main parts. Note that there's no changes needed here, we're simply taking a look at the code to understand what's happening.

Our API route makes a simple call to AI SDK's Agent class, which is a simple wrapper around AI SDK's streamText function. This is also where we pass tools to the agent.

app/api/chat/route.ts
export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();
  const agent = new Agent({ 
    model: gateway("bedrock/claude-4-5-haiku-20251001-v1"),
    system: FLIGHT_ASSISTANT_PROMPT,
    tools: flightBookingTools,
  });
  const modelMessages = convertToModelMessages(messages);
  const stream = agent.stream({ messages: modelMessages }); 
  return createUIMessageStreamResponse({
    stream: stream.toUIMessageStream(),
  });
}

Our tools are mostly mocked out for the sake of the example. We use AI SDK's tool function to define the tool, and pass it to the agent. In your own app, this might be any kind of tool call, like database queries, calls to external services, etc.

workflows/chat/steps/tools.ts
import { tool } from "ai";
import { z } from "zod";

export const tools = {
  searchFlights: tool({
    description: "Search for flights",
    inputSchema: z.object({ query: z.string() }),
    execute: searchFlights,
  }),
};

async function searchFlights({ from, to, date }: { from: string; to: string; date: string }) {
  // ... generate some fake flights
}

Our ChatPage component has a lot of logic for nicely displaying the chat messages, but at it's core, it's simply managing input/output for the useChat hook from AI SDK.

app/chat.tsx
"use client";

import { useChat } from "@ai-sdk/react";

export default function ChatPage() {
  const { messages, input, handleInputChange, handleSubmit } = useChat({ 
    // ... other options ...
  });

  // ... more UI logic

  return (
    <div>
      // This is a simplified example of the rendering logic
      {messages.map((m) => (
        <div key={m.id}>
          <strong>{m.role}:</strong>
          {m.parts.map((part, i) => {
            if (part.type === "text") { 
              return <span key={i}>{part.text}</span>;
            }
            if (part.type === "tool-searchFlights") { 
              // ... some special rendering for our tool results
            }
            return null;
          })}
        </div>
      ))}
      <form onSubmit={handleSubmit}>
        <input
          value={input}
          onChange={handleInputChange}
          placeholder="Type a message..."
        />
      </form>
    </div>
  );
}

Integrating Workflow DevKit

Now that we have a basic agent using AI SDK, we can modify it to make it durable.

Install Dependencies

Add the Workflow DevKit packages to your project:

npm i workflow @workflow/ai

and extend the Next.js config to transform your workflow code (see Getting Started for more details).

next.config.ts
import { withWorkflow } from "workflow/next";
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  // ... rest of your Next.js config
};

export default withWorkflow(nextConfig);

Create a Workflow Function

Move the agent logic into a separate function, which will serve as our workflow definition.

workflows/chat/workflow.ts
import { DurableAgent } from "@workflow/ai/agent"; 
import { getWritable } from "workflow"; 
import { tools } from "@/ai/tools";
import { openai } from "@workflow/ai/openai";
import type { ModelMessage, UIMessageChunk } from "ai";

export async function chatWorkflow(messages: ModelMessage[]) {
  "use workflow"; 

  const writable = getWritable<UIMessageChunk>(); 

  const agent = new DurableAgent({ 

    // If using AI Gateway, just specify the model name as a string:
    model: "bedrock/claude-4-5-haiku-20251001-v1", 

    // ELSE if using a custom provider, pass the provider call as an argument:
    model: openai("gpt-5.1"), 

    system: FLIGHT_ASSISTANT_PROMPT,
    tools: flightBookingTools,
  });

  await agent.stream({ 
    messages,
    writable,
  });
}

Key changes:

  • Add the "use workflow" directive to mark our Agent as a workflow function
  • Replaced Agent with DurableAgent from @workflow/ai/agent. This ensures that all calls to the LLM are executed as "steps", and results are aggregated within the workflow context (see Workflows and Steps for more details on how workflows/steps are defined).
  • Use getWritable() to get a stream for agent output. This stream is persistent, and API endpoints can read from a run's stream at any time.

Update the API Route

Remove the agent call that we just extracted, and replace it with a call to start() to run the workflow:

app/api/chat/route.ts
import type { UIMessage } from "ai";
import { convertToModelMessages, createUIMessageStreamResponse } from "ai";
import { start } from "workflow/api";
import { chatWorkflow } from "@/workflows/chat/workflow";

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();
  const modelMessages = convertToModelMessages(messages);

  const run = await start(chatWorkflow, [modelMessages]); 

  return createUIMessageStreamResponse({
    stream: run.readable, 
  });
}

Key changes:

  • Call start() to run the workflow function. This returns a Run object, which contains the run ID and the readable stream (see Starting Workflows for more details on the Run object).
  • Pass the writable to agent.stream() instead of returning a stream directly, ensuring all the Agent output is written to to the run's stream.

Convert Tools to Steps

Mark all tool definitions with "use step" to make them durable. This enables automatic retries and observability for each tool call:

workflows/chat/steps/tools.ts​
// ...

export async function searchFlights(
  // ... arguments
) {
  "use step"; 

  // ... rest of the tool code
}

export async function checkFlightStatus(
  // ... arguments
) {
  "use step"; 

  // ... rest of the tool code
}

export async function getAirportInfo(
  // ... arguments
) {
  "use step"; 

  // ... rest of the tool code
}

export async function bookFlight({
  // ... arguments
}) {
  "use step"; 

  // ... rest of the tool code
}

export async function checkBaggageAllowance(
  // ... arguments
) {
    "use step"; 

    // ... rest of the tool code
  }
}

With "use step":

  • The tool execution runs in a separate step with full Node.js access. In production, each step is executed in a separate worker process, which scales automatically with your workload.
  • Failed tool calls are automatically retried (up to 3 times by default). See Errors and Retries for more details.
  • Each tool execution appears as a discrete step in observability tools. See Observability for more details.

That's all you need to do to convert your basic AI SDK agent into a durable agent. If you run your development server, and send a chat message, you should see your agent respond just as before, but now with added durability and observability.

Observability

In your app directory, you can open up the observability dashboard to see your workflow in action, using the CLI:

npx workflow web

This opens a local dashboard showing all workflow runs and their status, as well as a trace viewer to inspect the workflow in detail, including retry attempts, and the data being passed between steps.

Next Steps

Now that you have a basic durable agent, it's a only a short step to add these additional features:

Complete Example

A complete example that includes all of the above, plus all of the "next steps" features is available on the main branch of the Flight Booking Agent example.