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.

By Theo · Maker of Tokenwise

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_, or REACT_APP_ are compiled into the client bundle. If the OpenAI key was set as NEXT_PUBLIC_OPENAI_API_KEY during 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 .git directory 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:

  1. 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.
  2. Source map extraction. If a .map file is accessible (linked in the bundle or at a predictable URL), the scanner reads it and scans the original source for hardcoded secrets.
  3. 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.
  4. 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

More guides

Add this to your app in one line

Point your OpenAI baseURL at Tokenwise and every call is logged, priced, and optimizable — no SDK rewrite, no LangChain required.