
An exchange API key is not a forum password. If it leaks, the damage is measured in order history and balance, not in a password reset. This guide is about reducing the cost of a leak to near zero before it happens.
An LLM key spends tokens. An exchange key moves money. The permission model defines everything:
| 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 |
So the defense is not “hide the string”. It is shrinking the permission set. Even a stolen key without withdrawal rights and with an IP allowlist is a bounded problem, not a lost deposit.
The lesson: the threat is almost never clever cryptography. It is the permissions you granted and the channels through which the secret is reachable.
Where the exchange supports it, create a subaccount and a separate key for trading only.
subaccount: agent-hot-01
key label: executor-01
scopes: read, trade
withdraw: disabled
ip allow: 203.0.113.10
symbols: BTC/USDC, ETH/USDC
Why it matters: if the key leaks, the attacker cannot withdraw. The worst case is a trading loss bounded by your limits. Painful, but survivable.
The core rule: the key never enters the model context, the logs, or tool responses. A model can ask to “show the config”, and if the key sits in a file the agent can read, it ends up in chat or history.
chmod 600 file readable only by the executor process.sk-...abcd, not the full string..env and config directories.import os, re
KEY = os.environ["EXCHANGE_TRADE_KEY"] # executor only
def redact(text: str) -> str:
# keep the key out of logs and model responses
return re.sub(r"(?i)(api[_-]?key|secret|token)(\s*[:=]\s*)\S+", r"\1\2***", text or "")
Why it matters: even if the model errs and tries to print the config, it will not see the key. The secret lives in the executor process, not in shared context.

Secret isolation is useless if the agent container can reach the whole server and every domain.
# 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 directories
analyst:
build: ./analyst
networks: [data_net]
# no exchange keys here at all
networks:
exchange_net: {}
data_net: {}
Why it matters: a compromised analyst cannot reach the key and cannot reach the exchange directly. It can only produce proposals that the controller checks.
A key with no expiry is a key forever. Make rotation routine, not a reaction to an incident.
# quick check that the old key is dead
curl -s -H "X-API-KEY: $OLD_KEY" https://api.exchange.example/account | head
# expected: 401 / invalid key
Why it matters: rotation limits the window in which a leaked key is useful. Monitoring catches the leak before the balance does.
Act in order, and do not rely on the model.
1. Revoke the key in the exchange panel — immediately, not “after the review”.
2. Stop the executor with an independent kill switch.
3. Check history: orders, cancellations, withdrawal attempts, new IPs.
4. Assess the damage from actual operations, not from the agent’s logs.
5. Rotate the remaining keys if they could have been exposed too.
6. Check the infrastructure: open ports, admin panels, container access.
7. Write the postmortem: how the key became reachable and what now closes it.
If the key had withdrawal rights, treat the funds as lost until proven otherwise and act as during an incident, not a suspicion.
The bot stopped trading after enabling the IP allowlist. The executor goes out through a different egress IP (VPN, NAT, a second server). Fix: find the actual outbound IP and add it, do not disable the allowlist.
The key shows up in application logs. The cause is logging environment variables or full API responses. Fix: apply redact() on every logging path and check that .env does not appear in docker inspect or error reports.
Some services broke after rotation. They all shared one key. Fix: issue a separate key per service from the start and rotate them one by one.
The agent “sees” the key in its context. The key sits in a file the agent’s tool can read, or in the system prompt. Fix: remove the key from reachable paths and from the prompt, and keep it only in the executor process.
Is disabling withdrawals enough? It is the most important step, but not the only one. Without an IP allowlist and secret isolation, the key can still be used for losing trades or balance reconnaissance.
Can I keep the key in an environment variable? Yes, if only the executor can read that environment. But do not pass it to the agent, do not log it, and do not commit it to git.
Do I need a separate key if there is only one agent? Yes. A per-service key simplifies rotation and limits the blast radius: revoking one does not stop everything.
What matters more: rotation or the allowlist? Both. The allowlist narrows the source, rotation shortens the lifetime of a leaked key. Each is weaker alone.

Status: practical guide. Some statements come from vendors; verify in your environment.
Step-by-step protection of exchange API keys from an AI agent: least privilege, secret isolation, egress, rotation, and a leak runbook.
Machine data: Citation Map JSON · JSON-LD · Semantic Embeddings — separate stage.
| Entity | Type | Role |
|---|---|---|
| exchange API keys | тема/инструмент | упоминается в заголовке, TL;DR или разделах гайда |
| AI agent security | тема/инструмент | упоминается в заголовке, TL;DR или разделах гайда |
| key rotation | тема/инструмент | упоминается в заголовке, TL;DR или разделах гайда |
| secret isolation | тема/инструмент | упоминается в заголовке, TL;DR или разделах гайда |
| trading bot security | тема/инструмент | упоминается в заголовке, TL;DR или разделах гайда |
| Telegram | платформа/технология | упоминается в заголовке, TL;DR или разделах гайда |
| Docker | платформа/технология | упоминается в заголовке, TL;DR или разделах гайда |
No independent confirmation is provided; source claims and editorial conclusions are separated below.
| Source | Type | Role |
|---|---|---|
| api.exchange.example | внешний источник | ссылка из тела гайда |
Cite as: AI Release. Как защитить API-ключи биржи от ИИ-агента: изоляция, права и ротация. ai-release.org.