Deploying an AI SDK Agent to Vercel Edge Functions
Streaming chat feels fast when the function starts near the user and begins flushing tokens immediately.
This page deploys a Next.js App Router AI SDK agent route on Vercel's Edge runtime with duration, env, and compatibility checks that keep streams healthy in production.
Summary
Mark the chat route with export const runtime = 'edge', stream with streamText + UI message response helpers, set maxDuration within platform limits, keep dependencies edge-safe, and verify secrets via Vercel env vars. Prefer Node runtime when the agent needs heavy native modules or long multi-tool work.
Recipe
- Confirm the agent route is pure Web Fetch APIs (no Node-only
fs, optional native addons). - Add
export const runtime = 'edge'(and region config if you pin locales). - Set
export const maxDuration = ...appropriate to plan and workload. - Store model keys in Vercel project env (
AI_GATEWAY_API_KEY,OPENAI_API_KEY, etc.). - Return a streaming UI message response from the first await path quickly (start streaming within about 25 seconds).
- Bound agent steps (
stopWhen) so tools cannot run until platform kill. - Deploy with
vercel/ git integration; smoke-test streaming from multiple regions. - Fall back to
nodejsruntime if edge bundles fail or libraries require Node.
Working Example
app/api/chat/route.ts:
import {
convertToModelMessages,
isStepCount,
streamText,
tool,
type UIMessage,
} from "ai";
import { z } from "zod";
export const runtime = "edge";
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: "openai/gpt-4o-mini", // gateway string or edge-safe provider - verify at build
messages: await convertToModelMessages(messages),
instructions: "Be concise. Prefer tools only when needed.",
stopWhen: isStepCount(5),
tools: {
timeNow: tool({
description: "Return the current server time ISO string",
inputSchema: z.object({}),
execute: async () => ({ now: new Date().toISOString() }),
}),
},
});
return result.toUIMessageStreamResponse();
}Client stays standard useChat + DefaultChatTransport({ api: '/api/chat' }).
vercel.json is optional; runtime export on the route is enough for many apps.
Env in Vercel dashboard (Production + Preview):
AI_GATEWAY_API_KEY=...
# TOOL_APPROVAL_SECRET=... # if you sign approvalsDeploy:
pnpm i
pnpm build
vercel --prodDeep Dive
Why edge for agents
Edge runtimes (V8 isolates) optimize cold start and geographic placement.
For chat, time-to-first-token often matters more than raw multi-second tool throughput.
Streaming remains supported: start sending the response promptly, then continue streaming within platform caps (edge streaming commonly up to ~300s class limits; confirm current Vercel docs).
Duration and the 25-second start rule
Edge functions should begin the response within about 25 seconds to keep streaming beyond that window.
Design implications:
- Do not block on slow non-stream work before returning the stream.
- Prefer streaming the model call immediately; run slow tools as later steps inside the stream lifecycle when possible.
- Move batch prep (large RAG builds) out of the request path.
Edge vs Node for AI SDK apps
| Concern | Edge | Node (nodejs runtime) |
|---|---|---|
| Cold start / TTFB | Often better | Heavier |
| Library support | Web-standard only | Full Node ecosystem |
| Max duration | Platform edge limits | Often higher configurable caps on Pro+ |
| Native modules | Usually no | Yes |
| Long agent graphs | Risky | Safer |
| Simple streamText chat | Excellent | Fine |
If @something imports Node crypto legacy APIs or PDF native bindings, use Node.
AI SDK Core streaming itself is edge-friendly when providers use fetch.
Regions and data locality
Pinning functions near your DB can beat "closest to user" if every tool call cross-ocean hops.
For pure LLM proxy chat with external model APIs, multi-region edge is often ideal.
For tools hitting a single regional Postgres, consider Node in that region or edge with a nearby data plane.
Observability on the edge
Log:
- request id / chat id
- model id
- step count / tool names
- error class (provider vs validation)
- duration until first byte and total
Wire OpenTelemetry or Vercel log drains; avoid logging full prompts if policy forbids it.
Security checklist before prod
- Authn before tools; never authorize from client-provided user id alone.
- Rate limit by IP/user on
/api/chat. - Cap
maxDurationand tool steps. - Approval secrets for write tools.
- Separate preview vs production API keys and spend limits (OpenRouter / provider dashboards).
Preview deployments
Each PR preview needs env vars copied or shared.
Point useChat at same-origin /api/chat so previews do not call production APIs accidentally.
Gotchas
- Node-only dependency pulled into the edge route. Bundle fails or crashes at runtime.
- Waiting on DB before streaming. Users see hung UI past the start deadline.
- Hobby/plan duration too low for multi-tool agents. Raise plan limits or reduce steps.
- Missing env on Preview. Works locally, 500s on Vercel preview URLs.
- CORS misconfig for separate frontend domain. Prefer same-origin Next routes.
- Assuming infinite stream. Platform max duration still kills the function.
- Large cold tool imports. Dynamic-import heavy code only on Node, or slim the edge path.
Alternatives
| Approach | Pros | Cons |
|---|---|---|
| Vercel Edge route | Low latency streaming | API surface limits |
| Vercel Node route | Full compatibility | Heavier isolates |
| Separate Node agent service | Scale agent independently | More ops, CORS/proxy |
| Serverless other clouds | Flexibility | You rebuild streaming glue |
| Long-running worker + poll | Hard jobs | Worse chat UX |
FAQs
Is Edge required for the AI SDK?
No. The SDK runs on Node and other runtimes. Edge is an optimization choice.
Can I use OpenAI's official SDK on edge?
Prefer AI SDK providers that use fetch. Some official SDKs need Node. Verify your provider package.
What maxDuration should I set?
Start with 30s for simple chat; raise for multi-step tools within your plan's maximum. Measure p95.
How do I force Node runtime?
export const runtime = 'nodejs' (or omit where Node is default for your Next version - verify).
Do Edge functions support streaming tool UIs?
Yes if you return the UI message stream correctly; tool parts flow like on Node.
What about Fluid compute?
Vercel continues to evolve function execution (including Fluid compute). Treat runtime choice as "edge vs node" and re-read current Vercel limits at deploy time.
Can Python backends run on Vercel Edge?
Not as the AI SDK TypeScript edge route. Host Python (Pydantic AI / LangGraph) on a Python-friendly service and proxy streams from Next.
How do I test edge locally?
next dev does not perfect production edge. Use vercel dev or deploy previews for integration tests.
Why is first token slow only in one region?
Check model provider regional latency, cold starts, and whether tools call a far database.
Should middleware also be edge?
Auth middleware often is edge; keep it thin. Do not run the full agent in middleware.
Related
- Vercel AI SDK Basics - local chat setup
- The Vercel AI SDK Mental Model: Streaming UI Meets Agent Loops - stream architecture
- Tool Calling and UI Generation with the AI SDK - tools in production
- AI SDK 6's Tool-Approval States for Human-in-the-Loop Agents - safe writes
- Integrating Pydantic AI or LangGraph Behind an AI SDK Frontend - non-edge Python agents
- Vercel AI SDK Best Practices - deploy habits
- Deploying Agents Basics - broader deploy section
Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.