Provider keys for LLMs and vector or data services need secure storage, rotation, scoping and monitoring. A leaked key means runaway spend and data exposure, in addition to an outage.

Where provider keys live and how they are scoped

Provider key & secret managementDiagram with these labels, in reading order: store keys; route calls; forward requests; log usage; trigger alerts; Secrets Manager; (HashiCorp, AWS); Scoped Keys; & Rotation; API Gateway; (Proxy Layer); API Calls; to LLM Provider; Monitoring; & Alerts.store keysroute callsforward requestslog usagetrigger alertsSecrets Manager(HashiCorp, AWS)Scoped Keys& RotationAPI Gateway(Proxy Layer)API Callsto LLM ProviderMonitoring& Alerts
Key Management Workflow
  1. Store keys in a secrets manager such as HashiCorp Vault or AWS Secrets Manager. This keeps them out of code and client bundles, where hardcoded secrets are a major risk.
  2. Issue project-specific keys limited to the resources each project needs, so one compromised key does not open everything.
  3. Rotate keys regularly. A leaked key then has a limited lifespan, which cuts the risk of long-term exploitation.
  4. Send API calls through a gateway or proxy such as LiteLLM or Cloudflare AI Gateway, so keys stay on the server side.

Rotate automatically. Manual rotation tends to leave outdated keys and security gaps.

Gateway virtual keys, rate limits and spend caps

A gateway can issue virtual keys per team, apply rate limits and log usage. You can see what each team spends. Per-key spend limits and budget alerts warn you as a key nears its cap, and the gateway can throttle a team that exceeds its budget.

Add a gateway when the architecture grows to multiple teams or applications, or when you need per-tenant cost attribution. Always use a secrets manager as the baseline. Never call provider APIs directly from browsers or mobile clients, because that exposes the key.

One shared key in an environment variable

The simple approach is a single shared key in an environment variable. Its limits are severe:

  • Rotation requires a redeployment.
  • A committed .env file or a client-side API call can drain the account.
  • There is no per-team attribution and no spend cap.
  • Nothing contains the damage after a compromise.

Scoped keys behind a gateway allow separate limits and monitoring for each team.

Agents that try to exfiltrate keys

Standard secrets practice stores sensitive values and reads them when needed. Provider keys add two differences. They map directly to money, so a leak lets an attacker make API calls up to the account's spend limit and run up charges. And AI adds an attack path: through prompt injection, an agent may be steered into exfiltrating keys with crafted inputs. Conventional secrets management does not account for that.

Example scenario: a leaked key and unexpected spend

Suppose a leaked API key causes $50,000 in unauthorized spend. The amount is illustrative. A key hardcoded in a public repository, or shared in an unsecured environment, can be used by outside parties to send excessive requests.

Provider key & secret managementDiagram with these labels, in reading order: Key rotation issue; Access scope issue; Monitoring issue; API Key Exposure Risk; Check Key Rotation Policy; Check Access Scoping; Check Monitoring Alerts; Implement Automated; Rotation; Restrict Key Permissions; Enhance Monitoring System.Key rotation issueAccess scope issueMonitoring issueAPI Key Exposure RiskCheck Key Rotation PolicyCheck Access ScopingCheck Monitoring AlertsImplement AutomatedRotationRestrict Key PermissionsEnhance Monitoring System
Key management failure diagnosis
  • Check API usage logs for abnormal patterns.
  • Verify where the API calls come from and identify unauthorized access.
  • Audit the codebase for hardcoded keys or insecure storage.

Rotate the compromised key immediately. That may cause downtime or require updates in dependent services. Keep monitoring API usage for spikes. The result of this setup is a contained blast radius, cost control and clear attribution of usage to teams.

Back to the knowledge baseRequest an assessment