Security
How to Keep API Keys Out of Your AI Agents' Hands
Every key an AI agent can read is a key it can leak. Why credentials don't belong in prompts or agent environments — and how the gateway pattern fixes it.
Every key an AI agent can read is a key it can leak. The fix is not a more careful prompt — it is an architecture where the agent never holds the secret at all.
Why agents leak keys
An LLM agent's context window is not a vault. Everything in it can end up in logs, traces, error reports, evaluation datasets — or in the output itself, extracted by a well-crafted prompt injection hiding in a web page or email the agent was asked to read.
Unlike a human, an agent cannot recognise that handing over a credential is odd. If the key is in context and something asks for it convincingly enough, the failure mode exists. Security teams call this the lethal trifecta: private data, untrusted content and outbound communication in one loop.
The three anti-patterns
Keys in the prompt. "Use this API key: sk-…" puts the secret one injection away from exfiltration, and into every log line along the way.
Keys in the agent's environment. Better, but the agent's tools can usually read env vars — and a broad key with full account scope and no expiry turns one bad tool call into a takeover.
Shared human tokens. Re-using your own OAuth grant or admin token means the audit trail says you did whatever the agent did.
The gateway pattern
The robust design inverts the flow: the agent never receives credentials. It sends an intent — "send this email", "call this endpoint" — to a gateway. The gateway checks policy, optionally asks a human, then injects the credential inside a protected execution environment and performs the call.
The secret never enters the model's context window. It cannot be logged in a transcript, cannot be extracted by injection, and cannot be copied into an agent's long-term memory. The agent gets capability without custody.
Scope small, rotate fast
Gateways also make hygiene practical. Store one credential per service, grant it to specific agents for specific actions, and revoke it in one place the moment something feels off. Per-task scoping shrinks the blast radius; central storage makes rotation a five-minute job instead of a scavenger hunt across .env files.
A quick checklist
No raw secrets in prompts, tool outputs or agent memory — ever. Agents authenticate to a gateway, not to your services. Every credential is scoped to the narrowest action set that works. Human approval guards the actions that could hurt: payments, deletion, outbound email. And everything ends in one-click revocation plus a tamper-evident log of every use.
That last pair is exactly what AgentLock is built around: a zero-knowledge vault holds your keys, agents file requests, and even our own backend only touches a credential momentarily during an authorised execution.
More articles
Operations
Human-in-the-Loop for AI Agents, Without the Bottleneck
Full autonomy is reckless, approving everything is unusable. How to risk-tier agent actions and design approvals that take seconds, not meetings.
Read article →
Compliance
AI Agent Audit Trails: What to Log, a Template and the EU AI Act
What an AI agent audit trail must record, a record template to copy, the logging duty in Article 12 of the EU AI Act — and three examples from practice.
Read article →
