📄 ai-sdk/docs/advanced/sequential-generations

File: sequential-generations.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/docs/advanced/sequential-generations

AI SDK

Menu

v5 (Latest)

AI SDK 5.x

AI SDK by Vercel

AI SDK 6 Beta

Foundations

Overview

Providers and Models

Prompts

Tools

Streaming

Getting Started

Navigating the Library

Next.js App Router

Next.js Pages Router

Svelte

Vue.js (Nuxt)

Node.js

Expo

Agents

Agents

Building Agents

Workflow Patterns

Loop Control

AI SDK Core

Overview

Generating Text

Generating Structured Data

Tool Calling

Model Context Protocol (MCP) Tools

Prompt Engineering

Settings

Embeddings

Image Generation

Transcription

Speech

Language Model Middleware

Provider & Model Management

Error Handling

Testing

Telemetry

AI SDK UI

Overview

Chatbot

Chatbot Message Persistence

Chatbot Resume Streams

Chatbot Tool Usage

Generative User Interfaces

Completion

Object Generation

Streaming Custom Data

Error Handling

Transport

Reading UIMessage Streams

Message Metadata

Stream Protocols

AI SDK RSC

Advanced

Prompt Engineering

Stopping Streams

Backpressure

Caching

Multiple Streamables

Rate Limiting

Rendering UI with Language Models

Language Models as Routers

Multistep Interfaces

Sequential Generations

Vercel Deployment Guide

Reference

AI SDK Core

AI SDK UI

AI SDK RSC

Stream Helpers

AI SDK Errors

Migration Guides

Troubleshooting

Copy markdown

Sequential Generations

=========================================================================================================

When working with the AI SDK, you may want to create sequences of generations (often referred to as "chains" or "pipes"), where the output of one becomes the input for the next. This can be useful for creating more complex AI-powered workflows or for breaking down larger tasks into smaller, more manageable steps.

Example


In a sequential chain, the output of one generation is directly used as input for the next generation. This allows you to create a series of dependent generations, where each step builds upon the previous one.

Here's an example of how you can implement sequential actions:

import { openai } from '@ai-sdk/openai';import { generateText } from 'ai';
async function sequentialActions() {  // Generate blog post ideas  const ideasGeneration = await generateText({    model: openai('gpt-4o'),    prompt: 'Generate 10 ideas for a blog post about making spaghetti.',  });
  console.log('Generated Ideas:\n', ideasGeneration);
  // Pick the best idea  const bestIdeaGeneration = await generateText({    model: openai('gpt-4o'),    prompt: `Here are some blog post ideas about making spaghetti:${ideasGeneration}
Pick the best idea from the list above and explain why it's the best.`,  });
  console.log('\nBest Idea:\n', bestIdeaGeneration);
  // Generate an outline  const outlineGeneration = await generateText({    model: openai('gpt-4o'),    prompt: `We've chosen the following blog post idea about making spaghetti:${bestIdeaGeneration}
Create a detailed outline for a blog post based on this idea.`,  });
  console.log('\nBlog Post Outline:\n', outlineGeneration);}
sequentialActions().catch(console.error);

In this example, we first generate ideas for a blog post, then pick the best idea, and finally create an outline based on that idea. Each step uses the output from the previous step as input for the next generation.

On this page

Sequential Generations

Example

Deploy and Scale AI Apps with Vercel.

Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.

Trusted by industry leaders:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert