Lesson 29 of 30
Letting users bring their own API key caps an app's AI bill at something other than "however much every user decides to use" — but it also means the app is now responsible for storing someone else's credential safely.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
In a platform-key-first, BYO-key-fallback pattern, what should happen if the platform key is out of quota and the user has never connected their own key?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
BYO (bring your own) API key means letting a user supply their own provider API key instead of, or in addition to, using AI capacity the app itself pays for. Two real reasons an app reaches for this:
The pattern that actually works in production is a fallback ladder, not a binary choice: try the platform's own key first; if that key is out of quota, fall back to the user's own key if they've provided one; if neither is available, tell the user clearly that they need to connect a provider. This keeps the default experience free/seamless for most users while giving the app a safety valve instead of just failing outright when platform quota runs out.
The security point that actually matters here, more than the mechanics: a user's own API key must never be readable by any other user, or by any code path that isn't authorized to use it. It's a real credential — whoever holds it can spend real money on that person's account — so it belongs in the same category as a password: stored server-side, encrypted at rest, never placed anywhere a client or another user's session could read it back out.
// lib/ai/provider/resolveKey.ts
export function resolveProviderConfig(deps: KeyResolutionDeps): ProviderConfig {
if (deps.platformQuotaOk()) {
const platform = deps.platformConfig();
if (platform) return platform;
}
const user = deps.userConfig();
if (user) return user;
throw new NeedsUserKeyError();
}
This is the fallback ladder made concrete: platform key first (while it has quota), the user's own key second, and a distinct, explicit error (NeedsUserKeyError) if neither is available — rather than a silent failure or a confusing generic error. lib/ai/chat/handler.ts calls this to pick a config before making a request, and separately catches a live QuotaExhaustedError from an actual 429 response mid-call to retry against the user's key if the platform key ran out unexpectedly between the check and the call.
"How would you let users bring their own API key safely?" is a real question, and a strong answer names both the fallback-ladder mechanics (platform key, then user key, then a clear error) and the storage/security constraint (server-side, encrypted, never readable by another user or an unauthorized path) — not just "encrypt it," which is necessary but not sufficient on its own.