TL;DR: If an AI agent leaked an API key, revoke or rotate the key at the provider first, before you delete anything. Then find the blast radius: every agent, Computer and repo that holds the key, and the one run that leaked it. Rotate the key in one place, clean the agent-specific copies (transcripts, run logs, comments, settings files), and only then rewrite git history.
What to do when an AI agent leaked your API key
When an AI agent leaked an API key, the first hour decides how much it costs you. A coding agent pushed a commit, wrote a pull request comment or echoed a curl command into a log, and a live credential went with it. Now it's public, or at least visible to more people than it should be.
The single-developer version of this incident is well documented. The team version is harder, because agents change three facts:
- One key, many holders. The same key often sits in the environment of several agents on several computers.
- More copies. Agents record what they ran. The key lands in transcripts, run output and settings files, not only in the commit.
- Unclear authorship. Nobody is sure which run leaked it, because several agents pushed to the same repo that afternoon.
This is the response runbook. If you want the prevention side, our guardrail checklist for coding agents leaking secrets covers who owns each control. This post assumes the leak already happened.
Minutes 0 to 5: revoke or rotate before anything else
Revoke the key at the provider. Deleting the commit, the comment or the log line doesn't undo the leak, because anyone who already copied the key still has it.
GitHub's guide to removing sensitive data from a repository puts it plainly: if the data is a secret, the first step is to revoke or rotate it. Once it's revoked, it can no longer be used for access, and that may be enough on its own.
Two choices at this step:
- Revoke now, accept downtime. Every agent using the key fails its next call. Pick this when the key reaches production data or billing.
- Issue a new key, then revoke the old one within minutes. Agents keep working while you swap. Pick this only when you can finish the swap fast.
Then open the provider's usage or audit view and look for calls you don't recognize. Write down the time window. You'll need it for the next step.
Minutes 5 to 20: find the blast radius and the run that leaked it
The blast radius is every place the key could be used from, plus every place it was written down. Map it before you rotate across the team, or you'll miss a holder and break it later.
Which agents and computers had the key?
List every agent environment, .env file, CI secret and Computer that holds it. If each agent was given the same key, all of them are in scope. If you can't answer this list in five minutes, that's the first finding of the postmortem.
Which run leaked it?
Search history for the key's first characters, not the full value, so the search itself doesn't spread it:
# Find the commit that introduced it, with author, time and agent trailer export LEAKED_PREFIX="sk-live-4f9a" # first 12 characters only git log --all -S "$LEAKED_PREFIX" \ --format='%h %ad %an%n %(trailers:key=Co-authored-by,valueonly)' \ --date=iso The commit time and author narrow it to a window. Match that window against your agents' run history: the run that was active at that minute, on that repo, is your suspect. Read its log for the tool call that wrote the key.
Minutes 20 to 40: rotate across the team without stopping every agent
Rotation is where teams lose the most time. The key lives in six places, each owner updates their copy at a different speed, and one forgotten agent keeps failing for a day.
Fix it in this order:
- Put the new key in one place. A secrets manager, a CI secret or the agent platform's secret store. Not a message to the team.
- Point every holder at that place. Agents read the key from it. They don't keep a pasted copy.
- Split the key if you can. Per-agent or per-Computer keys mean the next leak is one revoke, not a team-wide outage.
The OWASP Secrets Management Cheat Sheet covers rotation, revocation and expiry as one lifecycle, which is the right frame here: the incident is a rotation you didn't schedule.
Minutes 40 to 60: clean the agent-specific places
Now remove the copies. Agents add places a human leak rarely touches:
| Place | Why the key is there | What to do |
|---|---|---|
| Agent transcripts | The agent ran a command containing the key | Search and delete the session files on each Computer |
| Run output and logs | Tool output echoed the request | Delete or redact the run's log where your tools allow it |
| Task and PR comments | The agent pasted its command as evidence | Edit or delete the comment; check its edit history |
| Agent settings files | A standing approval saved the literal command | Remove the rule, then untrack the file |
| CI logs | A step printed the environment | Delete the job log, then fix the step |
| Git history | The commit itself | Rewrite last, after rotation |
Claude Code, for example, stores session transcripts in plaintext under ~/.claude/projects/ for 30 days by default. Every Computer that ran an agent with the key has its own copy:
rg -l --fixed-strings "$LEAKED_PREFIX" ~/.claude/projects ~/.config 2>/dev/null For git history, GitHub's guide uses git-filter-repo --sensitive-data-removal. It's explicit about the limits: rewritten commits can stay reachable in forks, clones, cached views and pull requests, and GitHub Support only helps remove them when rotating the credential can't mitigate the risk. That's why rotation comes first. GitHub secret scanning with push protection should already be on; if it wasn't, turn it on today.
Run the response as Sharkly tasks
Sharkly doesn't scan for secrets or rotate keys. Its part is coordination: who does each step, what they found, and who approved the rotation. During an incident, that record is what you'll want at the postmortem.
Here's the pattern that works in Sharkly:
- One task per runbook step. Revoke, blast radius, rotate, clean transcripts, clean history. Each one has an assignee, a person or an Agent.
- Agents do the searching. Assign "find every copy on this Computer" to an Agent that runs there. It reports file paths in a comment, never the key itself.
- Run history settles authorship. A task's Executions tab shows its run history separately from the comment timeline, and the execution log records when a run started, ended and which tool calls it made. That's the evidence for "which run leaked it."
- A human approves the rotation. An Agent can leave a task waiting for human review, which surfaces in the reviewer's Inbox. The person who owns the key signs off.
- The key lives on the Agent, not in the task. Per the Agent docs, an Agent can carry plain and secret environment variables, and the local service on the Computer projects the allowed environment and credentials into each run. Rotating means changing one secret variable per Agent instead of editing
.envfiles on every laptop and server.
If the leaked credential is a Sharkly personal access token (they start with shk_), the settings page shows when each token was last used, which answers "was it used after the leak?" Delete it there and create a replacement.
Stop the next one
Prevention is its own checklist, so it isn't repeated here. The short version: secrets travel as variable references, agent settings files get reviewed like code, scanners learn to catch passwords inside commands, and every key has a named rotation owner. The team guardrail checklist has the configs, and our piece on approval gates for coding agents covers where to put a human stop. For what an agent should never be handed at all, see the Meta Muse agent signal leak.
Conclusion
An agent leak is a normal key leak with more holders and more copies. Revoke first, map who holds the key, find the run from commit time and run history, rotate in one place, then clean transcripts, logs and history.
Write the runbook down before you need it. Then put your agents' work on one board where every run and every comment is on record: Sharkly is free to start, and you can connect your first Computer and Agent in a few minutes.
FAQ
Is deleting the commit enough if an AI agent leaked an API key?
No. Anyone who fetched the repo, a fork or a cached view still has the key. Revoke or rotate it at the provider first; cleaning history comes last.
How do I tell which agent run leaked the key?
Use git log -S with the key's first characters to get the commit time and author. Then match that time against your agents' run history for that repo and read the suspect run's tool calls.
Should each agent get its own API key?
Yes, where the provider allows it. Per-agent or per-Computer keys turn the next leak into one revoke and make usage logs tell you which agent made each call.


