---
title: Streaming responses from LLMs
description: Learn how to use the AI SDK to stream LLM responses.
url: /kb/guide/streaming-from-llm
canonical_url: "https://vercel.com/kb/guide/streaming-from-llm"
published: 2025-11-03
last_updated: 2026-07-29
authors: DX Team
related:
  - /docs/fundamentals/what-is-streaming
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---
<!-- docsgraph:related -->
## Related pages

> **For AI agents:** Follow these links to understand how this page connects to the rest of the Vercel ecosystem. For the full cross-link map (inbound, outbound, prerequisites, and semantic neighbors), see the .graph.md link below.

- [Streaming](https://vercel.com/docs/functions/streaming-functions?from=related) — Learn how to stream responses from Vercel Functions.
- [Streaming](https://vercel.com/docs/ai-gateway/sdks-and-apis/responses/streaming?from=related) — Stream tokens as they are generated with the OpenAI Responses API.
- [Streaming](https://vercel.com/docs/ai-gateway/sdks-and-apis/openai-chat-completions/streaming?from=related) — Stream OpenAI Chat Completions responses token by token as they are generated.
- [Streaming](https://vercel.com/docs/ai-gateway/sdks-and-apis/openresponses/streaming?from=related) — Stream responses token by token using the OpenResponses API.
- [Streaming](https://vercel.com/docs/ai-gateway/sdks-and-apis/anthropic-messages-api/streaming?from=related) — Stream Anthropic Messages API responses token by token as they are generated.
- [Express](https://ai-sdk.dev/cookbook/api-servers/express?from=related)
- [Fastify](https://ai-sdk.dev/cookbook/api-servers/fastify?from=related)
- [Testing](https://ai-sdk.dev/docs/ai-sdk-core/testing?from=related)
- [Node.js HTTP Server](https://ai-sdk.dev/cookbook/api-servers/node-http-server?from=related)
- [Streaming in web applications](https://vercel.com/kb/guide/what-is-streaming?from=related) — Learn how streaming works in web applications. Explore benefits, use cases, and implementation details with Vercel Funct
- [Processing Data Chunks](https://vercel.com/kb/guide/processing-data-chunks?from=related) — Learn how to create an API endpoint that processes data chunks.
- [Building AI apps on Vercel: an overview](https://vercel.com/kb/guide/how-to-build-ai-app?from=related) — Learn the key AI concepts and tools for building and scaling AI apps.

Full cross-link map for this page: [/kb/guide/streaming-from-llm.graph.md](/kb/guide/streaming-from-llm.graph.md)
<!-- /docsgraph:related -->


AI providers can be slow when producing responses, but many make their responses available in chunks as they're processed. Streaming enables you to show users those chunks of data as they arrive rather than waiting for the full response, improving the perceived speed of AI-powered apps.

**You can use** [**Vercel's AI SDK**](https://sdk.vercel.ai/docs) **to stream responses from LLMs and AI APIs**. It reduces the boilerplate necessary for streaming responses from AI providers and allows you to change AI providers with a few lines of code, rather than rewriting your entire application.

This example demonstrates a function that sends a message to one of OpenAI's GPT models and streams the response:

## Recipe

Before you begin, ensure you're using Node.js 18 or later.

1. Install the `ai` and `@ai-sdk/openai` packages:
   

```bash
pnpm install ai @ai-sdk/openai
```

1. Copy an OpenAI API key in the `.env.local` file with name `OPENAI_API_KEY`. See the [AI SDK docs](https://sdk.vercel.ai/docs/getting-started#configure-openai-api-key) for more information on how to do this
   
2. Add the following code to your example
   

```typescript
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```
```javascript
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```
```typescript
// Streaming Functions must be defined in an
// app directory, even if the rest of your app
// is in the pages directory.
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```
```javascript
// Streaming Functions must be defined in an
// app directory, even if the rest of your app
// is in the pages directory.
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```
```typescript
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```
```javascript
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

// This method must be named GET
export async function GET() {
  // Make a request to OpenAI's API based on
  // a placeholder prompt
  const response = streamText({
    model: openai('gpt-4o-mini'),
    messages: [{ role: 'user', content: 'Say this is a test.' }],
  });
  // Respond with the stream
  return response.toTextStreamResponse({
    headers: {
      'Content-Type': 'text/event-stream',
    },
  });
}
```

1. Build your app and visit `localhost:3000/api/chat-example`. You should see the text `"This is a test."` in the browser.
   

## More resources

- [Streaming on Vercel](/docs/fundamentals/what-is-streaming)
  
- [Vercel AI SDK](https://sdk.vercel.ai/docs)