Adding OpenAI to a Production MERN App, Safely

Prompt design, rate limits, cost control, and keeping sensitive data out of model calls on a safety-critical platform.
PHIR is a threat intelligence platform focused on online child safety, where AI analysis is useful only if it is careful. Working on it shaped a checklist I now use for any AI feature.
Never call the model from the browser
All model calls go through your Node.js API. That keeps keys secret, lets you validate input, and gives you one place for logging, limits and fallbacks.
Send the minimum data
- Strip names, emails and identifiers before text leaves your server when the task does not need them.
- Send excerpts, not whole records.
- Document exactly which fields are sent and why.
Ask for structured output
Free-form answers are hard to use in code. Ask the model for JSON that matches a schema, validate it with a library such as Zod, and treat anything that fails validation as an error.
const Result = z.object({
risk: z.enum(["low", "medium", "high"]),
reasons: z.array(z.string()).max(5),
});
const parsed = Result.safeParse(JSON.parse(modelOutput));
if (!parsed.success) return fallback();Limits and cost
Rate-limit AI endpoints per user, cap tokens per request, and cache results for identical inputs. Log token usage per feature so cost surprises show up in a dashboard, not an invoice.
Keep a human in the loop
On sensitive decisions, AI should rank and summarize, and a person should decide. The model speeds up review; it does not replace accountability.









