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
-
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 -
Add
--proxyTerminal lk run --proxy --env-file .env.local -- npm testThe command gets a random placeholder in
OPENAI_API_KEY, andOPENAI_BASE_URLpoints at a port on this Mac thatlk runlistens on. The same applies toANTHROPIC_API_KEYandANTHROPIC_BASE_URL. Requests that carry the placeholder go toapi.openai.comorapi.anthropic.comwith 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. -
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,Bforwards only requests whose JSON body names a listed model. A request with no body, a compressed or malformed body, or a call such asGET /v1/modelsis refused.--proxy-requests Nrefuses requests after N have been forwarded, across both providers. When either is set,lk runprintslatchkey: proxy limits enforced: ...on standard error, so you can see it took effect. Both need--proxy. -
Use it from an agent that has no shell
The MCP tool
latchkey_runtakes the same settings as fields:proxy: true,proxy_modelsas an array of model names, andproxy_requestsas a number. Both limits needproxy: 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
- The command and everything it starts can still use the key within your limits. It can send any prompt or file to the provider. Without
--proxy-modelsit isn't restricted to certain paths or methods. - Spend is capped by
--proxy-requestsonly, not by tokens or money. - The placeholder can show up in output or logs. It is useless outside the command's process tree, but it isn't concealed.
- Proxy mode refuses to start if a supported key isn't a whole reference, if its base URL is already set, or if the key would also reach the command through another variable or key file. At least one supported key reference is required.
- Other credentials are delivered normally. A database URL in the same run is not protected by this proxy.
- A program running as you that can attach a debugger to the command or read its memory gets whatever the command has.
See the CLI reference and the security page for the details.