
# Stopping Streams

Cancelling ongoing streams is often needed.
For example, users might want to stop a stream when they realize that the response is not what they want.

The different parts of the AI SDK support cancelling streams in different ways.

## AI SDK Core

The AI SDK functions have an `abortSignal` argument that you can use to cancel a stream.
You would use this if you want to cancel a stream from the server side to the LLM API, e.g. by
forwarding the `abortSignal` from the request.

```tsx highlight="10,11"
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const result = streamText({
    model: openai('gpt-4-turbo'),
    prompt,
    // forward the abort signal:
    abortSignal: req.signal,
  });

  return result.toTextStreamResponse();
}
```

## AI SDK UI

The hooks, e.g. `useChat` or `useCompletion`, provide a `stop` helper function that can be used to cancel a stream.
This will cancel the stream from the client side to the server.

```tsx file="app/page.tsx" highlight="9,18-20"
'use client';

import { useCompletion } from '@ai-sdk/react';

export default function Chat() {
  const { input, completion, stop, status, handleSubmit, handleInputChange } =
    useCompletion();

  return (
    <div>
      {(status === 'submitted' || status === 'streaming') && (
        <button type="button" onClick={() => stop()}>
          Stop
        </button>
      )}
      {completion}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
      </form>
    </div>
  );
}
```

## AI SDK RSC

<Note type="warning">
  The AI SDK RSC does not currently support stopping streams.
</Note>


## Navigation

- [Prompt Engineering](/v4/docs/advanced/prompt-engineering)
- [Stopping Streams](/v4/docs/advanced/stopping-streams)
- [Backpressure](/v4/docs/advanced/backpressure)
- [Caching](/v4/docs/advanced/caching)
- [Multiple Streamables](/v4/docs/advanced/multiple-streamables)
- [Rate Limiting](/v4/docs/advanced/rate-limiting)
- [Rendering UI with Language Models](/v4/docs/advanced/rendering-ui-with-language-models)
- [Language Models as Routers](/v4/docs/advanced/model-as-router)
- [Multistep Interfaces](/v4/docs/advanced/multistep-interfaces)
- [Sequential Generations](/v4/docs/advanced/sequential-generations)
- [Vercel Deployment Guide](/v4/docs/advanced/vercel-deployment-guide)


[Full Sitemap](/sitemap.md)
