Ingenious AI

Guides · Agent security

How to secure AI agent API keys

Every AI agent runs on credentials, and most teams manage those credentials worse than any password on their network: keys live in prompts, in environment files committed to repositories, in a developer's notes, shared across five agents and two environments. A key in a prompt is not stored — it is published to the model, the logs, and anyone who can extract either. This guide lays out the patterns that keep agent credentials out of reach, including the reach of the agent itself.

By the Ingenious AI team · Published 14 November 2026 · Reviewed 14 November 2026

The short answer

Secure AI agent API keys by never letting the agent see them: route tool calls through a gateway that holds the credentials and injects them at request time, issue one scoped key per agent (never a shared human key), restrict each key by target, budget and rate, rotate on a schedule and on suspicion, and scan repositories and logs continuously for leaked keys. A key that has ever appeared in a prompt, a chat transcript or a committed file should be treated as compromised and rotated immediately.

Try it free: AgentBlock — keep agent actions inside approved boundaries, so a leaked or misused key cannot turn into an open door.

What is agent credential management?

the discipline of issuing, storing, scoping, rotating and revoking the credentials an AI agent's tools use — arranged so that the agent itself never possesses the raw secret. It exists because an agent's context (prompts, retrieved content, conversation history) is exactly the material that prompt injection tries to extract, so anything placed in that context must be assumed readable by an attacker who finds the right words.

The rule: the agent never sees the secret

Architect agent tools so credentials live in a gateway or broker: the agent requests an action ('send this email to this customer'), the gateway authenticates the agent, checks the action against policy, attaches the real credential, and makes the call. The secret never enters the model's context, the conversation log, or the tool result. This single pattern defeats the entire class of 'trick the agent into revealing its key' attacks, because there is nothing in the agent to reveal. It also centralises revocation: kill one gateway policy and every action through it stops, instantly.

One agent, one key, scoped tight

Shared keys destroy attribution and multiply blast radius: a leak in the least-secure agent exposes every system the shared key touches. Issue per-agent credentials, scoped to the specific systems and operations that agent needs — read-only where the job is reading, single-recipient or allowlisted where it writes — with rate limits and spend caps attached. When something goes wrong, a per-agent key lets you revoke exactly one agent's access and read exactly one agent's history. Scoped keys are also how you discover over-privilege: the scopes nobody uses are the permissions to remove.

Rotation and the compromised-key drill

Rotate keys on a schedule that matches their exposure (quarterly is a floor for agent keys in active use) and immediately on suspicion: a key in a screenshot, a transcript, a repository, or a departed contractor's machine. Automate rotation where the provider allows it, and practise the drill — the first emergency rotation should not be the first time anyone has done it. Keep an inventory: every key, its owner, its scopes, its last rotation. The keys that leak are disproportionately the ones missing from the inventory.

Scan for leaks continuously

Keys escape through code commits, config files, screenshots in tickets, and logs that captured a request header. Continuous secret scanning across repositories — including history, not just the current tree — plus log review for credential-shaped strings catches most escapes while they are still cheap. A found key is rotated first and investigated second; the order matters because a live key is a live risk during every hour of investigation. Treat scanner false-positive fatigue seriously: tune the patterns to your providers' key formats so real findings are not buried.

What to do when a key has been in a prompt

Assume extraction is possible and act as if it happened: rotate the key, review the usage logs for the credential across the whole exposure window (anomalous targets, volumes, times), and check whether anything the key could reach was changed. Then fix the architecture that put it there — a prompt is not a vault, and instructions like 'never reveal this key' are requests to the very system an attacker is trying to persuade. The durable fix is the gateway pattern: nothing secret in context, ever.

Your 5-action checklist

  1. Move credentials into a gateway or broker so the agent requests actions without ever seeing raw keys.
  2. Issue one scoped key per agent — least privilege, rate-limited, budget-capped — and retire every shared key.
  3. Build the inventory: every key, owner, scopes, last rotation; investigate any key missing from it.
  4. Rotate quarterly and immediately on any suspicion; practise the emergency rotation once before you need it.
  5. Run continuous secret scanning over repositories (including history) and logs; rotate first, investigate second.

Worked example: San Pedro Property Rentals

San Pedro Property Rentals, a fictional agency, discovered during a routine repository scan that its booking agent's payment-provider key had been committed to a private repo eight months earlier — and, worse, the same key appeared in the agent's system prompt 'so it could confirm charges'. The key could issue refunds. Their response followed the guide: the key was rotated within the hour, usage logs showed no anomalous refunds, and the agent was rebuilt behind a gateway — it now requests 'refund booking #N by $X' and the gateway checks policy and executes with a credential the agent has never seen. The refactor took three days. The exposure it closed had been open for eight months and would have survived any amount of prompt engineering.

Sources

Frequently asked questions

Is it safe to put an API key in the system prompt if we tell the model not to reveal it?

No. System prompts leak through injection, through ordinary conversation tricks, and through your own logs and debugging tools. An instruction not to reveal a secret is a request the attacker is actively trying to override; architecture — keeping the secret out of context — is the only reliable control.

What's the difference between a key vault and a gateway?

A vault stores secrets and hands them to whoever is authorised — which, for an agent, still means the secret enters the agent's world. A gateway keeps the secret and performs the action itself. For agents, the gateway is the stronger pattern because the secret is never in the model's reach at all.

How do we handle keys for tools that require the agent to authenticate directly?

Some tools do force direct authentication. In those cases use the narrowest scoped credential the provider offers (ideally a dedicated, rate-limited token for that agent), shorten its lifetime, and monitor its usage for anomalies. Treat it as a known-risk exception in the inventory, not as the pattern.

We found a key in an old commit. The repo is private — do we still rotate?

Yes. Private repositories are shared with contractors, synced to laptops, backed up and occasionally made public by accident. Rotation is cheap; the alternative is betting that every copy of that history stayed controlled forever. Rotate, then clean the history if you can.

How often should agent keys rotate?

Quarterly at most for keys in active agent use, sooner for keys that touch payments or customer data, and immediately on any suspicion of exposure. If rotation is painful, that pain is the signal to automate it — painful rotation is skipped rotation.

Related guides and tools