Why static API keys die in production AI agents
Every AI agent in your stack reads an API key from an env var. One leaked worker image gives an attacker the whole account. Here's what breaks, in order, and why the usual fixes don't survive contact with production.
By Jared Dean
Every AI agent I've looked at inside a Series B production stack reads at least one API key from an environment variable. Usually it's an OpenAI key. Often it's also a vendor API key (Stripe, Resend, GHL, Twilio) that the agent calls on behalf of a user. The key is baked into the worker image at deploy time, lives in memory for the process lifetime, and gets rotated roughly never.
For a prototype, that's fine. For production, it's a time bomb with four failure modes. I've watched all four fire. Here's what breaks, in order of how badly it hurts.
Failure 1 — The worker image is the whole account
You deploy your agent to Kubernetes. The pod image carries the env file. The env file carries OPENAI_API_KEY. If an attacker ever reads the pod spec, the container filesystem, a crash dump, a misconfigured kubectl exec, or a log line that accidentally echoed the env, they have your entire OpenAI account. Not scoped. Not time-boxed. The whole thing, until you manually rotate.
The usual response is "we encrypt the env with sealed-secrets." That changes who can read it at rest. It does not change what happens when the running pod is compromised. The key is still in memory, still full-scope, still rotated by hand.
The honest version: the blast radius of a leaked worker image is your entire vendor account. If you can't live with that, env tokens aren't the right model.
Failure 2 — Sub-agents inherit everything, by default
You wire up an orchestrator agent. It spawns a sub-agent to handle a specific tool call. The sub-agent reads the same env, loads the same key, and now has every permission the orchestrator had.
This is the design, not a bug. Both agents share the same process (or the same pod), the same env, the same auth story. There is no mechanism that lets the orchestrator say "the sub-agent should only be able to do X" — because the credential itself doesn't know what X is. It's a single bearer token with full scope.
Three months in, your agent graph has fanned out. One sub-agent at depth 4 has a prompt-injection bug that lets a user instruct it to call an API it was never supposed to touch. The call succeeds because the credential has full scope. Nobody notices for a week.
The fix people reach for is "add middleware that checks what the sub-agent is allowed to do." Which works until someone adds a new sub-agent and forgets the middleware. The middleware is a convention, not a cryptographic property. Any permission system that depends on humans remembering to apply it will decay to no permission system.
Failure 3 — The 2am log line says the key, not the agent
The outage hits. The on-call pulls logs. The log line that proves the bad call was made shows the API endpoint, the request body, maybe a request ID. It does not show which of your agents made the call, which end-user they were acting on behalf of, or what the scoped permissions were — because there were no scoped permissions and the agent identity was "the pod that held the key."
The postmortem writes "we were unable to attribute the request to a specific agent." Which is engineering code for "we didn't log what we would have needed to log, and we can't retrofit it." The next incident writes the same sentence.
What the log should say: agent=resume_editor_v2 principal=user_123 chain=[chat_handler → tool_router → resume_editor_v2] permissions=[resume.read, resume.write] expires=2026-10-23T14:32:11Z. Enough to answer the three questions every 2am incident needs: who, on whose behalf, with what rights. Env-token agents can't produce this log line because they don't have the information — the token itself doesn't carry it.
Failure 4 — Rotation is a redeploy you'll never schedule
The right response to "we think a credential leaked" is to rotate it in seconds. The usual response in an env-token world is: generate a new token in the vendor UI, update your secret manager, redeploy every service that reads it, verify every webhook, pray nothing was caching the old value, finally revoke the old token.
That's a 2-hour sequence on a calm day. On a bad day, with a half-remembered deployment pipeline and a stressed-out engineer, it's a 2-day sequence. And because it's painful, teams don't do it on schedule — they do it when forced to by a visible leak.
The alternative is credentials that are already short-lived — they expire in 60 or 600 or 3,600 seconds and are reissued automatically. If one leaks, the blast window is however long until the next automatic rotation. If the issuer sees an anomaly, the next refresh request is denied. No redeploy. No calendar event. The vendor doesn't even need to be told.
This isn't theoretical. SPIFFE/SPIRE workloads have been running this way at scale since 2017. AWS IAM roles do effectively the same thing with STS tokens. GCP service accounts the same. The pattern is proven. It just hasn't been adapted to the "AI agent that calls OpenAI on behalf of a user" shape of the problem — because that problem is new, and most agent frameworks ship with env-var examples.
What to do about it
If you're running production agents, three questions are worth answering out loud with your team this week:
- If a worker image leaked right now, which keys would be in it, and what would the attacker get? Write it down. The exercise alone usually surfaces one key that shouldn't be there.
- For every agent that spawns a sub-agent, can the sub-agent do anything the orchestrator couldn't? If yes, document the design choice. If no, prove it — not with middleware, with a cryptographic property.
- Can your log for an agent-triggered external API call answer "which agent, on whose behalf, with what permissions"? If not, the next postmortem is already written.
None of these require buying anything. They just require being honest about what the current model does and doesn't give you.
If you'd like a second pair of eyes on your production agent stack and a written diagnostic (10-page report, 2-hour debrief, $10k flat), [get in touch][contact]. I've been running this audit against my own production code — the self-audit found three fixes worth doing in a sprint and ruled out a bunch of changes I would have made if I hadn't actually looked.
The audit template and my own self-audit are published verbatim at [claimwire.*/content][open]. Steal them, run them yourself, and if you still want help, you know where to find me.
[contact]: https://claimwire.* /#waitlist [open]: https://claimwire.* /content