GlossarySecurity & ComplianceUpdated 2026-03-16

API Key Management

Securing, rotating, scoping, and tracking API credentials across AI providers. Effective key management is the foundation of both cost attribution and security — every unmanaged key is a potential source of untracked spend and unauthorized access.

Definition

What is API Key Management?

API key management encompasses the full lifecycle of API credentials used to authenticate with LLM providers: creation (generating keys with appropriate scopes and permissions), storage (encrypting keys at rest and limiting access), distribution (securely delivering keys to applications and team members), monitoring (tracking which keys are being used, by whom, and how much they cost), rotation (periodically replacing keys to limit the blast radius of compromises), and revocation (immediately disabling keys that are compromised or no longer needed). In the context of AI cost management, API key management takes on additional significance because keys are the primary unit of cost attribution. Each key maps to a billing account at the provider level, and without per-key tracking, you cannot determine which team, service, or feature is responsible for which portion of your AI spend. Organizations that manage AI API keys casually — sharing a single key across teams, storing keys in plaintext configuration files, never rotating — face both security risks (unauthorized usage, key leakage) and financial risks (unattributed costs, budget overruns with no accountability).

Impact

Why It Matters for AI Costs

API keys are simultaneously the most critical and most mismanaged component of AI infrastructure. A 2025 GitGuardian report found that 12.8 million API secrets were exposed in public GitHub repositories that year, with cloud provider and AI API keys among the most common types. The consequences of key mismanagement are both immediate and compounding:

Security consequences:

  • A leaked OpenAI API key can be exploited within minutes. Automated scanners continuously search GitHub, Pastebin, and other public repositories for valid API keys. Once found, the key is used to generate tokens at maximum speed — often racking up thousands of dollars in charges before the owner notices.
  • In January 2026, a mid-size SaaS company reported $47,000 in unauthorized OpenAI charges after a developer accidentally committed an API key to a public repository. The key was exploited within 8 minutes of the commit.
  • Shared keys create accountability gaps. If five developers share one API key and one of them leaks it, there is no audit trail to identify the source of the leak.

Cost consequences:

  • Without per-key cost tracking, you cannot answer the most basic financial question: "Which team or feature is responsible for this month's $85,000 AI bill?" This makes budgeting, forecasting, and cost optimization impossible.
  • Orphaned keys — keys that are still active but no longer used by any production service — continue to accrue charges if they are compromised or used by forgotten scripts. A survey of CostHawk customers found that 23% of active API keys had zero legitimate usage in the past 30 days but had not been revoked.
  • Over-permissioned keys allow any service to use any model, making it impossible to enforce model routing policies or budget limits at the key level.

CostHawk addresses both dimensions by providing per-key cost tracking through wrapped keys, automated alerts for unusual key usage patterns, and integration with key rotation workflows to ensure that cost attribution remains accurate even as keys are rotated.

What is API Key Management?

API key management is the discipline of controlling the full lifecycle of API credentials — from creation through revocation — to ensure that keys are secure, properly scoped, traceable, and compliant with organizational policies. For AI API keys specifically, this discipline includes cost-aware practices that traditional API key management does not address.

The lifecycle of an AI API key has six stages:

  1. Creation. Keys are generated at the provider (OpenAI, Anthropic, Google) with specific scopes and labels. Best practice is to create separate keys for each team, service, and environment (development, staging, production) to enable granular cost attribution and limit blast radius.
  2. Storage. Keys must be stored encrypted at rest. Never store API keys in plaintext in configuration files, environment variables on shared systems, or source code. Use a secrets manager (AWS Secrets Manager, HashiCorp Vault, 1Password) or an encrypted environment variable system. CostHawk encrypts all stored provider keys with AES-256-CBC using unique initialization vectors per key.
  3. Distribution. Keys must reach the applications that need them without being exposed in transit. Inject keys through CI/CD pipeline secrets, Kubernetes secrets, or runtime secrets managers — never through chat messages, email, or shared documents. Each team member should have access only to the keys they need (principle of least privilege).
  4. Monitoring. Track key usage in real time: which key is making requests, to which models, at what volume, and at what cost. CostHawk's wrapped-key system provides this monitoring automatically — every request is logged with the wrapped key ID, model, token counts, and computed cost.
  5. Rotation. Replace keys periodically (quarterly at minimum, monthly for high-value keys) to limit the window of exposure if a key is compromised. Rotation must be seamless — the new key must be deployed to all consuming services before the old key is revoked.
  6. Revocation. Immediately disable keys that are compromised, no longer needed, or associated with departed employees. Provider dashboards allow instant revocation, but you must also update all services that used the revoked key to prevent service disruptions.

Most organizations handle stages 1 and 2 reasonably well (because developers need keys to work and know not to commit them to Git), but stages 3–6 are often neglected. The result is a sprawl of untracked, unrotated, over-permissioned keys that create both security and cost liabilities.

Why Key Management Matters for Costs

API keys are the natural boundary for cost attribution in AI infrastructure. Every API request is authenticated with a key, and the provider's billing system tracks usage per key (or per organization/project, depending on the provider). By controlling how keys are created and assigned, you control the granularity of your cost data.

Per-key cost tracking enables:

  • Team-level chargebacks. Issue separate keys (or CostHawk wrapped keys) to each team. Monthly cost reports show exactly how much each team spent, enabling budget accountability. Engineering Team A spent $12,000 on Claude 3.5 Sonnet for code review; Product Team B spent $8,000 on GPT-4o for customer chatbots. Without per-key tracking, these costs are a single $20,000 line item with no attribution.
  • Service-level optimization. When each microservice has its own key, you can identify which services are the biggest cost drivers and target them for optimization. A common discovery is that a single service — often a batch processing pipeline or internal tool — accounts for 40–60% of total AI spend despite serving a small fraction of users.
  • Environment cost separation. Separate keys for development, staging, and production environments reveal how much of your AI spend is non-production. CostHawk customers typically find that development and testing account for 20–35% of total AI API costs — an immediate optimization target. Reducing dev environment spending through model downgrading, request capping, or mock responses can save thousands per month.
  • Anomaly detection. Per-key usage baselines enable cost anomaly detection. If a production key that normally processes 50,000 requests per day suddenly jumps to 500,000, CostHawk flags the anomaly and alerts the key owner. Without per-key tracking, this spike is buried in aggregate metrics and may not be noticed until the monthly bill arrives.

Abuse prevention. Unmanaged keys are a cost liability because they can be used without accountability. A developer running personal experiments on a shared production key, a contractor retaining key access after their engagement ends, or a compromised key being used by an external attacker — all of these scenarios generate real costs with no legitimate business value. Per-key management with budget caps, usage alerts, and regular access reviews prevents these scenarios. CostHawk wrapped keys can be configured with daily and monthly spending limits that hard-stop requests once the budget is exhausted, preventing any single key from generating unbounded costs.

Key Management Best Practices

These eight practices represent the standard of care for AI API key management in production environments. Implementing all eight eliminates the most common sources of key-related security incidents and cost overruns.

1. One key per scope. Never share a single API key across multiple teams, services, or environments. Create separate keys for each logical boundary. At minimum, maintain separate keys for: each production service, each non-production environment, each team or business unit, and each external integration. This limits blast radius (a compromised key only affects one scope) and enables granular cost attribution.

2. Least privilege. Scope keys to the minimum permissions required. If a service only needs access to GPT-4o mini, do not give it a key that can access GPT-4o or o1. Most providers do not offer model-level key scoping natively, but proxy layers like CostHawk wrapped keys can enforce model restrictions at the proxy level — requests for unauthorized models are rejected before reaching the provider.

3. Encrypt at rest. Store all API keys using a secrets manager with encryption at rest. AWS Secrets Manager, Google Cloud Secret Manager, HashiCorp Vault, and 1Password all provide this capability. Never store keys in plaintext environment files on shared servers, CI/CD configuration visible to all developers, or application configuration files — even if those files are in private repositories.

4. Rotate regularly. Rotate keys every 90 days at minimum, every 30 days for high-value production keys. Rotation should be automated: a scheduled job generates a new key at the provider, updates the secrets manager, deploys the new key to consuming services, and revokes the old key after a grace period (24–48 hours to ensure all services have picked up the new key).

5. Monitor usage. Track every request made with every key. Alert on anomalies: sudden usage spikes, usage from unexpected IP addresses, usage of models that the key's owner should not be using, and usage outside of normal business hours. CostHawk provides this monitoring out of the box for wrapped keys, with configurable alerts for cost thresholds, usage spikes, and unusual patterns.

6. Audit access. Maintain a log of who has access to which keys, when access was granted, and by whom. Review access quarterly: revoke keys for departed employees, decommissioned services, and completed projects. The access review should be a formal process, not a best-effort cleanup.

7. Respond to compromises immediately. If a key is suspected of being compromised — exposed in a public repository, included in a log file, or sent via insecure channel — revoke it immediately and generate a replacement. Do not wait to confirm the compromise; the cost of a false positive (brief service disruption during key swap) is far less than the cost of a real compromise (unauthorized charges, data exposure).

8. Budget caps per key. Set spending limits on every key. A production key might have a $5,000/day cap; a development key might have a $50/day cap. These caps prevent runaway costs from bugs, abuse, or compromises. CostHawk wrapped keys support configurable budget caps with hard enforcement — the proxy rejects requests once the cap is reached.

Per-Key Cost Attribution

Per-key cost attribution is the practice of tracking AI API costs at the individual API key level, enabling precise allocation of costs to teams, projects, features, and environments. This is the foundation of AI cost governance — without it, the monthly AI bill is an opaque aggregate that cannot be optimized because no one knows which component is responsible for which portion.

How per-key attribution works:

At the provider level, most providers track usage per API key or per organization/project:

  • OpenAI tracks usage per organization and per project. You can create multiple projects within an organization, each with its own API key and usage dashboard.
  • Anthropic tracks usage per workspace. Separate workspaces have separate billing and usage tracking.
  • Google tracks usage per GCP project with detailed billing exports to BigQuery for analysis.

However, provider-level tracking has limitations: it cannot cross-reference usage across providers, it cannot attribute costs to specific features or code paths within a service, and the granularity depends on how many keys/projects/workspaces you create at the provider level.

CostHawk's wrapped-key system adds a layer of attribution on top of provider-level tracking:

// Team A's wrapped key for their code review service
const teamAClient = new OpenAI({
  apiKey: "ch_team_a_code_review_sk_...",
  baseURL: "https://proxy.costhawk.com/v1"
})

// Team B's wrapped key for their customer chatbot
const teamBClient = new OpenAI({
  apiKey: "ch_team_b_chatbot_sk_...",
  baseURL: "https://proxy.costhawk.com/v1"
})

// Both use the same underlying provider key,
// but CostHawk tracks costs separately per wrapped key

This approach has several advantages over provider-level attribution:

  • Unlimited granularity. Create as many wrapped keys as you need — per team, per service, per feature, per environment — without creating separate provider accounts or projects.
  • Cross-provider aggregation. CostHawk normalizes costs across all providers, so you can see that Team A spent $8,000 total ($5,000 on OpenAI + $3,000 on Anthropic) without manually aggregating across provider dashboards.
  • Real-time tracking. Costs are computed per-request and available in the dashboard within seconds, rather than waiting for end-of-month billing statements.
  • Tagging and metadata. Wrapped keys can be tagged with arbitrary metadata (team name, project code, cost center) that flows into analytics and reporting. This enables finance-oriented views like cost-per-cost-center or cost-per-customer.

The operational impact of per-key attribution is significant. When teams can see their own spending and compare it to their budget, they self-optimize. CostHawk customers report a 15–20% reduction in AI spend within the first quarter of enabling per-key dashboards, driven entirely by teams identifying and eliminating their own waste — no top-down mandates required.

Key Rotation Without Downtime

Key rotation — periodically replacing API keys with new ones — is a critical security practice that limits the window of exposure if a key is compromised. The challenge is rotating keys without disrupting the services that depend on them. Here is a zero-downtime rotation procedure for AI API keys:

Step 1: Generate the new key. Create a new API key at the provider with the same permissions and scopes as the existing key. Do not revoke the old key yet — both keys must be valid simultaneously during the transition.

Step 2: Update the secrets manager. Store the new key in your secrets manager alongside the old key. Tag the new key as "pending rotation" and record the rotation timestamp for audit purposes.

Step 3: Deploy the new key to all consuming services. Update environment variables, Kubernetes secrets, or configuration files in all services that use this key. For containerized deployments, this typically means updating the secret and triggering a rolling restart. For serverless functions, update the environment variable in the deployment configuration.

# Kubernetes secret update (zero-downtime with rolling restart)
kubectl create secret generic openai-key \
  --from-literal=OPENAI_API_KEY="sk-new-key-abc123" \
  --dry-run=client -o yaml | kubectl apply -f -

# Trigger rolling restart of deployments that use this secret
kubectl rollout restart deployment/chatbot-service
kubectl rollout restart deployment/code-review-service

Step 4: Verify the new key is in use. Monitor API request logs to confirm that all services are now authenticating with the new key. CostHawk's per-key usage dashboard makes this easy — you should see traffic shifting from the old wrapped key to the new one over the course of the rolling restart.

Step 5: Grace period. Wait 24–48 hours after all services have migrated to the new key. This grace period catches edge cases: long-running batch jobs that cached the old key in memory, services in other time zones that deploy on a different schedule, and any services you may have missed in Step 3.

Step 6: Revoke the old key. After the grace period, revoke the old key at the provider and remove it from the secrets manager. Record the revocation in your audit log.

Automating rotation with CostHawk wrapped keys. CostHawk wrapped keys simplify rotation because the wrapped key itself does not change — only the underlying provider key is rotated. Your services continue to use the same wrapped key (ch_team_a_sk_...) while CostHawk's backend swaps the underlying provider key. This means zero configuration changes in consuming services: update the provider key in CostHawk's dashboard, and all traffic through that wrapped key immediately uses the new provider key. This is the lowest-friction rotation model available and is one of the primary operational benefits of the proxy architecture.

API Key Management with CostHawk

CostHawk provides a comprehensive key management layer on top of your existing provider API keys, combining security best practices with cost tracking and policy enforcement. Here is how CostHawk's key management capabilities map to the key lifecycle:

Key onboarding. When you add a provider API key to CostHawk, it is encrypted with AES-256-CBC using a unique initialization vector and stored in CostHawk's secure database. The raw key is never logged, never included in API responses, and never accessible to CostHawk support staff. You can add keys from multiple providers (OpenAI, Anthropic, Google) and manage them all from a single dashboard.

Wrapped key generation. For each provider key, you can generate multiple wrapped keys with different scopes and policies:

  • Team-scoped keys: ch_engineering_sk_..., ch_product_sk_..., ch_data_science_sk_... — each team gets its own cost tracking namespace.
  • Service-scoped keys: ch_chatbot_prod_sk_..., ch_code_review_prod_sk_... — each service has independent budget and monitoring.
  • Environment-scoped keys: ch_dev_sk_..., ch_staging_sk_..., ch_prod_sk_... — separate environments with different budget caps (low caps for dev, higher for production).

Policy enforcement. Each wrapped key can be configured with policies that the proxy enforces on every request:

PolicyDescriptionExample
Budget capMaximum daily or monthly spend$100/day for dev keys, $5,000/day for production
Rate limitMaximum requests per minute100 RPM for internal tools, 1,000 RPM for production APIs
Model allowlistRestrict which models the key can accessDev keys limited to GPT-4o mini and Haiku only
IP allowlistRestrict key usage to specific IP rangesProduction keys only usable from VPC CIDR ranges

Usage monitoring and alerts. CostHawk tracks every request per wrapped key in real time. The dashboard shows: total spend per key (daily, weekly, monthly), token consumption by model, request volume and error rates, latency percentiles, and cost anomaly detection. You can configure alerts for: daily spend exceeding a threshold, sudden usage spikes (2x or more above the 7-day average), new models being accessed by a key for the first time, and keys with zero usage for 30+ days (candidates for revocation).

Rotation support. When you rotate the underlying provider key, update it in CostHawk's dashboard and all wrapped keys immediately use the new provider key. No changes needed in any consuming service. CostHawk also tracks provider key age and can alert you when a key is overdue for rotation based on your configured rotation policy (30, 60, or 90 days).

Audit trail. Every administrative action is logged with the actor, timestamp, and details: key creation, policy changes, budget modifications, key revocation. This audit trail supports compliance requirements (SOC 2, HIPAA, SOX) and provides forensic data for incident investigation.

FAQ

Frequently Asked Questions

How many API keys should I create for my organization?+
Create one key per logical boundary: each team, each production service, each non-production environment, and each external integration should have its own key. A typical mid-size engineering organization with 5 teams and 10 services might have 30–50 keys: 10 production service keys, 10 staging service keys, 10 development keys (one per service or one per developer), 5 team-level keys for ad-hoc usage, and a handful of keys for external integrations and CI/CD pipelines. This sounds like a lot, but it is essential for granular cost attribution and blast radius containment. If managing many provider keys directly feels burdensome, use CostHawk wrapped keys — you can generate unlimited wrapped keys from a single provider key, with each wrapped key getting its own cost tracking, budget cap, and usage monitoring. This gives you fine-grained attribution without the overhead of managing dozens of keys at the provider level.
What should I do if an API key is exposed in a public repository?+
Treat it as a security incident requiring immediate response. First, revoke the key at the provider immediately — do not wait to assess the damage. OpenAI, Anthropic, and Google all support instant key revocation from their dashboards. Second, generate a new replacement key and deploy it to all services that used the compromised key. Third, audit the billing dashboard for any unauthorized usage between the time of exposure and revocation. Fourth, investigate the root cause: was the key hardcoded in source code, included in a configuration file that was not gitignored, or exposed through a CI/CD log? Fifth, implement preventive measures: install a pre-commit hook (like git-secrets or detect-secrets) that scans for API key patterns before allowing commits, configure GitHub's secret scanning alerts, and review your key storage practices. CostHawk wrapped keys add a layer of protection: even if a wrapped key is exposed, you can revoke it instantly in CostHawk's dashboard without touching the underlying provider key, and the attacker never had access to the real provider key.
How often should I rotate API keys?+
Rotate production API keys every 90 days at minimum, with 30-day rotation for high-value keys that access expensive models or handle sensitive data. The rotation frequency should be based on risk assessment: keys that access frontier models (GPT-4o, Claude 3.5 Sonnet) with high spending limits should rotate more frequently because the financial impact of a compromise is greater. Keys that are distributed to many people (team-level development keys) should rotate more frequently because the probability of exposure increases with distribution. Keys that are used in automated pipelines with no human access can rotate less frequently. The most important thing is that rotation is automated and tested — a manual rotation process that requires a developer to remember to rotate keys every quarter will inevitably fall behind. Use your secrets manager's built-in rotation features or a simple cron job to automate the process. CostHawk tracks provider key age and alerts you when keys are overdue for rotation.
Is it safe to use environment variables for API keys?+
Environment variables are an acceptable storage mechanism for API keys in many deployment contexts, but they are not universally safe. In containerized deployments (Docker, Kubernetes), environment variables injected from a secrets manager at runtime are reasonably secure — the key exists only in the container's memory and is not persisted to disk. In serverless functions (AWS Lambda, Vercel), encrypted environment variables are the standard approach and are secure. However, environment variables are NOT safe in these contexts: shared servers where multiple users can inspect process environments via /proc, CI/CD systems that log environment variables in build output, local development machines where .env files may be accidentally committed to version control, and any system where printenv output is captured in logs. For maximum security, use a runtime secrets manager that injects keys at application startup rather than relying on environment variables. If you must use environment variables, ensure they are encrypted at rest, never logged, and sourced from a secrets manager rather than hardcoded in deployment configurations.
How do I track costs when multiple services share a provider API key?+
If multiple services share a single provider API key, the provider's billing dashboard shows aggregate usage with no per-service breakdown. There are two solutions: (1) Create separate provider keys for each service. This is the cleanest approach but requires managing more keys at the provider level and may not be practical if you are near the provider's per-organization key limit. (2) Use CostHawk wrapped keys. Generate a unique wrapped key for each service, all backed by the same provider key. CostHawk's proxy tracks usage per wrapped key, giving you per-service cost attribution without creating additional provider keys. This is the recommended approach because it decouples your attribution granularity from your provider key management. You can have 50 wrapped keys for 50 services all flowing through a single OpenAI API key, with full per-service cost tracking, budget caps, and anomaly detection. The provider sees one key; CostHawk sees 50 distinct cost centers.
What is the difference between API key scoping and budget caps?+
API key scoping and budget caps are complementary controls that operate at different levels. Scoping restricts what a key can do — which models it can access, which endpoints it can call, which IP addresses it can be used from. Scoping is a security control that prevents misuse by limiting the key's capabilities. Budget caps restrict how much a key can spend — the maximum dollar amount per day, week, or month. Budget caps are a financial control that prevents cost overruns by limiting the key's economic impact. A well-managed key has both: it is scoped to only the models and endpoints the owning service needs (least privilege), and it has a budget cap that matches the service's expected spending pattern with a reasonable buffer. For example, a development key might be scoped to GPT-4o mini only (no access to expensive frontier models) with a $25/day budget cap. A production chatbot key might be scoped to GPT-4o and Claude 3.5 Sonnet with a $2,000/day budget cap. CostHawk wrapped keys support both scoping (model allowlists) and budget caps, enforced at the proxy level.
How do I manage API keys across multiple LLM providers?+
Managing keys across multiple providers (OpenAI, Anthropic, Google, Mistral, etc.) creates a key sprawl problem: each provider has its own dashboard, its own key format, its own rotation process, and its own usage tracking. Without centralization, you end up checking four different dashboards to understand your total AI spend. The solution is a centralized key management layer that abstracts away provider differences. CostHawk serves this role: you add all your provider keys to CostHawk, generate wrapped keys for each service, and manage everything from a single dashboard. Key rotation, cost tracking, budget enforcement, and anomaly detection work identically regardless of which provider the underlying key belongs to. For organizations that prefer infrastructure-level solutions, a secrets manager like HashiCorp Vault with custom metadata tags for each key's provider, team, and environment provides centralized storage and rotation automation. The critical requirement is that all keys — regardless of provider — are tracked in a single inventory with consistent lifecycle policies.
What compliance standards require API key management?+
Several compliance frameworks mandate or strongly recommend API key management practices: SOC 2 Type II requires access controls, monitoring, and key rotation as part of the Common Criteria for security, availability, and confidentiality. If your organization handles customer data through AI APIs, your SOC 2 auditor will ask about key management. HIPAA requires that access to systems containing protected health information (PHI) be controlled and auditable. If your AI application processes PHI (medical chatbots, clinical note summarization), API keys must be managed with the same rigor as database credentials. PCI-DSS requires key rotation and access controls for systems in the cardholder data environment. ISO 27001 includes key management as part of its cryptographic controls (Annex A.10). Even if your organization is not formally pursuing these certifications, implementing their key management requirements is best practice. CostHawk's audit trail, RBAC, and key lifecycle tracking provide the documentation and controls that auditors look for during compliance assessments.

Related Terms

AI Cost Glossary

Put this knowledge to work. Track your AI spend in one place.

CostHawk gives engineering teams real-time visibility into every token, every model, and every dollar across your AI stack.