How to give an AI agent API access without handing it your keys
An API key is a bearer credential: whoever holds a copy can run billable requests on your account until you revoke it. If an agent can read the key, so can anything that tricks the agent, plus its logs, its config files, and every service that stores it. The safer pattern is to keep the real key somewhere the agent can't reach and give the agent something weaker: a placeholder, a scoped token, or a session that expires.
Why this matters now: the OpenCode Console breach
In October 2026, OpenCode said an intruder reached a legacy system on the night of October 16 and may have exposed the provider keys (OpenAI, Anthropic, Google) that 1.4% of OpenCode Console users had saved there. Some users reported unauthorized charges on their own accounts, ranging from a few hundred dollars to about $1,700, according to BleepingComputer and The Hacker News. OpenCode says refunds are underway. Deleting a key from a tool doesn't revoke it, and a tool's own budget can't cap use of a copied key. Only the provider can do that.
1. Credential brokers and injection proxies
The agent sends requests through a proxy. The proxy holds the real credential and attaches it on the way out, so the key is never in the agent's environment, files, or context.
- Infisical Agent Vault: you group the APIs an agent may reach into an "access bundle", optionally narrowed to certain HTTP methods or paths. Then you start the agent with a time-bound session token and point it at the proxy through
HTTPS_PROXY. The proxy attaches, replaces, or substitutes the real credential. Agents lose access within 60 seconds of a session ending, and every request is logged. Infisical recommends running the proxy on a different machine from the agent. It doesn't support WebSockets or HTTP/2-only clients such as gRPC. Pricing: the first 5,000 brokered requests a month are free, then $2 per 1,000. The Infisical core is MIT-licensed and can be self-hosted. - Latch (built by Subzero Labs, powered by Rialo): you store the real key as a Secret and mint a
lat_access token for the agent. The agent then callshttps://onlatch.com/proxyinstead of the provider. Latch's docs say the token can't be turned back into the real key and can be revoked, and Latch advertises policies and spend limits. It can also sign on-chain transactions under a policy without the agent holding the wallet key. We couldn't find published pricing. - Keycard: runtime authorization for agents. Each credential request is checked against a policy written in Cedar, and if it's approved Keycard issues a short-lived token scoped to the task (its example lasts one hour). There's an optional sidecar proxy that swaps placeholder secrets for real, user-scoped tokens, so the agent never holds a real credential. It's built for teams running many agents and MCP servers in production. Pricing isn't published.
2. Vaults with per-agent access
- 1Password: put only the secrets an agent needs in a dedicated vault, give it a read-only service account for that vault, and fetch secrets at runtime with the SDKs using
op://references. That way nothing is hard-coded, and the Activity Log shows which service account accessed what. For browser logins, 1Password for Claude fills approved logins directly into the page, so passwords and one-time codes never enter the model's context. - Wirebox Vault: a zero-knowledge store for logins (with TOTP), API keys, and other secrets. An admin grants each agent identity specific secrets, and the server only serves that agent the ciphertext it was granted. One caveat: the agent does decrypt and read those secrets, so this limits which secrets it gets rather than hiding them. The free plan includes 3 identities, and Pro is $30/mo for 25.
3. Managed auth for tool calls
If your agent mainly needs SaaS tools such as GitHub, Gmail, or Slack, tool platforms can hold the OAuth tokens for you. Composio says its execution proxy injects credentials into outbound requests server-side, so the agent only sees tool definitions and results. Arcade likewise keeps per-user tokens in its runtime and returns only tool results, never the tokens, to the model or MCP client.
4. Scoped, short-lived tokens
When a real key has to exist, shrink what it can do. OpenAI lets you create restricted keys with None, Read, or Write access per endpoint. Anthropic API keys belong to a single workspace. Use OAuth scopes and fine-grained tokens wherever a provider offers them, and prefer sessions that expire (Infisical sessions, Keycard tokens) over keys that never do.
5. Spend caps
A spend cap is the backstop for when everything else fails. OpenAI projects can enforce a hard monthly limit, after which requests return a 429 (there may be a small overage while settings propagate). In the Claude Console, each workspace can have its own monthly spend limit and alerts. Give each agent its own project or workspace so one leaked key can only burn that budget.
Checklist
- No raw provider keys in prompts,
.envfiles the agent can read,mcp.json, or the repo. - Route agent API calls through a broker or proxy (Infisical Agent Vault, Latch, Keycard) so the agent only holds a placeholder or session token.
- Run the proxy on a different machine from the agent.
- Give each agent and each tool its own key, so you can trace use and revoke one without breaking the rest.
- Allow only the hosts, methods, and paths the task needs, and use read-only scopes where you can.
- Set short session TTLs, a little longer than the longest run you expect.
- Set a hard spend limit and alerts at each provider, per project or workspace.
- Turn on audit logs, and review usage after any incident.
- If a key leaks, revoke it at the provider (deleting it from a tool isn't enough), replace it everywhere, and check billing from the day of exposure.
Related: how to track and cap what your agent spends and how to debug AI agents. For sandboxes that inject secrets at egress, see E2B vs Daytona vs Modal. See every option in Identity/Auth for AI agents.