Skip to content

Engineering

Securing LLM API Keys in Production: A Practical Guide

Leaked model API keys are one of the most common and most expensive AI production failures. Server-side proxying, key scoping, budget caps and the monitoring that catches abuse early.

A leaked language model API key is not like a leaked database credential. Nobody has to breach anything: they simply spend your money, at scale, until someone notices the invoice. The fixes are well understood and routinely skipped.

1. Never let a key reach the client

Every model call goes to your backend first. Your server authenticates the user, applies rate limits, then makes the upstream call with the key. A key in a mobile app binary or a browser bundle is a published key, whatever your obfuscation does. For browser-based realtime voice, use ephemeral tokens minted server-side and scoped to a single short session. See OpenAI Realtime API development.

2. Scope keys per environment

  • Separate keys for development, staging and production. Never share one across environments.
  • Separate keys per service, so revoking one does not take down everything.
  • Rotate on a schedule, and immediately whenever someone with access leaves.
  • 3. Hard-cap spending at the provider

    Set a budget limit in the provider console, not only in your own code. Application-level limits fail with the application. A provider-level cap is the backstop that turns a catastrophe into an incident.

    4. Rate limit per user, not only globally

    A global limit protects your bill; a per-user limit protects your product from one abusive account consuming the whole allowance. You want both, and you want the per-user limit tied to an authenticated identity rather than an IP address.

    5. Monitor for the shape of abuse

  • Alert on token spend per hour, not per month. A monthly alert arrives after the damage.
  • Watch for anomalies in request patterns: sudden volume from one account, unusual prompt lengths, or traffic from new regions.
  • Log every call with a user identifier, so an incident can be traced to a source rather than guessed at.
  • 6. Treat prompt injection as an access-control problem

    If your agent can call tools, a successful injection is a privilege escalation. Scope what each tool can do, require confirmation for anything destructive or costly, and never let content retrieved from a document or web page decide which tool runs.

    Direct Project Enquiry

    Tell me what you are building

    Share what you are building and where it is stuck. If a managed platform already solves it, I will say so. I review every enquiry personally and reply within about four hours.

    1. 1Send your project goal and the services you think you need.
    2. 2I review the context and whether I am the right fit.
    3. 3You get a direct reply with a practical next step.

    International projects welcome

    I work in English by email, video call or through Upwork, from GMT+7 with overlap hours for US and EU teams.

    Read the Privacy Policy, Terms of Service and Cookie Policy.

    Client project enquiries only

    This form is for businesses and teams looking to hire. Sales pitches, recruitment messages, guest posts and link-building outreach are not reviewed.

    Select Service *

    In a hurry? Message me on WhatsApp at nguyentienm@gmail.com

    Message me