Guide · Key proxy

Hide your OpenAI or Anthropic key from an AI agent

Your tests call OpenAI or Anthropic, so the agent that runs them needs a working key. It doesn't need to hold the key, and what it holds it can print.

In shortRun the command as lk run --proxy --env-file .env.local -- npm test. It gets a placeholder and a local base URL instead of the key, and Latchkey adds the real key only to requests bound for the provider. Add --proxy-models and --proxy-requests to narrow what it can do.

Why a working key means an exposed key

Normally lk run puts the real value in the command's environment. That is how process.env.OPENAI_API_KEY works. It also means the command, and any agent driving it, can print the value, write it to a file or send it somewhere. Masking hides exact copies in the output, nothing more.

For OpenAI and Anthropic keys there is a better arrangement. The command doesn't need the key. It needs to make API calls that carry it. So Latchkey adds the key to those calls outside the command.

Run the command through the proxy

  1. Keep the key as a whole reference

    The variable must be exactly a latchkey:// reference to the key, in an env file or in the environment's record. If you haven't moved your keys yet, start with Keep API keys out of your .env files.

    .env.local
    OPENAI_API_KEY=latchkey://shop/development/OPENAI_API_KEY
  2. Add --proxy

    Terminal
    lk run --proxy --env-file .env.local -- npm test

    The command gets a random placeholder in OPENAI_API_KEY, and OPENAI_BASE_URL points at a port on this Mac that lk run listens on. The same applies to ANTHROPIC_API_KEY and ANTHROPIC_BASE_URL. Requests that carry the placeholder go to api.openai.com or api.anthropic.com with the real key in its place. Anything else is refused. Programs that use the OpenAI or Anthropic SDKs, or read those base URL variables, work without changes.

  3. Narrow what the command may do

    The proxy lets the command use the key. These two options limit how.

    Terminal
    lk run --proxy --proxy-models gpt-4o-mini --proxy-requests 100 --env-file .env.local -- npm test

    --proxy-models A,B forwards only requests whose JSON body names a listed model. A request with no body, a compressed or malformed body, or a call such as GET /v1/models is refused. --proxy-requests N refuses requests after N have been forwarded, across both providers. When either is set, lk run prints latchkey: proxy limits enforced: ... on standard error, so you can see it took effect. Both need --proxy.

  4. Use it from an agent that has no shell

    The MCP tool latchkey_run takes the same settings as fields: proxy: true, proxy_models as an array of model names, and proxy_requests as a number. Both limits need proxy: true. The MCP guide covers adding the server.

Who can use the proxy

Only the command and the processes it starts. A request from any other program gets a 403, even from one that read the placeholder out of the environment, and the proxy stops when the command exits. A process that detaches from the command and is adopted by launchd is refused too. The placeholder is new for each run.

What this doesn't do

See the CLI reference and the security page for the details.

Related