AI API Key Security & Cost Controls
Protect AI workloads with separate keys, quotas, expiration, model limits, IP allowlists, routing policy, rotation, and auditable cost attribution.
An AI API key should identify one workload boundary, not an entire organization forever. Separate keys make access, quota, routing, rotation, and usage records easier to control without exposing one credential across unrelated applications.
Modelflare API keys can combine an expiration date, finite or unlimited quota, model limits, an IP allowlist, and routing policy. These controls are most effective when they are designed together rather than added only after an incident.
Start with one key per workload
Create separate keys for boundaries that need independent access or accounting, such as:
- production API and background workers;
- development and CI;
- a coding agent or local tool;
- a customer-facing application;
- a temporary contractor or migration;
- a high-cost batch workload.
Use names that describe environment and purpose, such as prod-support-agent or dev-evaluation. A clear name makes request records and rotation decisions understandable without revealing the secret.
Keep the secret out of client-visible code
Store the full key in a server-side secret manager or protected environment variable. Do not put it in:
- browser JavaScript or mobile packages that users can inspect;
- source control, container images, screenshots, or tickets;
- analytics events, error trackers, shell history, or application logs;
- URLs, query strings, or copied command examples with real values.
For a browser application, send requests through a backend that authenticates the user and holds the Modelflare key. If a key may have leaked, create a replacement, move the workload, verify traffic on the new key, and revoke the old one.
Use quota and expiration as independent boundaries
A finite key quota places a hard usage boundary on that key. An expiration date limits how long the credential remains valid. They solve different risks:
| Control | Primary purpose |
|---|---|
| Finite quota | Limits the amount of usage attributable to the key |
| Unlimited quota | Avoids a key-level cap; account balance and other policy still apply |
| Expiration | Ends access after a defined time |
| Disable or revoke | Stops new access immediately |
Development, contractor, and evaluation keys should usually have both a finite quota and an expiration. Production keys may need a larger or unlimited key quota, but they still benefit from separate workload attribution and a rotation plan.
Quota is not a substitute for concurrency limits or client retry control. A burst can consume the remaining amount quickly, and repeated failures can create additional requests.
Restrict models when the workload is narrow
Model limits prevent a key from requesting models outside its intended set. This reduces accidental use of an incompatible or unexpectedly expensive model and gives a clearer 403 boundary when the client asks for something it should not access.
Keep the allowed set aligned with the endpoint. A key that can list a model still needs a client and protocol that support that model's request format. Review the live contract in Models & Pricing instead of copying model IDs into a long-lived policy document.
Add IP rules only when the network path is stable
An IP allowlist checks the client IP observed by Modelflare against individual addresses or CIDR ranges configured on the key. It is useful for servers with known egress, but it can break traffic when:
- a cloud workload changes outbound NAT addresses;
- requests move through a new proxy or gateway;
- IPv4 and IPv6 paths differ;
- a developer switches networks;
- the configured CIDR is broader or narrower than intended.
Verify the real egress path before enabling the restriction, keep an emergency rotation path, and do not treat IP allowlisting as a replacement for secret protection.
Choose regular or Smart routing intentionally
| Key type | Routing behavior | Best fit |
|---|---|---|
| Regular API Key | One explicit primary group plus optional ordered fallback groups | Workloads that require a known route order and group policy |
| Smart API Key | Evaluates account-eligible groups using balanced, stability-first, or low-price-first strategy | Workloads that want broader automatic route selection |
Both key types still send the requested model and protocol. Routing policy chooses an eligible group; it does not guarantee that every group supports every model or feature. A regular fallback list should be tested in order. A Smart API Key should be evaluated with real latency, price, and compatibility requirements before production.
For detailed behavior, read Reliable AI API Routing.
Rotate without an avoidable outage
Use an overlapping rotation:
- Create a new key with the same intended restrictions.
- Store it in the application's secret system.
- Deploy the new credential without deleting the old one.
- Verify model access and one real request.
- Confirm new usage records appear under the new key.
- Remove the old key from every deployment and automation job.
- Disable or delete the old key.
- Monitor for unexpected requests using the retired credential.
If compromise is active, revoke first and accept the controlled interruption. The overlap procedure is for planned rotation, not continued exposure.
Connect key policy to cost review
Key-level isolation makes cost review actionable. Filter usage logs by key, then compare model, group, input and output usage, status, retries, and cost. A finite quota can stop further use, but the request records explain what consumed it.
See AI API Cost Tracking for a repeatable review process.
API key checklist
- One key per application, environment, or accountable workload.
- No secrets in browser code, repositories, logs, URLs, or screenshots.
- Finite quota and expiration for temporary or experimental use.
- Model restrictions for narrow workloads.
- IP rules only for a verified, stable egress path.
- Explicit regular fallback order or an intentionally selected Smart strategy.
- Rotation tested before the old key is removed.
- Usage and cost reviewed by key after every policy change.
Frequently asked questions
Does an unlimited key remove all spending limits?
No. It removes the key-level quota cap. Account balance, model pricing, group policy, request limits, and other service controls still apply.
Can multiple keys bypass a rate limit?
Not necessarily. A limit can apply at account, model group, or route level. Create keys for access and attribution, not as an unverified rate-limit workaround.
Is an IP allowlist enough if a key leaks?
No. Network rules reduce where a key can be used, but network paths change and configuration can be wrong. Rotate exposed credentials and keep the secret protected at rest and in deployment.