Your app runs on a handful of keys: Stripe, your database, OpenAI or Anthropic, your email provider, your cloud account. Whoever holds one can charge cards, read customer data or run up a bill in your name. Now an AI coding agent sits on your developer's laptop with access to the same files those keys live in. So can it leak them? Yes, and in October attackers built fake AI tools specifically to collect coding agents' config files. A leaked live key can mean a surprise invoice, a data breach notice, or an awkward slide in investor due diligence.
The short answer#
Yes, an AI coding agent can leak your API keys, because it can read every file on the machine it runs on and send text to the internet. It usually happens because the key was somewhere it shouldn't be, not because the AI is malicious. The 10-minute check below finds the usual hiding places, and two settings (spending limits and restricted keys) make any leak much cheaper.
How a key gets out#
An agent reads files to understand your project, and .env is a file. From there, a key can leave in a few ways:
- A fake or poisoned tool steals it. The October "free AI credits" campaign rerouted a coding agent's traffic to the attackers, including files the agent read.
- Someone tricks the agent into publishing it. Instructions hidden in a GitHub issue once made an agent copy private data into a public pull request.
- The agent commits it. Asked to "make it work", an agent may hard-code a key into a file that gets pushed to GitHub.
- It sits in a transcript. Claude Code keeps session transcripts on disk, in plain text, for 30 days by default, so a key pasted into the chat stays on that laptop.
What it means for you: the agent doesn't need to be hacked for this to go wrong. A key in the wrong place plus an ordinary mistake is enough.
Where your keys actually hide#
Most founders think of "the code". Keys hide in more places than that, and each one is a separate check.
The 10-minute check#
Do these in order. You need a browser, access to your GitHub repository, and your developer on a call for the last two.
- Minute 1–2: the live site. Open your app, press F12, go to the Sources or Network tab and search for
sk_live,sk-,secretandservice_role. Anything found here is public. (Stripe'spk_publishable keys are designed to be public;sk_andrk_keys are not.) - Minute 3–4: the code on GitHub. In your repository, search for the same strings. GitHub's search only covers the current code, so ask the next question too.
- Minute 5: the history. Ask your developer: "Has a real key ever been committed, even if we deleted it later?" A deleted file is still in the history.
- Minute 6: laptops. Ask: "Does anyone's
.envhold live keys, or only test ones?" Stripe gives every account separate test and live keys for this reason. - Minute 7: chats. Search Slack or email for
sk_andkey=. A key pasted to "just get it working" is the one everyone forgets. - Minute 8: spend limits. Open each AI provider's billing page and check a monthly limit is set. Anthropic's console, for example, lets you set your own limit below your account's cap.
- Minute 9: Stripe key type. In Stripe's API keys page, check whether the app uses the all-powerful secret key or a restricted one.
- Minute 10: the rotation question. Ask: "If I asked you to replace every key today, how long would it take, and would anything go down?"
Each finding goes on a list. If the list has more than two items, or the last answer was "I'm not sure", that's the point where a second pair of eyes pays for itself. I do exactly this check; email me your repo link.
You found a leaked key. Now what?#
Don't panic, and don't just delete it from the code. A key that leaked stays leaked until it's replaced. Here's the order:
- Rotate it. Create a new key, put it where the app runs (your host's environment settings), and revoke the old one. Stripe lets old and new keys both work for up to 7 days after a rotation, so you can switch without downtime.
- Check what it was used for. Every provider shows usage. Stripe has request logs per key. Look for activity you don't recognise since the key was exposed.
- Look for siblings. If one key leaked, others may have taken the same route. Check every place in the diagram.
- If customer data was reachable, get help working out what was accessed before deciding who needs to be told.
What it means for you: a rotation done in a calm afternoon costs nothing. One done during an incident, with the old key still in use somewhere nobody remembers, can take your app down.
Make the next leak cheap#
You can't promise a key will never leak. You can make sure a leaked key can't do much:
- Spending limits on every AI and email key, so a stolen key costs you a capped amount, not an open-ended bill.
- Restricted Stripe keys. Stripe recommends restricted keys over secret keys for most uses, because you choose exactly what each one may do. A key that can only create checkout sessions can't issue refunds or read your customer list.
- Stripe access policies, which block a key used from anywhere except your servers and notify you.
- Test keys on laptops, live keys only on the server. This one change removes the biggest risk agents add.
- GitHub push protection, which blocks a push that contains a recognised secret. It's on by default for your own pushes to public repositories; private repositories need it switched on.
How I approach this#
When I take over an AI-built app, keys come first, before any feature work. That's the top rung of my what to fix first in an AI-built app ladder, and it's on my SaaS launch checklist for every go-live. For the wider picture of what an agent on a laptop can reach beyond keys, start with is it safe to let AI coding agents into your code.
Fix it yourself vs bring someone in#
| You can do this yourself | Bring someone in when… |
|---|---|
| Search the live site and repo for key prefixes | The history needs searching, or the key was in an old commit |
| Set spending limits on AI and email keys | You don't know which keys the app uses, or where they're set |
| Rotate one Stripe key with the 7-day overlap | Several keys need replacing across app, CI and workers without downtime |
| Ask your developer the rotation question | Usage logs show activity you don't recognise |
| Move to restricted Stripe keys for a new feature | The app is built around the full secret key and needs untangling |
Want me to check your keys?#
Send me your repo link and I'll check it for exposed keys, history included, and tell you which to replace first and in what order, so nothing goes down. Email mhuzaifaa.dev@gmail.com or see the projects I've shipped.
More on AI coding tools and your startup: is Claude Code safe to use at work? and how to onboard and offboard developers who use AI tools.
FAQ#
Can AI coding tools see my API keys?#
Yes, if the keys are in files on the machine where the tool runs, such as a .env file. Coding agents read project files to do their job. Keep live keys out of developer machines entirely and use test keys there, so there's nothing valuable for an agent to see.
How do I know if my API key has been leaked?#
Search your live site's code (F12) and your repository, including its history, for key prefixes like sk_live and sk-. Check each provider's usage page for activity you don't recognise. If a key was ever in Git, in the frontend or in a chat, treat it as leaked.
Is it safe to put API keys in a .env file?#
On a server, a .env file kept out of Git is a common and acceptable setup. On a developer laptop where AI agents run, only test keys should be in it. Live keys belong in your hosting provider's environment settings or a secrets manager.
What should I do if my Stripe secret key was exposed?#
Rotate it in Stripe's API keys page. The old and new keys both work for up to seven days, so you can update your app without downtime. Then check the key's request logs for activity you don't recognise, and consider replacing it with a restricted key.
Does deleting a key from GitHub fix the leak?#
No. The key stays in the repository's history and in any copy someone already made. Replace the key with a new one and revoke the old one. Cleaning the history is optional tidying; rotation is what actually closes the leak.
