📄 ai-sdk/cookbook/next/stream-text-multistep

File: stream-text-multistep.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/cookbook/next/stream-text-multistep

AI SDK

Menu

Guides

RAG Agent

Multi-Modal Agent

Slackbot Agent Guide

Natural Language Postgres

Get started with Computer Use

Get started with Gemini 2.5

Get started with Claude 4

OpenAI Responses API

Google Gemini Image Generation

Get started with Claude 3.7 Sonnet

Get started with Llama 3.1

Get started with GPT-5

Get started with OpenAI o1

Get started with OpenAI o3-mini

Get started with DeepSeek R1

Next.js

Generate Text

Generate Text with Chat Prompt

Generate Image with Chat Prompt

Stream Text

Stream Text with Chat Prompt

Stream Text with Image Prompt

Chat with PDFs

streamText Multi-Step Cookbook

Markdown Chatbot with Memoization

Generate Object

Generate Object with File Prompt through Form Submission

Stream Object

Call Tools

Call Tools in Multiple Steps

Model Context Protocol (MCP) Tools

Share useChat State Across Components

Human-in-the-Loop Agent with Next.js

Send Custom Body from useChat

Render Visual Interface in Chat

Caching Middleware

Node

Generate Text

Generate Text with Chat Prompt

Generate Text with Image Prompt

Stream Text

Stream Text with Chat Prompt

Stream Text with Image Prompt

Stream Text with File Prompt

Generate Object with a Reasoning Model

Generate Object

Stream Object

Stream Object with Image Prompt

Record Token Usage After Streaming Object

Record Final Object after Streaming Object

Call Tools

Call Tools with Image Prompt

Call Tools in Multiple Steps

Model Context Protocol (MCP) Tools

Manual Agent Loop

Web Search Agent

Embed Text

Embed Text in Batch

Intercepting Fetch Requests

Local Caching Middleware

Retrieval Augmented Generation

Knowledge Base Agent

API Servers

Node.js HTTP Server

Express

Hono

Fastify

Nest.js

React Server Components

Copy markdown

streamText Multi-Step Agent

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

You may want to have different steps in your stream where each step has different settings, e.g. models, tools, or system prompts.

With createUIMessageStream and sendFinish / sendStart options when merging into the UIMessageStream, you can control when the finish and start events are sent to the client, allowing you to have different steps in a single assistant UI message.

Server


app/api/chat/route.ts

import { openai } from '@ai-sdk/openai';import {  convertToModelMessages,  createUIMessageStream,  createUIMessageStreamResponse,  streamText,  tool,} from 'ai';import { z } from 'zod';
export async function POST(req: Request) {  const { messages } = await req.json();
  const stream = createUIMessageStream({    execute: async ({ writer }) => {      // step 1 example: forced tool call      const result1 = streamText({        model: openai('gpt-4o-mini'),        system: 'Extract the user goal from the conversation.',        messages,        toolChoice: 'required', // force the model to call a tool        tools: {          extractGoal: tool({            inputSchema: z.object({ goal: z.string() }),            execute: async ({ goal }) => goal, // no-op extract tool          }),        },      });
      // forward the initial result to the client without the finish event:      writer.merge(result1.toUIMessageStream({ sendFinish: false }));
      // note: you can use any programming construct here, e.g. if-else, loops, etc.      // workflow programming is normal programming with this approach.
      // example: continue stream with forced tool call from previous step      const result2 = streamText({        // different system prompt, different model, no tools:        model: openai('gpt-4o'),        system:          'You are a helpful assistant with a different system prompt. Repeat the extract user goal in your answer.',        // continue the workflow stream with the messages from the previous step:        messages: [          ...convertToModelMessages(messages),          ...(await result1.response).messages,        ],      });
      // forward the 2nd result to the client (incl. the finish event):      writer.merge(result2.toUIMessageStream({ sendStart: false }));    },  });
  return createUIMessageStreamResponse({ stream });}

Client


app/page.tsx

'use client';
import { useChat } from '@ai-sdk/react';import { useState } from 'react';
export default function Chat() {  const [input, setInput] = useState('');  const { messages, sendMessage } = useChat();
  return (    <div>      {messages?.map(message => (        <div key={message.id}>          <strong>{`${message.role}: `}</strong>          {message.parts.map((part, index) => {            switch (part.type) {              case 'text':                return <span key={index}>{part.text}</span>;              case 'tool-extractGoal': {                return <pre key={index}>{JSON.stringify(part, null, 2)}</pre>;              }            }          })}        </div>      ))}      <form        onSubmit={e => {          e.preventDefault();          sendMessage({ text: input });          setInput('');        }}      >        <input value={input} onChange={e => setInput(e.currentTarget.value)} />      </form>    </div>  );}

On this page

streamText Multi-Step Agent

Server

Client

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