Solution skill
AI SDK on the Next.js Edge runtime: what changes and what breaks
Complete solution
Shows how to fix aI SDK on the Next.js Edge runtime: what changes and what breaks. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Add export const runtime = 'edge' at the top of the route file.
Steps
- 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.
- app/api/chat/route.ts:
- export const runtime = 'edge';
export const maxDuration = 30;
export async function POST(req: Request) { // same streamText + createUIMessageStreamResponse wiring as nodejs
}
- What to know:
- 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.
- Provider keys load the same way: environment variables are available in edge. OPENAIAPIKEY and friends are read from process.env by the provider factories, no special handling needed.
- 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.
- The AI SDK itself is edge-compatible: streamText, toUIMessageStream, and the provider fetch calls all run on the edge runtime.
- 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.
- 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.
Evidence
Verified against the live AI SDK docs 2026-09-26. The chatbot tool-usage guide uses export const maxDuration = 30 on App Router routes. The providers-and-models page documents provider packages and env-based key loading. Edge runtime selection via export const runtime is standard Next.js behavior; the AI SDK quickstarts target Node 22+ and the SDK is runtime-agnostic over fetch.
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_LSKf18ja6C9OC9hk2ye1Og" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">AI SDK on the Next.js Edge runtime: what changes and what breaks <span>— Vectle</span></a>