API Key Management for LLM APIs: Application, Storage, and Rotation Best Practices
This article explains how to treat LLM API keys as sensitive credentials rather than ordinary configuration. It covers identity verification before application, the difference between test and production keys, environment isolation, secret storage and server-side proxying, and the lifecycle practices of rotation, auditing, and revocation, along with a layered troubleshooting approach for rejected or blocked calls.
1. Why API Keys Are the First Security Boundary for LLM Applications
When developers connect large language model capabilities to a business system, the first thing they encounter is not model performance tuning but a seemingly ordinary string: the API key. It is the only credential a platform uses to identify the caller, determine the scope of permissions, and attribute usage data. In other words, if a key leaks, an attacker does not need to breach your servers at all — they can make calls in your name, consume your quota and budget, and even reach resources that should not be accessible through your permissions.
So start with one basic principle: a key is not a configuration item, but a sensitive asset on the same level as a database password or a cloud account’s primary credential. The design, storage, distribution, rotation, and revocation of that asset form a complete key governance practice.
Functionally, this type of credential usually serves three roles at once:
- Authentication: confirming which registered entity a request comes from, and rejecting anonymous or forged calls.
- Access control: limiting which capabilities, model categories, and concurrency or rate limits are available.
- Usage metering: aggregating call counts and input/output volume per entity, as the basis for billing and quotas.
2. Before You Apply: Identity Verification and Use Case Description
Most mainstream platforms require callers to complete identity verification of the responsible entity before opening up API access. The purpose is accountability: when abuse, prohibited content generation, or abnormal traffic occurs, the platform needs to be able to trace it back to a real responsible party. An organization typically submits business registration documents, while an individual completes identity verification.
Once verification is done, you create the credential in the key management area. The system generally generates two pieces of information at the same time: a public identifier for identity (Access Key ID) and a private part used for signing (Secret Access Key). The single most important practice here: the private part is usually shown in full only once, in the response that confirms creation. Once you close the page it cannot be viewed again, and the only option is to regenerate it. The correct order of operations is therefore to prepare a secure storage location first, then write the key into a secret management service or an encrypted configuration store immediately after creation — not to create the key first and figure out where to put it later.
If you are applying for an unrestricted or high-quota production credential, the platform will often also ask for a use case description, such as the business model, expected call volume, content safety strategy, and whether it is exposed to end users. This description is not a formality: it determines whether the review passes, and it prompts developers to think through capacity planning and compliance boundaries before submitting.
3. Test vs. Production Credentials: Why Strict Separation Matters
Most platforms divide credentials into test and production types, which differ significantly in quota, validity period, and the bar for obtaining them. Test credentials usually have a daily call limit and a short validity period, making them suitable for integration work, unit tests, and quality checks. Production credentials are issued after a use case review, with higher quotas and sometimes no fixed cap.
The engineering logic behind this design deserves attention: it decouples the cost of trial and error from production stability. If development uses a production credential directly, a single badly written retry loop can exhaust the monthly quota within minutes and cause a real service outage. The recommended approach is environment isolation:
Development/test -> test credentials: low quota, short validity, low-cost models onlyStaging -> test credentials or separate sub-account credentials for regression checksProduction -> production credentials, separate account structure, full auditing and alerting
Also avoid the “one credential for everything” pattern. Applying for separate credentials per business line and per service keeps usage attribution clear, keeps the blast radius of a failure contained, and lets you revoke a single compromised service without affecting everything else.
4. Storing and Calling Credentials: From Config Files to Secret Management
In practice, many leaks do not come from sophisticated attacks at all, but from keys hardcoded into source code, committed to a repository, or written in plaintext into images and frontend build artifacts. A layered set of protections to follow:
- No hardcoding and no commits. Plaintext keys must not appear in code repositories, image layers, or frontend bundles.
- Prefer a secret management service. Store credentials in the platform’s key management, configuration center, or credential hosting capability, and read them at runtime by reference name so nothing is written to disk in plaintext.
- Proxy calls through the server. Terminal applications should not call the model API directly; a backend gateway should handle signing, rate limiting, and content moderation, with the frontend communicating only with your own service.
- Least privilege and network constraints. Use access control policies to limit which capabilities a credential can invoke and the conditions under which it can be used, narrowing permissions to what the business actually needs.
A typical server-side read (pseudocode):
# Fetch the credential from the secret manager by reference name; no plaintext on diskapi_key = secret_manager.get("llm_api_key_ref")resp = client.chat(model="general-purpose-model",messages=[{"role": "user", "content": user_input}],timeout=10,)
With a shared wrapper in place, you can also enforce rate limits, filter input and output content for safety, and record call-chain logs at a single exit point — capabilities typically provided by general cloud product categories such as API gateways, logging services, and monitoring and alerting.
5. Rotation, Auditing, and Revocation: Giving Keys a Lifecycle
The core of key management is not “set it once” but continuous operation. A minimum viable set of mechanisms:
- Rotate on a schedule. Roll keys over roughly every 90 days with zero downtime by running two keys in parallel: issue the new key and shift traffic gradually, then revoke the old one once nothing looks abnormal.
- Rotate on anomalies. Trigger rotation immediately when someone leaves, when repository permissions change, or when a suspected leak alert fires, rather than waiting for the cycle to end.
- Audit everything. Log the calling entity, time, model, usage, and source identifier, and set alert thresholds for traffic spikes, unusual regions, and calls outside working hours.
- Revoke in tiers. Keep the ability to disable a single credential with one action, and periodically clean up zombie keys that have gone unused for a long time.
- Usage attribution dashboard. Track consumption by business and by credential, which both surfaces cost anomalies and provides a basis for capacity planning.
6. Common Issues and How to Troubleshoot Them
When a call is rejected, work through the layers in order — credential validity, permission scope, quota and rate limits, request structure. Confirm the key has not been revoked or expired, confirm the capability being called is within the authorized scope, confirm you have not hit a per-minute or daily rate limit, and finally check that the request body and signing method match the API contract. If the returned content is blocked, check the content safety policy and how the prompt is constructed instead of retrying repeatedly, which only wastes quota.
Overall, API key governance is low-cost, high-return engineering work: think through the use case at application time, enforce strict isolation and managed storage during use, and hold to rotation and auditing in operation. Get those three stages solid, and a team can adopt LLM capabilities while keeping the probability of credential risk and runaway cost low enough.
