BroadRouter
Get started

LLM API key security: storage, rotation and IP allow-lists

By the BroadNet Inference team · 6 min read · Published · Updated

Keep LLM API keys in a secret manager, never in code or the browser. Give each app or agent its own key, rotate on a schedule, and lock keys to your servers with an IP allow-list. BroadNet Inference says its allow-lists take 32 entries, revocation stops new requests within five seconds, and keys last one year.

To secure an LLM API key, keep it in a secret manager and call the model only from your server, never from a browser or app. Give each workload its own key, rotate keys on a schedule, lock them to your server IPs with an allow-list, and revoke a leaked key first, investigating second.

This post covers the habits that keep keys safe, with the specific controls BroadNet Inference provides. For step-by-step portal clicks, see the API keys and IP allow-lists guide; for budgets and runaway-loop protection, see LLM API spend control.

Where should you store an LLM API key?

Store keys in a secret manager or your platform’s encrypted secrets, never in code. The OWASP Secrets Management Cheat Sheet covers the options in depth. Note its caution: environment variables “are generally accessible to all processes and may be included in logs or system dumps”, so OWASP prefers mounted secret files or fetching secrets from a secret store where you have the choice. An environment variable filled from a secret manager at start-up is the common middle ground.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://inference.broadnet.ai/v1",
    api_key=os.environ["BROADNET_API_KEY"],  # fails loudly if missing
)

Use os.environ[...], not os.getenv(...) with a hard-coded fallback; a fallback is how keys end up in Git. For local development, a .env file is fine if it is in .gitignore from the first commit. Turn on your Git host’s secret scanning and push protection. On GitHub, push protection blocks pushes that contain known secret formats before they reach the repository; push protection for users is on by default, but repository-level push protection is off by default and must be enabled by a repository administrator or organisation owner.

Can you put an API key in a browser or mobile app?

No. Anything shipped to a client can be extracted. Front-end code and app bundles are public, however they are minified or obfuscated. Call the LLM from your server, authenticate your own users there, and apply your own per-user limits before forwarding.

The same goes for desktop tools you hand to others. If a teammate needs access to a coding agent, give them their own key on their own account rather than sharing one.

Why give each workload its own key?

On BroadNet Inference an account can hold up to ten live keys. Use them:

Key label Used by
prod-api Production backend
prod-worker Batch jobs
staging Staging environment
agent-ci Coding agent in CI
dev-laptop-<name> Local development

When one leaks, you revoke only that key, and the others keep working. Labels also make usage readable: you can see which workload spends what. Name keys after what uses them, not who created them.

How do IP allow-lists protect a key?

An IP allow-list means a stolen key fails anywhere except your own infrastructure. On BroadNet Inference:

  • The account allow-list takes up to 32 entries, one IPv4 or IPv6 address or CIDR range per line.
  • An empty list means no restriction.
  • New keys inherit the account list, unless you create a key with its own allowed_ips, which replaces the inherited list for that key.
  • A request from an address outside the list is refused with a 403 (“refused by model, route or IP policy”).

Create a key with its own list through the portal API:

curl -X POST https://inference.broadnet.ai/portal/keys \
  -H "Authorization: Bearer $BROADNET_PORTAL_CREDENTIAL" \
  -H "Content-Type: application/json" \
  -d '{"label":"prod-worker","allowed_ips":["203.0.113.7","198.51.100.0/24"]}'

The response contains the raw key once. Store it in your secret manager immediately; it cannot be read back. Full steps are in the IP allow-list guide.

Allow-lists suit servers with stable egress IPs: a VPS, a NAT gateway, a fixed office IP. For laptops on changing networks, use a separate key without a list and keep its balance exposure small.

Why separate inference keys from admin credentials?

BroadNet uses two kinds of credential:

Credential Can do Store it
Inference key Model requests to /v1/* Wherever the app runs
Portal credential Create and revoke keys, change allowed IPs, make purchases Only in your automation or admin tooling

The docs say to treat each portal credential as full account authority. Never deploy a portal credential next to your application. If your app server is compromised, the attacker should get one inference key, not control of the account.

How often should you rotate API keys?

OWASP recommends regular rotation “so that any stolen credentials will only work for a short time.” BroadNet keys last one year from creation, so every key will need replacing anyway. Make rotation routine:

  1. Create a new key with the same label pattern and allow-list.
  2. Deploy it alongside the old one, or switch the secret and restart.
  3. Watch traffic move to the new key.
  4. Revoke the old key with DELETE /portal/keys/{alias}.

Because an account can hold up to ten live keys, you can overlap old and new keys for a few minutes without downtime. Rotate sooner for keys that a departing team member had access to.

What should you do when a key leaks?

When a key leaks, revoke it first and investigate second. On BroadNet Inference, new requests stop within five seconds of a revocation or allow-list change; the docs explain that authorisation decisions may remain in a replicated cache for that bounded interval.

After revoking:

  • check usage for the key in the account page or GET /portal/usage;
  • find how it leaked (commit history, CI logs, a shared screenshot);
  • rotate any other secrets stored in the same place.

A revoked or blocked key returns 401 or 403; see the errors guide so your monitoring does not treat those as transient.

How do keys leak through logs?

Logs are an easy leak to miss. Redact the Authorization header in HTTP debug logging, error reporters and proxy access logs. When you contact support, send the X-Request-ID, timestamp, route, status and the key alias, never the key itself. BroadNet’s docs say the portal stores only a hash of each key, so it cannot show you a lost key either.

How do you limit the damage of a leak you have not found yet?

Even with every habit above, assume a key may leak someday. Shrink the blast radius:

  • Keep the pay-as-you-go balance at a level you are comfortable losing, and top up as needed.
  • Use a flat plan for heavy agents: a plan session has a fixed price, so a stolen plan key cannot run up a per-token bill. See what unlimited really means.
  • Alert on unusual spend or request rates per key; the LLM API cost calculator gives you a normal monthly figure to alert against. Our post on LLM API spend control covers budgets and alerts.

Which key controls does BroadNet Inference provide?

Control BroadNet Inference
Key shown once, stored hashed Yes
Live keys per account Up to 10
Key lifetime One year
IP allow-list Up to 32 IPv4, IPv6 or CIDR entries
Per-key allow-list Yes, allowed_ips at creation
Revocation takes effect Within five seconds
Separate admin credential Portal credential

Keys protect your account; zero data retention protects what you send through it. Read both before you ship.

Questions

Where should I store an LLM API key?

In a secret manager or your platform's encrypted secrets, injected into the process as an environment variable such as BROADNET_API_KEY. Not in source code, not in a committed .env file, and never in front-end code.

How fast can I revoke a BroadNet Inference key?

BroadNet's docs say new requests stop within five seconds of a revocation or an allow-list change, because authorisation decisions may stay in a replicated cache for that bounded interval.

How long does a BroadNet Inference key last?

A pay-as-you-go key lasts one year from creation. An account can hold up to ten live keys, so you can create the replacement before the old one expires.

Can BroadNet show me a key again if I lose it?

No. BroadNet's docs say the raw key is returned once and the portal stores only its hash. Create a new key and revoke the old one.

What should I do if my OpenAI-compatible API key leaks?

Revoke it first, then investigate. Check the key's usage for unknown traffic, find where it leaked (commit history, CI logs, screenshots), rotate any other secrets stored in the same place, and issue a replacement key with an IP allow-list.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity