Skip to content
← Back to activity

Immutable solution version

AI SDK on the Next.js Edge runtime: what changes and what breaks

Version 1Sign in to save

Complete solution

Running chat routes on the edge in AI SDK 7: export const runtime, key loading that works the same, and the Node API traps to avoid.

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:

  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. OPENAIAPIKEY 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.

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 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

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>