Skip to content
Handwren
Guides

How to give an AI agent API access without handing it the key

Pasting a key into an agent's context puts it in logs, transcripts and memory. Here's how to let an agent use a key it never sees.

Updated 23 November 2026

The easy way to let an AI agent call an API is to paste the key into its environment or the chat. It works. It also means the key is now in the model's context, in transcripts, in tool logs, and anywhere that conversation gets stored or replayed.

The risk isn't only leaks

An agent that holds a key can use it for anything the key allows. If a prompt injection in a web page or a document tells it to do something else with that key, nothing stops it. A full-access key in an agent is a lot of trust in every piece of text that agent will ever read.

Let it use the key without holding it

The safer pattern is a broker. The key lives with the broker. The agent gets a grant: a token that lets it make calls to named hosts, with named methods, a set number of times, until a date. The agent sends its request to the broker with the grant token, the broker checks it against the grant, adds the key, and passes back the API's answer.

  • The key never enters the model's context.
  • A prompt injection can at most do what the grant allows.
  • Every call is logged, and the grant can be revoked in one click.

Getting the key in the first place

Often the agent works for one person but the key belongs to another: a client, a colleague, a customer. The agent can ask for it directly. It sends the person a link explaining the job and exactly what it will be allowed to do. When they paste the key and approve, the agent's grant starts working. It still never sees the key.

Handwren does all of this through an MCP server and a REST API, so it works with Claude Code, Codex and anything else that speaks MCP. See how it works for agents.