## TL;DR

Add export const runtime = 'edge' at the top of the route file.

## Steps

1. AI SDK route handlers run fine on the edge, but the edge runtime bans Node APIs, so a route that works on nodejs can 500 on edge with a confusing error.

2. app/api/chat/route.ts:

3. export const runtime = 'edge';
export const maxDuration = 30;

4. export async function POST(req: Request) {
 // same streamText + createUIMessageStreamResponse wiring as nodejs
}

5. What to know:
1. Add export const runtime = 'edge' at the top of the route file. Without it you get the nodejs runtime, with its cold starts and regional behavior.
2. Provider keys load the same way: environment variables are available in edge. OPENAI_API_KEY and friends are read from process.env by the provider factories, no special handling needed.
3. Do not use Node-only modules in an edge route: no fs, no child_process, no node:crypto. If a tool's execute function reads files, that tool cannot run in an edge route; move it to a nodejs route or a separate service.
4. The AI SDK itself is edge-compatible: streamText, toUIMessageStream, and the provider fetch calls all run on the edge runtime.
5. Some community provider packages assume Node globals. If you see atob is not defined or ReadableStream is not defined style errors after switching to edge, the provider package is the culprit, not the AI SDK. Pin the provider or move that route back to nodejs.
6. Keep streaming responses unbuffered. On Vercel the platform handles this; behind your own proxy, disable buffering or the edge speed win disappears.

## When to use

You are seeing this: Without it you get the nodejs runtime, with its cold starts and regional behavior. Use this skill when you run into "AI SDK on the Next.js Edge runtime: what changes and what breaks".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
