You just spotted an API key in the diff your agent pushed. Maybe GitHub flagged it, maybe you saw it in review. The panic is normal. What matters now is the order of operations, because the most expensive version of this incident is doing step three before step one.
Step zero: assume the key is burned
If the repository is public, assume automated scrapers captured the key within minutes of the push. Credential scanners watch public commit feeds continuously, and agent commits are just as visible as human ones. If the repository is private, the key still reached every clone, your CI cache, and possibly your hosting provider's build logs. Either way: the credential's exposure window started at push time, not when you noticed it.
The order that matters
- Revoke and rotate the credential first. Before cleanup, before history rewriting, before the postmortem. Deleting the file does not revoke anything: the key lives in git history and in every existing clone. Go to the provider dashboard (Stripe, OpenAI, Anthropic, AWS IAM, wherever it belongs), revoke the exposed key, and issue a new one. Rotation is the only step that works even if your cleanup later fails.
- Audit usage while the trail is fresh. Check the provider's logs for the exposure window: unfamiliar IPs, model calls you did not make, charges you did not expect. If the key was a payment provider key, look for new charges immediately. This tells you whether you had an incident or a near miss.
- Remove the secret from the working tree properly. Delete the credential from the files, and rotate anything the agent may have copied into nearby code or test fixtures. Just adding the file to
.gitignoredoes nothing once the file is tracked. - Scrub history last.
git filter-repoor BFG removes the secret from history, but it requires a force-push and coordination with everyone who has a clone, and it does not touch forks. This is why it comes after rotation: rewritten history still contains the key on every machine that has not pulled. - Sweep for the rest. Run a full-history scanner (
gitleaksortrufflehog) across the repository. Agents rarely leak exactly one thing; a session that committed one fallback pattern often committed others.
Why agents cause this specifically
Agents leak credentials through three mechanical behaviors, not through malice: blanket staging (git add . executed while .env or key files sit in the working tree), helpful fallbacks (the agent inserts a working credential as a default argument so a test passes), and .env contents pulled into context and echoed into logs. The full failure-mode breakdown is in our agent secret-leak taxonomy.
Making it structurally impossible
After the incident, the fix is not a stern team message. It is removing the agent's ability to stage secrets at all. TokenSafe Guard runs locally before commits and blocks the patterns that cause these incidents: blanket git add . when .env or private keys are present, hardcoded credential fallbacks in environment lookups, verify=False TLS bypasses, and staging of .pem, .key, or id_rsa files. When it blocks a call, it tells the agent why and how to fix it, so the session recovers without a human in the loop:
TokenSafe Guard: BLOCKED `git add .`
Reason: blanket staging risks committing .env and uncommitted private keys.
Directive: stage specific paths instead.
The five guardrail categories are detailed in Five guardrails before AI-written code lands.
Set it up
npx tokensafe@latest init
Guard rules install into Claude Code, Cursor, Codex, Windsurf, and Antigravity lifecycle hooks in one command. See the quickstart docs or start a 7-day team trial to put the same blocks across every seat.
Rotation first, cleanup second. A key that stays unrevoked in rewritten history is still a live key.