Skip to content

Next.js app with OpenAI: AI launch checklist

Next.js apps call OpenAI with your own API key, from server code, so OpenAI'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.

    vercel.com ↗

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

    vercel.com ↗

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

    nextjs.org ↗

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

    vercel.com ↗

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

    vercel.com ↗

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.

    nextjs.org ↗

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

    nextjs.org ↗

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

    nextjs.org ↗

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

    nextjs.org ↗

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

    vercel.com ↗

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

    nextjs.org ↗

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

    nextjs.org ↗

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.

    nextjs.org ↗

  • 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).

    vercel.com ↗

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

    nextjs.org ↗

  • You add it

    Who the AI fails for

    Have the agent save input traits on each result and split worked-first-time by them.

    nextjs.org ↗

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.

    nextjs.org ↗

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

OpenAI limits to set before launch

Organisation limit
https://platform.openai.com/settings/organization/limits → Spend → Edit spend limit → Monthly spend limit → turn on 'Enforce a hard limit' → Save.
Project limit
https://platform.openai.com/settings/ → (project) Limits → Spend → Edit spend limit → same steps.
What happens at the limit
A spend alert only notifies; traffic continues. A hard limit makes requests return 429 with project_spend_limit_exceeded or organization_spend_limit_exceeded. Enforcement isn't instantaneous, so spend can slightly exceed the limit. Separate codes: organization_usage_limit_exceeded (OpenAI-assigned tier limit) and credit_balance_exhausted (prepaid credits gone).
Note
Community threads in 2025–2026 report hard caps disappearing and returning; the current official guide documents them.
Key scoping
Keys belong to projects; project service-account keys are unique to their project. Key permissions: All, Restricted or Read Only. Per project, owners can choose allowed models and set a rate limit per model under Limits. (From OpenAI help-center snippets; the page returned 403 to the fetcher.)
Rate limits
Set at organization and project level, not per user. Tiers: Free ($100/month usage limit, allowed geography), Build ($5 total credit purchases, $500/month), Launch ($100, $5,000/month), Grow ($500, $200,000/month); upgrades automatically. View at Settings → Organization → Limits (platform.openai.com/settings/organization/limits). The current page no longer uses 'Tier 1–5' names.
Where usage shows
Usage at https://platform.openai.com/settings/organization/usage, grouped by project and model. Per-API-key grouping is reported by a third-party summary of OpenAI's changelog, but community threads say some accounts only see project/model; not confirmed in official docs.

developers.openai.com ↗developers.openai.com ↗help.openai.com ↗help.openai.com ↗

Mistakes Next.js apps make at launch

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

    nextjs.org ↗vercel.com ↗

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

    community.vercel.com ↗

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

    developers.openai.com ↗vercel.com ↗

Questions

How do AI calls work in a Next.js app with OpenAI?

Next.js apps call OpenAI with your own API key, from server code, so OpenAI'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 OpenAI 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 OpenAI bill?

Add a per-user daily cap in your own code, checked before every AI call. Then set a limit at OpenAI as a backstop: https://platform.openai.com/settings/organization/limits → Spend → Edit spend limit → Monthly spend limit → turn on 'Enforce a hard limit' → Save.

More checklists