# Is it safe to give an AI agent your API keys?

> It can be reasonably safe if each agent gets only the keys its task needs, for a limited time, with a record of every read, and never through a chat.

It depends on how you give them. Pasting a key into a chat, or leaving it in a file the agent can read in full, puts the key in the agent's hands with no limit and no record on your side. Giving the agent a limited, expiring way to get only the keys its task needs, with a record of each read, is much safer, though no method removes the risk completely.

## What can go wrong

- A key pasted into a conversation becomes part of that conversation's text.
- A key in a `.env` file can be read in full by any process with file access.
- An agent can print a value, write it to a log, or put it in a file that is later committed.
- An agent can follow an instruction hidden in a page or a file it was asked to read.

## What makes it safer

1. Give each agent its own credential, so you can stop one of them.
2. Limit each agent to one environment and the names its task needs, for a short time.
3. Put values into the program that needs them instead of showing them to the agent.
4. Ask a person to approve reads of the most sensitive values.

## How Guardry helps

In Guardry each agent gets its own [agent credential](https://guardry.ai/answers/agent-credential.md) with a scope, an optional name prefix, permissions and a lifetime. Every released value is written to the audit log before it leaves the vault. This command runs a program with secrets injected into its environment, and nothing is written to disk:

```sh
guardry --profile coding-agent secrets run --secret API_KEY --secret DATABASE_URL -- python agent.py
```

## Limits

An agent that can read a secret can copy it. Guardry controls who gets a value and records every release. It cannot control what a program does with a value afterwards.

## Questions

### Should I paste an API key into a chat with an AI agent?

No. Keep keys out of the conversation, including the agent's own credential. `guardry login --profile coding-agent` reads it at a hidden prompt.

### What should I do if an agent leaked a key?

Revoke the key at its provider and replace it. In Guardry an agent can report the leak with `guardry secrets report-leak` without copying the value, and the dashboard shows "Rotation required" on that entry until a person resolves it.

### Can I stop an agent that already has access?

Yes. A credential can be revoked at any moment, and "Revoke all agents" stops every agent in an organization at once. Revocation applies to future requests. A value the agent already received stays with it.

---

Source: https://guardry.ai/answers/is-it-safe-to-give-ai-agents-api-keys/ · Updated 2026-11-27 · Guardry for language models: https://guardry.ai/llms.txt
