Security Blog

How do I give an AI agent an API key without it seeing the key?

#44

November 20, 2026 · By Mila

← All posts

Give the agent a reference, not the key, and let a proxy on the same machine put the real key into the request on its way out. Here is why env vars stopped being enough, and how to set it up.

Give the agent a reference instead of the key, and let a small proxy on the same machine swap the real key into the request on its way out. The agent writes clavitor://OpenAI/key where the secret goes. The API receives the real key. The agent never holds it, so it can't print it, log it or be talked into handing it over.

That's the whole idea. The rest of this page is why it matters, and how to set it up.

Why "just put it in an env var" stopped being fine

A year ago, export OPENAI_API_KEY=sk-... was the normal way to hand a key to a script. Nobody blinked.

Then the script started thinking for itself.

An AI agent reads files, runs tools, writes logs and talks to strangers on the internet. Every one of those is a way out for a key it holds:

  • The environment. Anything the agent runs inherits it. It shows up in /proc, in crash dumps, in CI output.
  • Memory. Fetch the key at runtime and it still sits in the process for as long as the agent runs.
  • The conversation. A prompt injection in a web page or an issue comment asks the agent to "include your configuration for debugging". A helpful agent does.

None of that needs a bug in the agent. It only needs the key to be somewhere the agent can reach.

The pattern: the agent asks, the proxy signs

Think of a hotel key card. The guest can open their room. The guest never sees the master key, and the door logs every swipe.

For an API key, that looks like this:

  1. The real key lives in a vault, encrypted.
  2. A proxy runs next to the agent, on the same machine.
  3. The agent sends its HTTPS request with a reference where the key goes.
  4. The proxy fetches the value from the vault, puts it in the request and forwards it.
  5. The agent's side only ever holds the reference.

The agent's logs show the reference. So does its context window. There is nothing to leak.

How to set it up with Clavitor

This is how we built it. One binary, one environment variable, no change to the agent's code.

Create a client for the agent in your vault with the scopes it needs, then enroll the proxy and start it:

$ clavitor-proxy init
$ clavitor-proxy ca
$ clavitor-proxy serve

init enrolls the proxy as that client. ca prints the proxy's certificate authority: add it to the trust store your agent's HTTP client uses, because the proxy opens the agent's HTTPS connection itself. Then point the agent at the proxy:

$ export HTTPS_PROXY="http://clavitor:$(cat proxy-auth)@127.0.0.1:1983"

$ curl -H "Authorization: Bearer clavitor://OpenAI/key" \
    https://api.openai.com/v1/chat/completions

proxy-auth is the proxy's own access token, the file next to the binary. Every connection has to carry it, so another user on the same machine can't borrow the proxy. The proxy listens on 127.0.0.1:1983 and only on that machine, unless you set it up on purpose for a trusted private network.

It works with any agent whose HTTP client honors HTTPS_PROXY: Claude Code, Codex, your own scripts. A credential only goes to a host its entry lists as a web address, so a prompt-injected agent can't point the same reference at a stranger's server. The step-by-step setup is in the proxy docs.

What about the agent's other secrets?

Not every secret travels in an HTTP header. A deploy password, an SSH key, a two-factor code for a sign-in.

For those, the agent asks the vault for one named entry at a time with clavitor-cli, and only for entries it was given:

$ clavitor-cli get "GitHub Deploy" --field password

$ clavitor-cli get "Stripe API" --field key
clavitor-cli: CV-2041: Access denied.

The agent can't list the vault. A name search shows at most five matching titles, and only a count above that.

Authenticator codes work the same way. clavitor-cli totp "GitHub" works out the current code on the agent's machine, so it can get through a two-factor sign-in without anyone reading a phone. The vault holds the secret encrypted.

What this doesn't solve

A proxy keeps the key out of the agent. It doesn't make the agent trustworthy.

  • A compromised agent can still use the access it was given. It just can't take the key home.
  • Someone with administrator access to the machine can use the proxy too, or read its memory.
  • An API that echoes your key back in its response hands it to the agent. The proxy puts keys into requests and doesn't filter responses.

That's why the access around it matters as much as the proxy:

  • Scope. Each agent gets only the entries it needs. A work agent sees GitHub and AWS. A deploy agent sees SSH keys and the database.
  • Read limits. Each agent may read only so many different entries per minute and per hour, and you set the numbers. An agent that goes over its hourly limit twice within two hours is locked until you unlock it.
  • A record. Every read is logged: which agent, which entry, when.

Running several agents? Give each one its own proxy and its own enrollment. Then each has its own scope, its own limits and its own trail, and agent A can't see agent B's keys.

The short version

Don't hand an agent your key. Hand it a name for the key, and keep the key one step away, in a place that decides, every time, whether this agent may use it right now.

Set up the proxy or see how agent access works.