I've looked at more than a few trading-agent setups where the exchange key has every permission ticked simply because that was the default in the exchange UI. This post is the sequence I now run through before letting any LLM-driven agent near a funded account.
Budget 20–40 minutes. You need Docker, systemd (or another supervisor), a secret manager, and an exchange that supports subaccounts. By the end you should have a key that can't be turned into a withdrawal, a secret the model never sees, egress limited to the exchange API, a rotation routine, and a runbook you can actually follow at 2 a.m.
An exchange key is not an LLM key
Both look like a string in an env var, and that's where the similarity stops. A model key buys tokens. An exchange key places orders against real money.
| Permission | LLM key | Exchange key |
|---|---|---|
| Read data | yes | yes |
| Create operations | generate text | real-money orders |
| Withdraw funds | no | often enabled by default |
| Cost of a leak | a token bill | the subaccount balance |
That table is the whole argument. You don't make a leaked exchange key harmless by hiding the string better — you make it harmless by shrinking what the string is allowed to do. A stolen key with no withdrawal rights and a pinned IP is an annoying afternoon. The same key with withdrawals enabled is a story you tell on a podcast.
The leak paths I keep seeing are not cryptographic:
- Compromised bot infrastructure. The Bloom Telegram bot stored users' private keys where the service could reach them; 48 wallets were drained of about $953,000 in 94 seconds. No model exploit — an architecture that made the keys reachable.
- Exposed agent platforms. In February 2026, researchers counted more than 40,000 OpenClaw instances reachable from the open internet. A public interface plus weak auth puts whatever it holds one request away.
- Prompt injection. The agent reads outside text, treats it as an instruction, and calls a tool it should never have called. If that tool's key can withdraw, an injection becomes a theft.
In all three, reachability and granted permissions were the weak point.
Step 1: cut the key's scope down to trading
Where the exchange supports it, create a subaccount and a separate key whose only job is trading.
- Read balance and positions — yes.
- Create and cancel orders — yes.
- Withdraw — no. Switch it off at the key level if the panel allows.
- IP allowlist — only the executor's outbound address.
- Symbol and max-order restrictions if the exchange exposes them.
- One key per agent or service, so rotating one doesn't disable the rest.
subaccount: agent-hot-01 key label: executor-01 scopes: read, trade withdraw: disabled ip allow: 203.0.113.10 symbols: BTC/USDC, ETH/USDC The worst case after a leak becomes a bounded trading loss instead of a drained balance. Painful, but survivable — and visible in the order history.
Step 2: the secret must never enter the model's context
My rule: the key never shows up in the prompt, the logs, or a tool response. It leaks easily because a model can simply ask to print the config, and if the key sits in a file the agent's tool can read, it ends up in chat history.
- Store it in a secret manager, or a
chmod 600file readable only by the executor process. - Pass it through the process environment, never through the prompt.
- Run the analyst container without any access to key paths.
- Redact on every logging path.
- Deny the agent read access to
.envand config directories.
import os, re KEY = os.environ["EXCHANGE_TRADE_KEY"] # executor process only def redact(text: str) -> str: # keep key material out of logs and model responses return re.sub(r"(?i)(api[_-]?key|secret|token)(\s*[:=]\s*)\S+", r"\1\2***", text or "") Now when the model asks to see the config, it gets asterisks. The secret lives in the executor process, not in shared context.
Step 3: split the network, not just the process
Container isolation is worthless if the agent can reach every host and domain on the box.
- Separate containers for the analyst and the executor.
- Executor egress — the exchange API only.
- Analyst egress — only the data sources it actually needs.
- No admin interfaces on the public network, not even temporarily.
- Never mount the Docker socket or a home directory into the agent container.
# docker-compose.yml (excerpt) services: executor: build: ./executor env_file: ./secrets/executor.env # chmod 600, not in git read_only: true networks: [exchange_net] # no docker.sock, no home mounts analyst: build: ./analyst networks: [data_net] # no exchange keys here at all networks: exchange_net: {} data_net: {} A compromised analyst can't reach the key and can't reach the exchange directly. All it can do is produce proposals that the controller validates before anything is sent.
Step 4: rotate on a schedule and alert on the odd stuff
A key with no expiry is a key forever. Make rotation routine instead of a reaction to an incident.
- Keep two live keys — current and next. Create the new one, switch, revoke the old one.
- Rotate every 30–90 days, and on any suspicion at all.
- Alert on failed authentication, unfamiliar IPs, and spikes in order cancellations.
- Keep a log: created when, used where, rotated when.
- Verify that a revoked key really fails.
# confirm the old key is dead curl -s -H "X-API-KEY: $OLD_KEY" https://api.exchange.example/account | head # expected: 401 / invalid key Rotation shortens the window in which a leaked key is useful. Monitoring tells you the window was opened.
Step 5: the runbook for the day it leaks
Work through this in order, and don't ask the model what to do.
- Revoke the key in the exchange panel — immediately, not after the review.
- Stop the executor with an independent kill switch.
- Pull the history: orders, cancellations, withdrawal attempts, new IPs.
- Judge the damage by actual operations, not by the agent's logs.
- Rotate every other key that could have been exposed.
- Audit the infrastructure: open ports, admin panels, container access.
- Write the postmortem — how the key became reachable and what now closes that path.
If the key had withdrawal enabled, treat the funds as lost until proven otherwise and respond as if it is an incident, not a suspicion.
My checklist, kept beside this:
- ☐ Dedicated subaccount for the agent
- ☐ Key without withdrawal rights
- ☐ IP allowlist scoped to the executor
- ☐ One key per service
- ☐ Key absent from prompts, logs, and tool responses
- ☐ Analyst cannot read key files
- ☐ Egress limited to the exchange API
- ☐ No admin panels and no Docker socket in the agent container
- ☐ Scheduled rotation with two active keys
- ☐ Alerts on failed auth and new IPs
- ☐ An independent kill switch
- ☐ A leak runbook you have actually tested
What I'd take away
The highest-leverage thing you can do takes thirty seconds: turn off withdrawal on that key. Everything else — isolation, egress rules, rotation, alerting — reduces blast radius and shortens the time window. None of it needs clever cryptography. It needs a decision about what your agent is allowed to do, followed by making sure the key physically cannot do more than that.
This article was originally published on AI Release — https://ai-release.org. If you prefer short-form versions of these guides, the channel is at https://t.me/ai_release1.
