kosa8 / Guides / Keep API keys out of AI agents

Guide · Secrets · Last checked: November 2026

Keep API keys out of AI agents: credential brokering

Do not put the key where the agent runs. Give the sandbox a placeholder and an API address that points at a broker outside it. The broker checks each request, replaces the placeholder with the real key, and forwards the request only to the host that key belongs to. A prompt injection that prints the environment prints nothing useful, and a copied placeholder is worthless anywhere else. While the sandbox runs it can still use the key along that one route, so scope the key at the provider too.

01Why a key inside the agent is already leaked

Anything in the agent's environment can be read by the agent, by every package it installs and by every script it runs. A poisoned README can tell the agent to print the key; a compromised dependency can read it at install time; a crash dump or a verbose log can carry it into a ticket. Once copied out, a key works from anywhere until someone notices and revokes it.

Filtering output for things that look like keys helps a little and fails often: keys can be base64-encoded, split across lines or sent one character at a time in DNS names. The reliable answer is structural. If the real value never enters the sandbox, there is nothing to steal from it.

02How credential brokering works

  1. The secret is stored outside. In the host service, a secrets manager or a gateway, never in the sandbox image, its disk or its snapshots.

  2. The sandbox gets a placeholder and a URL. For example OPENAI_API_KEY holds a random stand-in and OPENAI_BASE_URL points at the broker. Most SDKs read both, so the agent's code runs unchanged.

  3. The broker checks the request. Right placeholder for this sandbox, an allowed method and path, the expected upstream.

  4. It adds the real key and forwards. Over verified TLS to the one upstream the key belongs to. It should not follow redirects, which could carry the header elsewhere.

  5. The direct route is closed. The API's host is not on the sandbox's allow-list, so the agent cannot skip the broker with a key it found some other way.

  6. Every call is logged. Host, method, path and status, never the key itself.

03Ways to give an agent credentials, compared

ApproachReal secret inside the sandbox?Useful if copied out?Tied to one host?Notes
Environment variable or .env fileYesYes, until revokedNoThe default, and the pattern to retire
Mounted secret fileYesYesNoSame exposure, different path
Fetched at run time from a secrets managerYes, once fetchedYes, for its lifetimeNoShorter-lived secrets help; the agent can still read it
Short-lived, narrowly scoped tokensYesYes, until expiryUsually noGood practice for every approach; shrinks the window
Model or API gateway with its own virtual keysA virtual key, yesYes, from anywhere that reaches the gatewayTo the gatewayAdds budgets and logs; the virtual key is still a bearer token
Injecting egress proxy (credential broker)No: a placeholderNoYes, by designNeeds a client that accepts a base URL or proxy; bodies not inspected
Request-signing proxy (for signed cloud APIs)NoNoYesThe proxy signs each request; more work per API

The approaches combine. A broker keeps the value out; short-lived, scoped keys limit what the route can do; a gateway adds budgets. Use the broker for the boundary and the others for depth.

04What brokering does not cover

  • Use along the allowed route. While the sandbox runs, any request the route allows is made with the real key. Restrict methods and paths where you can, and scope the key at the provider: read-only where possible, one project where possible.
  • Request bodies. A broker that forwards bodies unchanged does not judge what the agent asks the API to do.
  • Clients that cannot change their base URL. They cannot be brokered this way. Give them the narrowest token that works and say so in your records.
  • Keys that must live on disk. SSH keys and signing keys are better replaced by an agent or signing service outside the sandbox.
  • Identity. One key per agent makes each action attributable and each key revocable on its own. Shared keys make both impossible.

05Which approach fits

  • Model provider keys for coding agents. Broker them. Every major SDK accepts a base URL.
  • Internal APIs. Broker them, or issue short-lived tokens bound to the task; never a long-lived service account key.
  • Cloud provider credentials. Prefer a signing proxy or short-lived federated credentials over static keys, and block the metadata address.
  • Source control. A fine-grained token for one repository, or a proxy that adds the token for that host only.
  • Tools that will not take a base URL. The narrowest token, a short expiry and a separate sandbox.

The hands-on steps are in How do I give an AI agent API keys without exposing them? and How do I stop AI coding agents from reading my .env secrets?

06One way to do it: kosa8

kosa8's egress broker is a small HTTP proxy inside the kosa8d service on the host, one per sandbox, and it is the only way out a brokered sandbox has. The sandbox holds a placeholder (kosa8-placeholder- followed by 32 random hexadecimal characters, fresh per sandbox), so one found in a log is obviously not a real key. The broker checks the method, path and placeholder, writes the real key into the header and forwards over verified TLS to the one upstream; redirects are not followed. Every request, allowed or refused, goes to the audit log with host, method, path and status, never the key. The real key is not in the sandbox, its record on disk, a snapshot or the audit log.

terminal
export ANTHROPIC_API_KEY=...
kosa8 agent run --dry-run claude
kosa8 agent run -w ~/code/api claude

The dry run prints a line such as brokering ANTHROPIC_API_KEY -> api.anthropic.com before anything starts. A kit file can broker your own tool's key, with the upstream, header, methods and path prefixes it may be used on. A brokered sandbox that is restarted, restored or forked comes back with its network closed and no broker, because carrying the key across would mean storing it. One kit, for a CLI that cannot change its base URL, still passes its token in and says so on every run.

kosa8 is not a secrets vault or an identity provider; those stay where they are. kosa8 limits what a hijacked agent can reach; it does not detect prompt injection. SOC 2 Type II is in progress; there is no SOC 2 report and no external penetration test yet.

To try a kosa8 sandbox without installing anything or making an account, kosa8.com/try runs code in a fresh sandbox with no outbound network: 15 minutes of run time free per 24 hours, each run up to 60 s.

Try it free See pricing

Try free with no account. Starter is free with no card; plans from $29 a month.

07Questions

What is credential brokering for AI agents?

A broker outside the agent's sandbox holds the real credential. The sandbox holds a placeholder, and the broker swaps in the real value on each request to that credential's own host, so the agent never sees the key.

Can an AI agent steal a brokered API key?

Not the value. It can print the placeholder, which does not work anywhere else. While the sandbox runs, it can still make requests through the broker along the allowed route, so scope the key at the provider as well.

Is a secrets manager enough to keep keys from an agent?

Not if the secret ends up in the agent's environment or files. A secrets manager shortens how long a secret lives and controls who can fetch it, but once the agent's process holds the value it can copy it out.

Does brokering work with any API?

With any HTTP API whose client lets you set a base URL or a proxy. Tools that hard-code their API address cannot be brokered this way and need the narrowest token that works.

Should every agent have its own key?

Yes. One key per agent makes every action attributable to that agent and lets you revoke one without stopping the others.

08Sources and further reading

Last checked: November 2026 · Updated 2026-11-29