How Do You Manage Secrets and API Keys for LLM Applications in 2027?
You manage secrets and API keys for an LLM application by keeping them out of source code and out of prompts, storing them in a central secrets manager, giving each app its own scoped key with an expiry, rotating on a schedule, and scanning for leaks before they reach a repository. The model should never be the thing that holds a credential.
The main providers say the same thing. OpenAI's production guide tells you to expose keys to your application using environment variables or a secret management service and to avoid exposing them in code or public repositories. OWASP says credentials and connection strings should not be in the system prompt and should be externalized to systems the model does not directly access.
This page is general guidance from the linked vendor and standards pages, not a security audit of your setup. If a key has leaked, revoke it first and ask questions later.
Why LLM keys are a bigger target than most
An LLM key is billable by use, so a leaked one can run up a bill as well as expose data. Leaks are common: GitGuardian's State of Secrets Sprawl 2025 reports 23,770,171 new secrets detected in public GitHub commits in 2024, a 25% increase from the previous year, and says 70% of secrets leaked in 2022 remain valid today. It also reports that public repositories using GitHub Copilot had a 6.4% secret leakage rate, 40% higher than the average across public repositories.
The report gives no breakdown for LLM provider keys specifically, so treat these numbers as the general secrets problem that LLM apps inherit.
Keep secrets out of code, prompts and logs
Three places leak secrets in LLM projects. Source code and container images come first; the OWASP Secrets Management Cheat Sheet says secrets should never be hardcoded using docker ENV or docker ARG commands, and to make sure pipeline output does not leak secrets.
The second is the prompt. OWASP LLM07:2025 System Prompt Leakage says sensitive data such as credentials and connection strings should not be in the system prompt language. LLM02:2025 lists security credentials among sensitive information types, and warns that restrictions written into a system prompt may not always be honored and could be bypassed via prompt injection or other methods.
The practical rule: your backend holds the key and calls the provider or the tool. The model only sees the result. If a tool needs a credential, the tool code attaches it; the prompt never contains it.
Store them in a secrets manager
OpenAI recommends environment variables or a secret management service. The OWASP cheat sheet goes further and says to centralize the storage, provisioning, auditing, rotation and management of secrets. A managed service does that for you. Two examples with published details:
- AWS Secrets Manager. Its pricing page uses examples of $0.40 per secret per month and $0.05 per 10,000 API calls. It says new customers starting July 15, 2025 get up to $200 in AWS Free Tier credits.
- HashiCorp Vault. Its database secrets engine generates credentials dynamically based on configured roles, with a default TTL of 1 hour and a max TTL of 24 hours. The pattern applies to a database that your LLM app queries.
Google Cloud, Azure and other clouds have equivalents; check your own provider's current pricing, which is not compared here.
One scoped key per app, with an expiry
OpenAI says to use a project service account for an application's identity instead of depending on an individual developer's key, to set an expiration date when you create a project API key, and to establish a regular key rotation process. It also describes spend alerts for a dollar threshold and a hard spend limit to enforce a monthly cap.
Anthropic's Admin API shows the same idea in its key model: each key has a name, an expiry (null if it never expires), a principal that is a user or a service account, a scope that is a Workspace or the organization, and a status of active, archived, expired or inactive. The endpoint never returns the key's secret value.
- Per-app keyName it after the app and environment so a leak points at the right place
- ExpirySet one when the provider allows it
- Spend capUse the provider alert and hard limit features OpenAI describes
Rotate, and prefer short-lived credentials
The OWASP cheat sheet says to regularly rotate secrets so that stolen credentials only work for a short time, and that dynamic secrets should be used where possible to reduce the surface area of credential reuse. It gives no fixed rotation interval, and neither does this page; pick one your team can actually follow and automate it.
Rotation is easiest when your app reads the key at start-up or on refresh from the secrets manager, not from a value copied into config files. Vault says its leasing mechanism helps you roll keys, and that its internal revocation system makes credentials invalid within a reasonable time of the lease expiring.
Control who can read them and log access
The OWASP cheat sheet says engineers should not have access to all secrets in the secrets management system, so give each service and person only the secrets they need. It also says auditing must be implemented securely to resist tampering or deletion of logs, and that logs should be queryable for at least 90 days.
Detect leaks before and after they ship
Catch them early: the OWASP cheat sheet recommends running secrets detection at the developer level, before commit, through IDE checks or pre-commit hooks, and creating detection rules for each stage of the secret lifecycle.
On GitHub, secret scanning scans your entire Git history on all branches for hardcoded credentials, and GitHub partners with service providers to validate detected secrets and notify them so they can revoke the credential. Push protection will block the push and provide a detailed message explaining the reason for the block.
If a key leaks: revoke it, issue a new one from the secrets manager, check provider usage logs for the exposure window, and remove it from Git history. Deleting the file is not enough, because secret scanning reads your entire Git history.
A short checklist
- No keys in code, images, prompts or logs. Backend only.
- Central secrets manager. Reads at runtime, access audited.
- One service-account key per app and environment. With an expiry and a spend cap.
- Rotation you automate. Interval chosen by your team.
- Scanning on. Pre-commit plus push protection.
- A revocation runbook. Know who revokes what, and where the logs are.
Questions people ask
Should I put an API key in the system prompt?
No. OWASP says credentials and connection strings should not be in the system prompt and should be externalized to systems the model does not directly access. See LLM07:2025.
Is an environment variable enough?
OpenAI accepts environment variables or a secret management service. The OWASP cheat sheet recommends centralizing storage, rotation, auditing and management, which a secrets manager does better than scattered environment files.
How often should I rotate LLM API keys?
No source here gives a fixed interval. OWASP says to rotate regularly so stolen credentials work only for a short time, and OpenAI strongly recommends setting an expiration date and a regular rotation process.
Can I use one key for the whole team?
Avoid it. OpenAI says to use a project service account for an application's identity instead of an individual developer's key, and Anthropic's Admin API models keys per principal and per Workspace.
How do I stop a leaked key being abused?
Revoke it, replace it, and review usage. Set provider spend alerts and a hard spend limit, as OpenAI describes, so a leak has a ceiling.
Does GitHub catch leaked keys automatically?
Secret scanning detects hardcoded credentials in your Git history, and push protection can block a push containing one, per GitHub Docs. Turn them on and keep a pre-commit check as well.
Sources
- OWASP Secrets Management Cheat Sheet · Centralize, rotate, least privilege, audit, detection
- OWASP Top 10 for LLM Applications 2025 · The 2025 list of ten LLM risks
- OWASP LLM07:2025 System Prompt Leakage · Keep credentials out of the system prompt
- OWASP LLM02:2025 Sensitive Information Disclosure · Credentials as sensitive data; prompt restrictions can be bypassed
- OpenAI: Production best practices · Environment variables, service accounts, expiry, rotation, spend limits
- Anthropic: Retrieve API Key (Admin API) · Key scope, principal, expiry and status model
- HashiCorp Vault: Database secrets engine · Dynamic credentials, TTL and revocation
- AWS Secrets Manager pricing · Per-secret and per-call example prices
- GitHub Docs: About secret scanning · Scans full Git history; provider notification
- GitHub Docs: About push protection · Blocks pushes containing secrets
- GitGuardian: State of Secrets Sprawl 2025 · Public GitHub leak statistics
- Photo: Buffy May / CC BY-SA 2.0 via Wikimedia Commons · Vault door image at the top of this page
Figures were read from the linked pages on November 7, 2026. Prices and features change; this is general guidance, not a security audit.
Top photo: Buffy May / CC BY-SA 2.0 via Wikimedia Commons