SECURITY GUIDE
Securing AI API Keys & Preventing Runaway Ingestion Costs
How to avoid $10,000 surprise billing alerts through rate limits, token budgeting, and server-only cryptographic secrets.
By TRIHEX Security Lab (Cloud Security & Threat Modeling)
•5 min read
•Published: 2026-03-021. The Server-Only Invariant
Never prefix an API key or database credential with 'NEXT_PUBLIC_'. Anything prefixed with NEXT_PUBLIC_ is embedded in plain text into client JavaScript bundles and can be inspected by anyone using browser developer tools.
Always use server-only modules ('import server-only') to guarantee that sensitive credentials fail compilation if accidentally imported by client components.
2. Enforcing Hard Spend Caps at the Provider Level
Configure hard billing limits in your OpenAI, Anthropic, and Google Cloud consoles. Soft limits send an email notification, but do not stop requests; a hard limit immediately halts API calls when the threshold is reached, protecting your bank account from unexpected automated loops.