Secure API keys and permissions for AI agents

Give each agent its own API key with a narrow scope and a spending cap, treat tool results as untrusted input, and decide which actions an agent must never take.

  • 30 min
  • 4 parts
  • Intermediate

A script you wrote does exactly what the code says. An agent does what it decides the conversation needs, and it decides with incomplete information, in a loop, at machine speed. It will make three hundred calls because a prompt was ambiguous. It will retry a rejected argument by guessing differently, several times, each attempt billed. It will read a web page that contains instructions and may follow them.

None of that makes agents unusable. It means the controls belong outside the model, in places the model cannot talk its way around: the key it holds, what that key can reach, how much it is allowed to spend, and which actions require a human. This lesson builds those controls in that order.

01One key per agent, never the primary

The first rule is the simplest and the one most often skipped: an agent gets its own sub-key, never your primary key.

A sub-key is a second credential on the same account. It reaches the same catalog and bills to the same credits, so nothing about how the agent calls changes. What changes is what you can do when something goes wrong:

  • It is independently revocable. Delete the sub-key and exactly that agent stops, immediately. Every other integration keeps working. With a shared primary key, revoking it after a leak takes down everything at once, which is exactly why people hesitate and leave a leaked key live.
  • It has its own rate-limit window. A runaway agent exhausts its own per-minute bucket, not the one your production traffic depends on.
  • It can carry its own restrictions, covered below.

"One per agent" means one per deployment of an agent, not one per product. The support assistant in production, the same assistant in staging and a developer's local copy are three consumers with three different risk profiles. Name keys so that six months from now the name tells you whether it is safe to delete: support-bot prod, email checks only answers that; agent2 does not.

Where the key lives matters as much as which key it is. Recall from lesson 2 that OAuth keeps the credential out of config files entirely. If a key must be in a file, it is a secret on disk and gets the same treatment as a database password. And it never goes into a prompt or a system message: anything in the model's context can end up in a transcript, a log, a shared conversation or a provider's retention window, and a model can be talked into repeating it.

Check yourself

Your staging agent's sub-key leaks in a public repository. What is the right first move?

Run every example on your own key. Free plan, no card.

Scaling up?

Volume pricing, custom SLAs, and dedicated support for high-traffic teams.

Contact sales