Next.js app with OpenRouter: AI launch checklist
Next.js apps call OpenRouter with your own API key, from server code, so OpenRouter's own limits are your safety net. Of the 20 checks before you charge users, Next.js handles 0, partly handles 3, and leaves 17 to you.
Checked 12 Nov 2026 against each platform's docs; every fact links to its source.
How AI calls work in Next.js
- Built-in AI
- None unless you choose one. You call providers from route handlers or server actions with their SDKs or the AI SDK; Vercel AI Gateway is optional (same budgets, logs and reporting as v0).
- Who pays
- Each provider directly (or AI Gateway Credits at provider list price with no markup).
Keys and secrets
- Where secrets live
- Vercel project environment variables (Production / Preview / Development); .env files locally, which create-next-app adds to .gitignore.
- Server-side by default?
- 'By default, environment variables are only available on the server.'
- How keys end up in the browser
- Prefixing an AI key with NEXT_PUBLIC_, which Next.js inlines into the JavaScript sent to the browser at build time; or committing .env.
The 20 checks, in Next.js
What Next.js does for you, and what to ask it (or your coding agent) to add. Tick them off in the interactive checklist.
Cost and abuse
- You add it
A spending cap per user
Ask Claude Code or Cursor: 'In every route handler or server action that calls the provider, sum the user's cost today from ai_costs and return an error over $2.' If you use AI Gateway, add a project budget as a backstop.
- Partly
Rate limits on every AI endpoint
Add a Vercel WAF rate-limit rule on your AI routes (all plans; one rule on Hobby) and have the agent add a per-user limit in code backed by Redis or Postgres.
- Partly
AI keys only on the server
Next.js keeps non-NEXT_PUBLIC_ variables on the server and inlines NEXT_PUBLIC_ ones into the browser bundle, so have the agent grep for NEXT_PUBLIC_ on any AI key and keep every provider call in route handlers or server actions.
- You add it
Retries capped, with a timeout
Have the agent set an explicit retry cap (2) and a timeout on every SDK call and remove client-side retries.
- You add it
A budget alert at each AI provider
Set a hard limit at each provider (OpenAI 'Enforce a hard limit', Anthropic spend limits, Gemini project spend cap, OpenRouter key limit, fal alerts); Vercel Spend Management only covers Vercel's own metered usage.
Charging for AI use
- You add it
Every AI call charged or deliberately free
Have the agent list every provider call site, including background jobs and moderation, and mark each as charged or free on purpose.
- You add it
Credits deducted on the server, after success
Have the agent deduct credits in the same database transaction that saves the result, inside the route handler, with no client-writable balance.
- You add it
Failed AI calls refunded
Have the agent refund reserved credits with a reason on provider errors, timeouts, refusals and empty or invalid outputs.
- You add it
One charge per request, even on a double click
Have the agent add a client-generated request id with a unique constraint and return the saved result on repeats.
A record of every AI result
- You add it
Each AI result stored with its feature, model, prompt version and settings
Have the agent create ai_results with feature, model id reported by the provider, prompt version, params, input, user_id and provider request id (AI Gateway's logs help but aren't tied to your users).
- You add it
Retries and corrections saved next to the original, never over it
Have the agent store retries and edits as new versioned rows linked to the original.
- You add it
What users do next with each AI result, recorded on the server
Have the agent add an ai_events table and a server action called from the existing Retry, Edit, Copy and Export buttons.
What worked, for whom, at what cost
- You add it
Worked first time, per feature, model and prompt version
Have the agent add an admin page with worked-first-time per feature, model and prompt version.
- You add it
A cost record for every AI step
Have the agent save an ai_costs row per call from the provider's usage field and one price table (or AI Gateway Generation Lookup).
- You add it
Wasted AI spend and real cost per result, per feature
Have the agent build the wasted-spend and cost-per-working-result query from ai_costs and ai_events.
- You add it
Who the AI fails for
Have the agent save input traits on each result and split worked-first-time by them.
Switching safely
- Partly
Prompts in version control, with version labels
The app is already a git repo; have the agent move inline prompts into prompts/*.ts with version labels saved on each result.
- You add it
Model and prompt switchable by a flag
Have the agent read model id and prompt version from a Vercel environment variable or database setting per feature, with a user-id-hashed split.
- You add it
Real past requests to replay before a change
Have the agent write a script that replays 50 anonymised past requests through any model and prompt version and saves results and cost.
- You add it
A plan for model shutdowns
Have the agent centralise model ids in one config and write MODELS.md with each provider's retirement dates.
OpenRouter limits to set before launch
- Per-key limit
- Key dialog → 'Key limit' → 'Credit limit', reset interval and 'Include BYOK usage in limit' (toggle appears only with BYOK keys configured). API: PATCH https://openrouter.ai/api/v1/keys/{key_hash} with limit (USD), limit_reset (daily, weekly, monthly; unset = lifetime; resets at midnight UTC) and include_byok_in_limit (default false).
- When a key hits its limit
- Conflicting docs: the capping guide says requests fail with 403 'Key limit exceeded'; the limits reference says over-limit requests return 402 with limit_source openrouter_key_limit (or openrouter_guardrail_budget / openrouter_workspace_budget). Test before relying on either.
- Workspace and guardrails
- Workspace budget: one pooled cap for all members and keys. Guardrail budgets: per member or per key, daily/weekly/monthly; 'the first limit reached blocks the request'; the lower of key limit and member guardrail wins.
- Account balance
- Prepaid credits; an 'in-flight spending budget' (a fraction of balance) can fail paid requests with 402 even when balance is positive; a negative balance can error even free models.
- Alerts and top-up
- Low-balance alerts and auto top-up: not found in the pages read.
- Key scoping
- Per-key credit limits and per-key guardrails (model, provider, ZDR and data-region policy). Keys are owned by a member or a workspace; spend is attributed to the key's creator, so per-person enforcement needs one key per person. Management API key can create keys (POST /api/v1/keys).
- Rate limits
- Free model variants (':free'): 20 RPM; 50 requests/day if under 10 credits ever purchased, 1,000/day at 10+. Paid models: 'no platform-level request cap' (upstream provider limits still apply). Key-management endpoints 10,000 requests/day. Cloudflare DDoS protection blocks dramatic excess.
- Where usage shows
- Activity page filters by API key, model, provider and user; export CSV or PDF grouped by Model, API Key or Creator. GET /api/v1/activity (management key, last 30 days) filters by api_key_hash. GET https://openrouter.ai/api/v1/key returns usage, usage_daily, usage_weekly, usage_monthly, limit_remaining and applicable budgets for the calling key.
openrouter.ai ↗openrouter.ai ↗openrouter.ai ↗openrouter.ai ↗
Mistakes Next.js apps make at launch
AI key prefixed with NEXT_PUBLIC_.
Next.js inlines NEXT_PUBLIC_ values into JavaScript sent to the browser; Vercel reported over 17k v0 deployments blocked in July 2025 for exposed secrets and NEXT_PUBLIC_ misuse.
An unauthenticated or forgotten AI route.
Vercel Community thread (2026): unexpected Anthropic and OpenAI usage traced to a likely leftover dev route; keys had to be rotated.
Relying on an alert instead of a hard stop.
OpenAI spend alerts don't stop traffic unless 'Enforce a hard limit' is on; Vercel Spend Management checks every few minutes and doesn't cap provider bills.
Questions
How do AI calls work in a Next.js app with OpenRouter?
Next.js apps call OpenRouter with your own API key, from server code, so OpenRouter's own limits are your safety net.
Can Next.js cap AI spending per user?
You add it. Ask Claude Code or Cursor: 'In every route handler or server action that calls the provider, sum the user's cost today from ai_costs and return an error over $2.' If you use AI Gateway, add a project budget as a backstop.
Where should the OpenRouter key live in Next.js?
Partly. Next.js keeps non-NEXT_PUBLIC_ variables on the server and inlines NEXT_PUBLIC_ ones into the browser bundle, so have the agent grep for NEXT_PUBLIC_ on any AI key and keep every provider call in route handlers or server actions.
How do I stop one user running up my OpenRouter bill?
Add a per-user daily cap in your own code, checked before every AI call. Then set a limit at OpenRouter as a backstop: Key dialog → 'Key limit' → 'Credit limit', reset interval and 'Include BYOK usage in limit' (toggle appears only with BYOK keys configured). API: PATCH https://openrouter.ai/api/v1/keys/{key_hash} with limit (USD), limit_reset (daily, weekly, monthly; unset = lifetime; resets at midnight UTC) and include_byok_in_limit (default false).