🔑
Security

Stolen LLM API key: how to cap your costs

27.11.2026
← All articles

Many websites now call large language model (LLM) APIs for chatbots, text generation or product descriptions. These services bill by tokens used, which means the key owner pays no matter who is using the key. A stolen API key is therefore not just a data leak: a stranger can send requests around the clock at your expense and push the bill to a large amount within hours. Below we look at how keys leak, what to do right after a leak and how to limit spending in advance.

Where keys usually leak from

The most common case is a key written straight into frontend code. To ship a feature quickly, a developer pastes the key into a JavaScript file, and any visitor can open the browser developer tools and copy it. The second path is committing the key to a Git repository. Even if it is removed in the next commit, it stays in history, and bots that scan public repositories find such keys quickly. A third risk is a .env file reachable over the web: if the document root is misconfigured, a bot requesting https://example.uz/.env reads every secret in it.

Example: a Tashkent online store chatbot

Imagine an appliance store in Tashkent adding a chatbot that answers product questions. To save time, the developer embedded the API key in a page script. A few days later the provider console showed a sharp jump in requests, most of them unrelated to the store. The key had spread as a "free" resource for other people's projects. Without a spending limit on the account, this ends with an unexpected large invoice at the end of the month.

The first hour after you notice a leak

Revoke the key immediately. Postponing the swap is a mistake because every minute costs money. Then work through the rest:

  1. Create a new key and store it only on the server, in .env or an environment variable.
  2. Review the usage and billing pages in the provider console.
  3. Search the repository, logs and frontend files for traces of the old key.
  4. If the amount is significant, contact the provider's support.

Secret scanners make the repository check easy. The open-source gitleaks tool walks through the entire commit history:

gitleaks detect --source . -v
git log -p | grep -n "sk-"

The second command is a plain search for a prefix many providers use. Prefixes differ between services, so confirm the exact format in your provider's documentation.

Set a spending cap in advance

Most major LLM providers let you configure a monthly spending limit, budget alerts or project-level limits in their console. Names and behaviour vary, so read your provider's docs. The principle is simple: set a hard limit slightly above your real need and an email alert well below it. Then even a stolen key cannot cost more than a predefined amount. Use a separate key per project and environment as well, so you can see each one's spending and revoke a problem key without breaking everything else.

Keep the key out of the browser with a server proxy

In a sound design the browser never talks to the LLM provider directly. The user's request goes to your server, which validates it, applies limits and only then forwards it with the key. Here is a simplified PHP example that reads the key from the environment and allows at most 10 requests per minute per IP:

<?php
$key = getenv('LLM_API_KEY');
$ip  = $_SERVER['REMOTE_ADDR'];
$f   = sys_get_temp_dir() . '/rl_' . md5($ip . date('YmdHi'));
$n   = (int) @file_get_contents($f);
if ($n >= 10) { http_response_code(429); exit('Too many requests'); }
file_put_contents($f, $n + 1);

$msg = mb_substr(trim($_POST['message'] ?? ''), 0, 1000);
// the request to the provider is sent here using $key

The message is also trimmed to 1000 characters, since long inputs burn more tokens; limit the response length through API parameters too. In production, Redis or a database is a more reliable place to keep such counters.

Make .env unreadable over the web

The best option is to keep secrets outside the document root. If that is not possible, block access to dotfiles in the web server:

# Apache (.htaccess)
<FilesMatch "^\.(env|git)">
    Require all denied
</FilesMatch>

# Nginx
location ~ /\.(env|git) {
    deny all;
    return 404;
}

Verify with curl -I https://example.uz/.env; the answer should be 403 or 404. When deploying a project to sayt.uz hosting, it is worth running this check at least once.

Ongoing habits

Once a month, review the usage graph in the provider console, delete unused keys and rotate keys that former team members had access to. Never send keys over Telegram or email; use a password manager or server-side secrets instead. That way a leak gets noticed quickly and the damage stays within the cap you set.

Related articles

🗝️ Don't keep passwords and keys in code: managing secrets properly 🔐 Protecting WordPress from Brute Force with fail2ban 🦠 Malware Scanners on Hosting: How They Work and How to Read Results 🔁 Cleaned Site Hacked Again? The Entry Point Is Still Open
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓