FREE1 free audit every month. No card, every finding included.

GUIDESEPISODES

EP 03 · BILLAster × ChatGPT

OpenAI API bill too high? How to protect your AI endpoint

Your API key can sit safely on the server and you can still get a bill you can’t pay. Does your AI route have no login, no rate limit and no spending cap? Then anyone can use it, on your card. Here’s how to lock it in four steps.

  • 13 min read
  • Beginner
  • Oct 1, 2026
Aster looking at a huge AI bill next to ChatGPT at a gas station where a fuel pump has been left running with nobody at the counter.

01THE PROBLEM

How an open AI route turns into a huge bill

Why it mattersKeeping the key on the server stops people stealing the key. It doesn’t stop them using it through your route.

This story is from the episode, and it’s a made-up example. Aster asks ChatGPT why his AI bill is $3,000 when only three people use his app. He did the key part right. OPENAI_API_KEY sat on the server, in a route handler, exactly where it belongs.

3users of his app in the episode
$3,000what he owes in the story (a made-up example)
0logins, rate limits or caps on the route
Open the full chapterIncludes code, a note

The problem was the route itself. His app had a /api/chat endpoint that took any message and sent it to OpenAI. No login, no rate limit, no spending cap. Anyone could open DevTools, see the request and copy it. Then they could send their own prompts through his app, as many as they liked, on his card. Bots that scan for open AI endpoints don’t need your code. They just need the URL.

ChatGPT’s verdict in the episode: “The key is safe. The pump is wide open.” It’s a gas station that left the pump running with nobody at the counter. The fuel is locked away, but anyone can drive up and fill their tank.

anyone’s terminal
# What a stranger can do with an open AI route. No key needed, yours is used.
curl -X POST 'https://your-app.com/api/chat' \
  -H 'content-type: application/json' \
  -d '{"message": "Write a 5,000-word essay about…"}'

# Put it in a loop and your bill grows every second.

The request is visible in the Network tab of your own site. Hiding the URL doesn’t help; your frontend has to call it.

02WHY

Why hiding the key isn’t enough

Why it mattersAn AI route with no auth is a public key with a nicer URL. Treat every route that spends money like it holds the key.

Moving the key to the server is step one (our EP01 guide on protecting API keys shows how). It means nobody can copy the key and use it from their own machine. But your server route now stands in for that key (it’s a proxy). It turns any request from the internet into a paid OpenAI call. If that route accepts requests from anyone, your bill looks the same as if you had published the key.

Open the full chapterDetails and sources

OWASP has a name for this: API4:2023 Unrestricted Resource Consumption. It covers APIs with no limits on how often or how much a client can use them, including APIs that call paid third-party services per request. Its prevention list reads like this episode’s fix. Rate limit clients. Set maximum sizes for inputs. Limit how often one client can run an operation. And “configure spending limits for all service providers/API integrations”, with billing alerts where limits aren’t possible.

OpenAI’s own safety best practices agree. Users should generally need to register and log in. Limiting input length and output tokens also helps reduce misuse.

It happens to real teams

  • METR (2026): an AI evaluation organization disclosed that a dashboard a researcher ran on a personal cloud server had authentication that silently “failed open”. An attacker got the agent to reveal its model-provider key and used about $600,000 of credits over three weeks. At the time, there was no way to put a spending limit on that key.
  • Gemini, April 2026: a developer reported a €54,000+ Gemini bill in 13 hours from an unrestricted Firebase browser key. Their budget alert and anomaly alert both fired hours late.
  • A vibe-coded SaaS, March 2025: an app built entirely with an AI editor was attacked days after launch. Keys were maxed out, there was no auth, and the founder shut it down within a week.

People also report smaller versions of the same story, like a few thousand dollars of OpenAI calls they didn’t make. Those are anecdotes, but they follow the same pattern: something that spends money was reachable without a limit.

03STEP 1

Require login before the AI route runs

Why it mattersLogin turns “anyone on the internet” into “people you can identify, limit and ban”. Every other step depends on it.

The first check in your route is the session. No session, no AI call, return 401. Then cap what a signed-in user can send and get back. Here’s a Next.js App Router route handler. getSession() stands in for your auth library’s server-side session check:

Open the full chapterIncludes code, a note
lib/openai.ts
import 'server-only'
import OpenAI from 'openai'

// No NEXT_PUBLIC_ prefix: this value never reaches the browser bundle.
export const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
app/api/chat/route.ts
import { NextResponse } from 'next/server'
import { createHash } from 'node:crypto'
import { getSession } from '@/lib/session' // your auth library's server-side session check
import { openai } from '@/lib/openai'

const MAX_INPUT_CHARS = 4_000
const MAX_OUTPUT_TOKENS = 800

export async function POST(req: Request) {
  // 1. No session, no AI. This runs before anything that costs money.
  const session = await getSession()
  const userId = session?.user?.id
  if (!userId) {
    return NextResponse.json({ error: 'Sign in to use chat' }, { status: 401 })
  }

  // 2. Validate and cap the input
  const body = await req.json().catch(() => null)
  const message = typeof body?.message === 'string' ? body.message.trim() : ''
  if (!message || message.length > MAX_INPUT_CHARS) {
    return NextResponse.json({ error: 'Message is empty or too long' }, { status: 400 })
  }

  // 3. Server picks the model and caps the output. The client can't change either.
  const response = await openai.responses.create({
    model: process.env.OPENAI_MODEL!,
    instructions: 'You are the support assistant for this app. Keep answers short.',
    input: message,
    max_output_tokens: MAX_OUTPUT_TOKENS,
    // A hashed, stable user id helps OpenAI detect abuse without getting the raw id
    safety_identifier: createHash('sha256').update(userId).digest('hex'),
  })

  return NextResponse.json({ reply: response.output_text })
}

Auth.js, Clerk, Supabase Auth and others all have a server-side way to read the session. Use that, never a flag sent by the browser.

Why every detail is there

  • Session check first: everything after it can cost money, so it has to come before the OpenAI call, not after.
  • A user id, not just “logged in”: you need it for the per-user rate limit and budget in the next steps.
  • MAX_INPUT_CHARS: input tokens are billed too. A 4,000-character cap stops someone pasting a novel into every request.
  • max_output_tokens: an upper bound on what the model generates, including reasoning tokens. Without it, one prompt like “write 10,000 words” is billed in full.
  • Model from an env var: if the client can pick the model, it can pick the most expensive one.
✕ Don’tapp/api/chat/route.ts
// Anyone on the internet can call this
export async function POST(req: Request) {
  const { message, model } = await req.json()
  const response = await openai.responses.create({ model, input: message })
  return Response.json({ reply: response.output_text })
}
✓ Doapp/api/chat/route.ts
// Session first, server-chosen model, capped input and output
const session = await getSession()
if (!session?.user?.id) return Response.json({ error: 'Sign in' }, { status: 401 })
// …validate message length, then:
await openai.responses.create({
  model: process.env.OPENAI_MODEL!,
  input: message,
  max_output_tokens: 800,
})

04STEP 2

Rate limit per user and per IP

Why it mattersA rate limit caps how fast money can leave. One account, one script or one bad loop can only spend so much per minute.

Login stops strangers. A rate limit stops one account, or one script with a stolen session, from sending thousands of requests. On serverless hosts like Vercel, an in-memory counter resets with every new instance, so keep the counter in Redis. Upstash Ratelimit is built for this:

Open the full chapterIncludes code
terminal
npm install @upstash/ratelimit @upstash/redis @vercel/functions
.env.local
# Server-only. No NEXT_PUBLIC_ prefix.
OPENAI_API_KEY=sk-...
OPENAI_MODEL=your-model-id
UPSTASH_REDIS_REST_URL=https://...
UPSTASH_REDIS_REST_TOKEN=...
lib/ratelimit.ts
import 'server-only'
import { Ratelimit } from '@upstash/ratelimit'
import { Redis } from '@upstash/redis'
import { ipAddress } from '@vercel/functions'

const redis = Redis.fromEnv() // reads UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN

// Signed-in users: 20 messages per minute each
const perUser = new Ratelimit({
  redis,
  limiter: Ratelimit.slidingWindow(20, '1 m'),
  prefix: 'rl:chat:user',
})

// Per IP: a wider net for many accounts behind one address, or no session at all
const perIp = new Ratelimit({
  redis,
  limiter: Ratelimit.slidingWindow(60, '1 m'),
  prefix: 'rl:chat:ip',
})

export function clientIp(req: Request) {
  // On Vercel, ipAddress() reads the platform's IP header.
  // Elsewhere, only trust x-forwarded-for if your host or proxy sets it.
  return ipAddress(req) ?? req.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ?? 'unknown'
}

// Limit by user id when there is one, and always by IP as well
export async function checkRateLimit(req: Request, userId?: string) {
  const byIp = await perIp.limit(clientIp(req))
  if (!byIp.success || !userId) return byIp
  return perUser.limit(userId)
}

Then add it to the route, right after the session check and before the OpenAI call:

app/api/chat/route.ts
import { checkRateLimit } from '@/lib/ratelimit'

// …after the session check
const limit = await checkRateLimit(req, userId)
if (!limit.success) {
  return NextResponse.json(
    { error: 'Too many requests. Try again in a minute.' },
    { status: 429, headers: { 'Retry-After': String(Math.max(1, Math.ceil((limit.reset - Date.now()) / 1000))) } },
  )
}

limit() returns success, limit, remaining and reset (a Unix timestamp in milliseconds). If you turn on Upstash analytics, also wait for the returned pending promise before the function ends.

Pick numbers from your real usage

Start from what a real person can do. Someone typing in a chat box rarely sends more than a few messages a minute, so 20 per minute is generous. Watch your 429 responses for the first week: if real users hit them, raise the limit; if nobody comes close, you can lower it.

05STEP 2B

Using Express instead? express-rate-limit

Why it mattersSame idea, different framework: identify the caller, then cap how often they can spend your money.

On an Express server, express-rate-limit does the same job as middleware. Put auth first, then the limiter, then the handler:

Open the full chapterIncludes code, a note
terminal
npm install express-rate-limit
server.js
import express from 'express'
import { rateLimit, ipKeyGenerator } from 'express-rate-limit'
import OpenAI from 'openai'

const app = express()
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })

// Only if you run behind exactly one proxy or load balancer, so req.ip is the real client
app.set('trust proxy', 1)
app.use(express.json({ limit: '16kb' })) // cap the request body size

// req.user is set by your session middleware (Passport, express-session, your auth SDK…)
function requireAuth(req, res, next) {
  if (!req.user) return res.status(401).json({ error: 'Sign in to use chat' })
  next()
}

const chatLimiter = rateLimit({
  windowMs: 60 * 1000, // 1 minute
  limit: 20,           // 20 requests per user per minute
  standardHeaders: 'draft-8',
  legacyHeaders: false,
  // Per user when signed in, per IP otherwise (ipKeyGenerator handles IPv6 ranges)
  keyGenerator: (req) => req.user?.id ?? ipKeyGenerator(req.ip),
  // With more than one server instance, add a shared store such as Redis
})

app.post('/api/chat', requireAuth, chatLimiter, async (req, res) => {
  const message = typeof req.body?.message === 'string' ? req.body.message.trim() : ''
  if (!message || message.length > 4000) return res.status(400).json({ error: 'Message is empty or too long' })

  const response = await openai.responses.create({
    model: process.env.OPENAI_MODEL,
    input: message,
    max_output_tokens: 800,
  })
  res.json({ reply: response.output_text })
})

The default store is in memory, so each server instance keeps its own count. Add a Redis or Memcached store if you run more than one instance.

06STEP 3

Give every user a daily budget

Why it mattersRate limits stop bursts. Budgets stop the slow, steady drain that runs all night at exactly your limit.

A rate limit controls speed. A budget controls the total. Twenty messages a minute is fine for a person. A script can run at exactly that speed all day. Count the tokens each user spends per day, and stop at a limit you can afford. OpenAI returns the usage on every response, so you record real numbers, not guesses:

Open the full chapterIncludes code
lib/budget.ts
import 'server-only'
import { Redis } from '@upstash/redis'

const redis = Redis.fromEnv()
const DAILY_TOKENS_PER_USER = 50_000 // pick a number you can afford × your number of users

// One counter per user per UTC day
const key = (userId: string) =>
  `budget:chat:${userId}:${new Date().toISOString().slice(0, 10)}`

export async function hasBudget(userId: string) {
  const used = (await redis.get<number>(key(userId))) ?? 0
  return used < DAILY_TOKENS_PER_USER
}

export async function recordUsage(userId: string, tokens: number) {
  const k = key(userId)
  await redis.incrby(k, tokens)
  await redis.expire(k, 60 * 60 * 48) // clean up after two days
}
app/api/chat/route.ts
import { hasBudget, recordUsage } from '@/lib/budget'

// …after the rate limit, before the OpenAI call
if (!(await hasBudget(userId))) {
  return NextResponse.json({ error: 'You’ve reached today’s chat limit.' }, { status: 429 })
}

const response = await openai.responses.create({ /* …as in step 1 */ })

// Record what this request actually used (input + output tokens)
await recordUsage(userId, response.usage?.total_tokens ?? 0)

Prefer your database? The same counter works as one row per user per day:

migration.sql
create table ai_usage (
  user_id text not null,
  day     date not null default current_date,
  tokens  bigint not null default 0,
  primary key (user_id, day)
);

-- after each response: add the tokens it used
insert into ai_usage (user_id, day, tokens)
values ($1, current_date, $2)
on conflict (user_id, day)
do update set tokens = ai_usage.tokens + excluded.tokens;

Tokens or dollars?

Tokens are what the API reports, so they’re the simplest thing to count. If you use several models, multiply input and output tokens by each model’s price from OpenAI’s pricing page and store cents instead. Either way, the check happens before the call and the record happens after it, so a few requests already in flight can go slightly over. The rate limit keeps that overshoot small.

07STEP 4

Set spend limits and alerts in the provider dashboard

Why it mattersSet the hard limit to the most you could pay without panicking. Then set alerts well below it, so you hear about trouble first.

Your code is the first net. The provider’s billing controls are the last one, for the day your code has a bug. Here’s what we could confirm in the official docs as of October 2026.

Open the full chapterIncludes a note

OpenAI

  • Hard spend limits at two levels: for the whole organization (Settings → Organization limits → Spend) and per project (Project settings → Limits → Spend). When a hard limit is reached, requests return 429 with organization_spend_limit_exceeded or project_spend_limit_exceeded. Enforcement isn’t instant, so recorded spend can go slightly over.
  • Spend alerts email you when usage passes an amount. They don’t stop traffic, and they keep working alongside a hard limit, so set alerts below the limit (for example at 50% and 80%).
  • Separate projects for staging and production, each with its own keys and its own rate and spend limits.
  • Prepaid billing: new API accounts buy credits up front and usage stops when the balance runs out. Auto-recharge is on by default when you set it up. You can turn it off, or keep it with an optional monthly recharge limit.
  • Usage tiers come with a monthly usage limit for your organization that rises as you spend more. It’s a platform ceiling, not a budget you chose, so set your own lower limit.

Google Gemini API and Vertex AI

  • Project spend caps in Google AI Studio (announced March 2026): a monthly dollar limit for Gemini API spend per project. Google marks it experimental and notes about a 10-minute billing delay, and you pay for overages in that window.
  • Usage tier caps: each Gemini API tier has a maximum monthly spend across the billing account (Tier 1 is $250). When it’s reached, service pauses for all linked projects until the next month.
  • Cloud Billing spend cap budgets (Preview) can block new Gemini API and Vertex AI usage in a project once a budget is passed. (Vertex AI is now Gemini Enterprise Agent Platform.) The block stays until you lift the cap. Google notes enforcement isn’t instant and overages are billed.
  • Restrict your Google API keys. Keys that were meant for Maps or Firebase could start working with Gemini once the API was enabled on the project. Limit each key to the APIs it needs.

08COMPARE

How much each layer protects you

Why it mattersEach layer covers a hole in the one before it. Only all three together put a ceiling on the bill.

Open the full chapterIncludes a table, a note
SetupWho can spend your moneyWorst case on a bad nightWhat stops it
No protectionAnyone who finds the URL, including botsUnlimited, until you notice or the card failsNothing
Login onlyAnyone who signs up, as fast as they can script itOne free account can still run a loop all nightYou, when you spot it and ban the account
Login + rate limitSigned-in users, at a human speedUsers × your per-minute limit × hours awakeThe rate limit slows it, but doesn’t end it
All three (login, rate limit, spend caps)Signed-in users, at a human speed, up to a daily budgetEach user’s daily budget, and never more than your provider hard limitPer-user budget first, provider hard limit last

THE QUICK READ

What to take away.
In one minute.

  1. 01

    A server-side key is not enough. An AI route with no login works like a public key. Anyone who finds the URL can spend your money.

  2. 02

    OWASP calls this API4:2023 Unrestricted Resource Consumption and recommends rate limits, size limits and spending limits with every paid provider.

  3. 03

    Three layers stop it: require login, rate limit per user and IP, and cap spend per user and per project.

  4. 04

    Provider alerts and caps can lag by minutes or hours. They are the last net, not the first one.

FOR YOUR CODING AGENT

Let your agent check
your own code.

Paste this into Claude Code, Cursor, Codex, Gemini CLI or Copilot. It only reads and reports; it won’t change your code until you approve.

agent-prompt.md
You are a security reviewer for this codebase. Investigate ONE issue: AI endpoints that let anyone spend my money (OWASP API4:2023 Unrestricted Resource Consumption).

Where to look:
- Every server route that calls a paid model: `app/api/**`, `pages/api/**`, Server Actions, Express, serverless functions.
- Search for `openai`, `@anthropic-ai/sdk`, `@google/genai`, `responses.create`, `chat.completions.create`, `messages.create`, `streamText`, `generateText`.

How to judge each route, in this order:
1. Session first: no signed-in user gets `401` before any paid call. Missing = critical.
2. Rate limit per user, plus per IP, in a shared store (Redis/Upstash or the database). An in-memory counter on serverless doesn't count.
3. Caps: a max input length, `max_output_tokens` / `max_tokens`, and the model chosen on the server, never from the request body.
4. A per-user daily token or cost budget, checked before the call.
5. The key read only in server code, never through `NEXT_PUBLIC_` or `VITE_`.
Flag any logged-out demo route without its own strict limits.

Report a table: file:line | what's missing | severity (critical, high, medium, low) | the minimal fix (code).
Remind me to set hard spend limits and alerts in the provider dashboard; you can't check those from code.

Rules: read and report only. Don't modify any file until I approve. Never print key values: mask them.

Then ask me which fixes to apply.

Paste it into the agent you already build with

  • Claude
  • ChatGPT
  • Gemini
  • Cursor
  • Copilot

QUESTIONS PEOPLE ASK

Good questions.
Short answers.

Why is my OpenAI API bill so high when I have few users?

Usually because something other than your users is calling your AI route. If /api/chat (or any route that calls OpenAI) works without a login, anyone who finds it can send requests on your key. Check the OpenAI usage dashboard for traffic that doesn’t match your real users, then add auth, a rate limit and spend limits.

Is my OpenAI API key safe if it’s only on the server?

The key itself is safe from being copied, but your route can still be abused. A server route that forwards any request to OpenAI works like a public key. Require a login, rate limit each user and cap spend.

Can I set a hard spending limit on the OpenAI API?

Yes. OpenAI supports hard spend limits for the organization and for each project, set in the dashboard’s limits settings. When a limit is reached, requests return a 429 error until you raise it or the next monthly cycle starts. Enforcement isn’t instant, so spend can go slightly over.

What’s the difference between an OpenAI spend alert and a spend limit?

An alert sends a notification when spend passes an amount, and traffic keeps flowing. A hard spend limit stops requests. Use both: alerts below the limit so you hear about trouble before the limit cuts traffic.

How do I rate limit a Next.js API route?

Use a shared store, because serverless instances don’t share memory. With Upstash Ratelimit, create a Ratelimit with Ratelimit.slidingWindow(20, '1 m'), call limit(userId) at the start of the route, and return 429 when success is false. Fall back to the client IP when there’s no user.

Should I rate limit by user or by IP?

Both. Per-user limits are fair and can’t be dodged by switching networks. Per-IP limits catch many accounts from one address and traffic without a session. Behind a proxy, make sure you read the real client IP from a header your host controls.

How do I limit how much each user can spend on AI?

Keep a per-user daily counter in Redis or your database. Check it before each AI call, and after the call add the tokens from the response’s usage field. When the counter passes your limit, return a friendly “daily limit reached” message.

Does Google offer spend caps for the Gemini API?

Yes, as of 2026. Google AI Studio has project spend caps (experimental, with about a 10-minute delay). Each usage tier has a monthly spend maximum. Cloud Billing also has spend cap budgets in Preview for the Gemini API and Vertex AI. None of them are instant, so keep limits in your own code too.

Done with the guide?
Let Aster check the rest.

1 free audit every month. Aster reads your code, env files, database rules and packages. You get each finding in plain English, with the file and line.

Aster

CHECKING

YOUR GUIDE TO ASTER

Hi, I’m Aster.
Ask me what Aster checks.

I can’t see your code from here. Please don’t paste secrets or private code.

Prepared answers are always available.