How to Detect Leaked LLM API Keys in Deployed Web Apps (2026)
LLM API keys leak through JavaScript bundles, source maps, and environment variables. Learn how automated security scanners find them before an attacker runs up your bill.
An LLM API key is the most expensive secret a web application can leak. A single exposed OpenAI key can generate thousands of dollars in charges before the billing alert fires. And the leak is almost always in the client-side code, where the key was placed during development and never removed.
Where LLM keys leak
The most common sources of leaked AI API keys in production web applications:
- JavaScript bundles. Environment variables prefixed with
NEXT_PUBLIC_,VITE_, orREACT_APP_are compiled into the client bundle. If the OpenAI key was set asNEXT_PUBLIC_OPENAI_API_KEYduring development, it ships in every build. - Source maps. Even when the bundle is minified, a deployed source map restores the original code, including hardcoded key strings that the minifier rearranged but did not remove.
- Server-side API routes that echo their configuration. A debug endpoint, a health check that dumps environment variables, or an error response that includes the stack trace with interpolated secrets.
- Git history exposed through
/.git/. If the.gitdirectory is served, every commit that ever contained the key is accessible. - Client-side analytics or logging. Telemetry that logs request headers or payloads can capture and transmit API keys to a third-party service.
What happens when a key leaks
An attacker who finds an LLM API key does not need to understand your application. They call the provider directly:
- Financial damage: GPT-4 and Claude calls are billed per token. An automated script can exhaust a $10,000 monthly budget in hours by generating long completions in a loop.
- Data exfiltration: If the key has access to fine-tuned models or retrieval-augmented generation (RAG) stores, the attacker can query your proprietary data.
- Reputation risk: The key can be used to generate harmful content attributed to your account. Provider abuse policies hold the account owner responsible.
How automated scanners detect leaked keys
Manual code review catches some leaks but misses what has already shipped. An automated security scanner like Keybrum tests the deployed application from the outside, the same way an attacker would:
- Bundle analysis. The scanner downloads every JavaScript file the app serves. It parses the code and matches patterns for known API key formats: OpenAI (
sk-), Anthropic (sk-ant-), Google AI (AIza), Cohere, Mistral, Replicate, and 80 other secret types. - Source map extraction. If a
.mapfile is accessible (linked in the bundle or at a predictable URL), the scanner reads it and scans the original source for hardcoded secrets. - Key verification. Finding a string that looks like a key is not enough. Keybrum calls the provider's lightest endpoint (list models, read balance) to confirm the key is active. A revoked key is reported as informational; a live key is a critical finding.
- Endpoint probing. Debug routes, exposed configuration files,
/.env,/.git/HEAD, and other common paths are tested for secret exposure.
Securing your LLM integration
The fix is straightforward once you know where the leak is:
- Move LLM calls to the server. The client sends a request to your own API route; that route calls OpenAI or Anthropic with the key stored in a server-only environment variable. The key never reaches the browser.
- Rotate every leaked key immediately. Removing the key from the code is not enough. The old value is still in the CDN cache, in browser caches, in search engine caches, and in anyone's notes who saw it. Rotate first, then fix the code.
- Set provider spending limits. OpenAI, Anthropic, and Google all support monthly budget caps. Set them to a value that limits the blast radius of a compromised key.
- Use scoped keys. If the provider supports project-scoped or model-restricted API keys, create a key that can only call the models your application needs. A key that can only call GPT-4o-mini is less dangerous than one that can call every model and read every fine-tune.
- Scan on every deploy. Run an automated scan in CI so leaked keys are caught before they reach production. Keybrum starts at EUR 49 per application and integrates with any CI pipeline through its CLI.
The Tokenwise angle: monitoring after the scan
A security scanner tells you which keys leaked. Tokenwise tells you if someone is using them. Unusual spikes in token consumption, calls from unexpected IP addresses, or requests to models your app does not use are all signals that a leaked key is being exploited.
The two tools complement each other: scan your deployed application with Keybrum's free security check to find the leaks, then monitor your LLM spend with Tokenwise to catch exploitation in real time.
Further reading
- Keybrum: web app security scanner — automated pentest with 300+ attack checks including LLM key detection
- Secure LLM API Keys in Production — the companion guide on key rotation and endpoint hardening
More guides
- The hidden cost of a security breach on your AI infrastructureA breach on an LLM app costs more than the tokens a thief burns. Stolen keys, exposed prompts, downtime and cleanup add up. The full bill, and the price of prevention.
- AI agent security: protecting tool-calling endpoints from abuseAI agents that call tools add three doors to your app: an open endpoint, prompt injection that triggers a tool call, and keys in config files. How to check each.
- How to Secure LLM API Keys and Endpoints in ProductionLLM API keys are the most expensive secrets in your codebase. This guide covers key rotation, endpoint hardening, proxy-layer security, and automated vulnerability scanning for AI-powered apps.
- How to Reduce OpenAI API Costs Without Hurting QualityLearn how to reduce OpenAI API costs with live spend tracking, risk-based routing, caching, framework controls, and a proxy baseURL setup.
- How to Reduce LLM Costs When Building with CursorLearn how to reduce LLM cost in Cursor with model routing, context trimming, caching, batching, and monitoring tactics I use in production.
- How to Reduce LLM Costs in LlamaIndex RAG AppsLearn how to reduce LLM cost in LlamaIndex with model routing, context trimming, caching, batching, and monitoring tactics that cut RAG spend fast.