> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AI credentials proxy

> Store your model-provider keys once and let sandboxes call the provider without ever holding the real key.

[//]: # "smoke: not executed, the account calls need a signed-in session or a key-api token and a real provider key"

A sandbox calls the provider through the proxy with a placeholder key, and the real key never enters the sandbox: anyone with a shell in it can copy the placeholder, and the placeholder is worthless outside the proxy.

```text theme={"theme":"css-variables"}
 sandbox                              proxy                               provider
 ANTHROPIC_API_KEY=styne-ai-...
 ANTHROPIC_BASE_URL=https://api.styne.ai/ai-proxy/anthropic
 agent ── request + placeholder ──▶  placeholder valid? sandbox blocked? proxy on?
                                      swap in the real key  ── request + real key ──▶  api.anthropic.com
       ◀──────── reply ─────────────  count tokens  ◀──────────── reply ───────────────
```

The proxy is on for every account. Store a key in your default profile and every new sandbox of the account starts with
placeholders for the providers that profile holds a key for; you change nothing in your agents. You can switch it off for the
account at any time (see [the proxy switch](#the-proxy-switch)).

## Store provider keys in a profile

A profile is a named set of provider keys. An account holds up to 10 profiles and one is the default; a sandbox gets the default
profile of its account. A profile holds up to 8 keys per provider; one is **active**, the others are ready to switch to.

| Provider | Key variable in the sandbox | Base URL variable |
| - | - | - |
| `anthropic` | `ANTHROPIC_API_KEY` | `ANTHROPIC_BASE_URL` |
| `openai` | `OPENAI_API_KEY` | `OPENAI_BASE_URL` |
| `openrouter` | `OPENROUTER_API_KEY` | `OPENROUTER_BASE_URL` |
| `gemini` | `GEMINI_API_KEY` | `GOOGLE_GEMINI_BASE_URL` |
| `deepseek` | `DEEPSEEK_API_KEY` | `DEEPSEEK_BASE_URL` |
| `mistral` | `MISTRAL_API_KEY` | `MISTRAL_BASE_URL` |
| `xai` | `XAI_API_KEY` | `XAI_BASE_URL` |
| `moonshot` | `MOONSHOT_API_KEY` | `MOONSHOT_BASE_URL` |

Inside the sandbox the base URL points at the proxy, `https://api.styne.ai/ai-proxy/<provider>`, plus `/v1` for the providers that
put their API under it: `ANTHROPIC_BASE_URL=https://api.styne.ai/ai-proxy/anthropic`, `OPENAI_BASE_URL=https://api.styne.ai/ai-proxy/openai/v1`.
The key variable holds the placeholder, which starts with `styne-ai-`.

Keys are encrypted (AES-256-GCM, bound to your account) before they are stored. **No call ever returns a key**: a profile lists, per
provider, each key's `id`, its `hint` (the last four characters) and whether it is `active`. A key you pass in the request body is not
logged anywhere.

Needs a role of owner or admin to change anything; every role except billing can read. The calls below use the account API at `https://app.styne.ai`, so `$API` is `https://app.styne.ai`. Send a signed-in session
token as `$TOKEN`, or an API key with `/v1/key-api` in front of the path (`$API/v1/key-api/ai-credentials/profiles/default/keys/anthropic`);
an API key acts as the member who created it and needs the `ai.manage` scope to change anything. The dashboard does the same. A stored
key reaches the sandbox regions within about 20 seconds.

Create a profile (it becomes the default when it is the first), add two keys, switch:

```text theme={"theme":"css-variables"}
curl -sS -X PUT "$API/v1/ai-credentials/profiles/default" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{}'
curl -sS -X POST "$API/v1/ai-credentials/profiles/default/keys/anthropic" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"value":"'"$ANTHROPIC_API_KEY"'"}'
curl -sS -X POST "$API/v1/ai-credentials/profiles/default/keys/anthropic" -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" -d '{"value":"'"$ANTHROPIC_API_KEY_2"'"}'
curl -sS -X PUT "$API/v1/ai-credentials/profiles/default/keys/anthropic/$KEY_ID/active" -H "Authorization: Bearer $TOKEN"
```

| Call | What it does |
| - | - |
| `GET /v1/ai-credentials` | The proxy switch, the default profile, every profile with its keys' ids, hints and active flags |
| `PUT /v1/ai-credentials/profiles/{name}` | Create the profile if missing; `{"make_default":true}` makes it the default; `{"copy_from":"other"}` starts a new profile with a copy of its keys |
| `DELETE /v1/ai-credentials/profiles/{name}` | Delete the profile and its keys |
| `POST /v1/ai-credentials/profiles/{name}/keys/{provider}` | Store a key (`{"value":"..."}`, one token of 8 to 8192 printable characters). The first key of a provider becomes active |
| `PUT /v1/ai-credentials/profiles/{name}/keys/{provider}/{id}/active` | Make this key the active one |
| `DELETE /v1/ai-credentials/profiles/{name}/keys/{provider}/{id}` | Remove a key; the oldest remaining key of that provider takes over when it was active |

## The proxy switch

The switch is per account and on by default. With it on, every sandbox created afterwards gets, for each provider the default
profile holds a key for, a placeholder key and the proxy's base URL in its environment, so the agents inside work without changes.
An account whose default profile holds no key gets nothing added. A variable you pass yourself at create (or set in a stored
environment) wins over the placeholder.

```text theme={"theme":"css-variables"}
curl -sS "$API/v1/ai-proxy" -H "Authorization: Bearer $TOKEN"
curl -sS -X PUT "$API/v1/ai-proxy" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"enabled":false}'
```

Switching it off takes effect within seconds: the proxy refuses the calls of every sandbox of the account (`ai_proxy_off`), also the
sandboxes that were already running. Switch it on again with `{"enabled":true}`: sandboxes that already hold a placeholder work again, and a sandbox created
while it was off holds none.

## How the proxy protects keys

* The placeholder is random per sandbox; only its hash is stored. It is bound to that sandbox and works only while the sandbox
  runs. At every call the proxy asks the sandbox's node (a result is reused for 2 seconds at most):

  * running: the call goes through;
  * paused or stopped: refused with `403 sandbox_not_running`; the placeholder is kept, so the same token works again once the
    sandbox runs again;
  * ended by timeout, crash, node loss or delete, or unknown to every node: refused with `401 invalid_placeholder` and the placeholder
    is ended for good, so a token copied out of a sandbox is useless the moment the sandbox is gone;
  * the nodes cannot tell: refused with `503 proxy_unavailable`, nothing is ended.

  As a backstop a placeholder also expires 2 days after the sandbox was created.
* A key goes only to its own provider: the host behind `/ai-proxy/anthropic/...` is fixed to `api.anthropic.com`, whatever the
  request says. The key is put only in the provider's own header (`x-api-key`, `Authorization` or `x-goog-api-key`). Every other
  credential header the sandbox sends is dropped.
* When a provider answers `401` or `402` to the active key, the proxy tries the profile's next key of that provider with the same
  request, and returns the first answer that is not a refusal. This keeps a sandbox working when one account runs out of credit. The
  active flag you set is the first one tried; it is not changed by the retry.
* Replies stream through unchanged. The proxy reads the provider's own `usage` fields as they pass and keeps no content.
* Request bodies up to 32 MiB are forwarded.

The proxy forwards only providers that do not echo a request's headers back. Sign-in subscriptions
(Claude, ChatGPT), AWS Bedrock and custom provider APIs go through environment variables you pass yourself.

## Read token usage

Per sandbox and provider the proxy counts requests, input and output tokens (from the provider's usage fields; cached input counts as
input) and refused or failed calls. Counters are kept for 40 days after their last change.

```text theme={"theme":"css-variables"}
curl -sS "$API/v1/ai-usage" -H "Authorization: Bearer $TOKEN"
curl -sS "$API/v1/ai-usage?sandbox=$SANDBOX_ID" -H "Authorization: Bearer $TOKEN"
```

```json theme={"theme":"css-variables"}
{ "usage": [ { "sandbox": "sbx_123", "provider": "anthropic", "requests": 14, "input_tokens": 52113, "output_tokens": 9120, "errors": 1 } ] }
```

A stream that the sandbox abandons is counted with the tokens seen so far.

## Block a sandbox

```text theme={"theme":"css-variables"}
curl -sS -X PUT "$API/v1/ai-access/$SANDBOX_ID" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"blocked":true}'
curl -sS "$API/v1/ai-access" -H "Authorization: Bearer $TOKEN"
```

A blocked sandbox gets `403 ai_access_blocked` from the proxy on its next call; unblock with `{"blocked":false}`.

## Errors from the proxy

| Status | `error.type` | Meaning |
| - | - | - |
| 401 | `invalid_placeholder` | No placeholder, an unknown revoked or ended-sandbox one, or a real provider key sent to the proxy |
| 403 | `ai_access_blocked` | The sandbox is blocked |
| 403 | `sandbox_not_running` | The sandbox is paused or stopped; works again when it runs |
| 403 | `ai_proxy_off` | The account's proxy switch is off |
| 404 | `unknown_provider` | Not one of the providers above |
| 409 | `no_credential` | The profile holds no usable key for this provider |
| 413 | `request_too_large` | Body over 32 MiB |
| 502 | `upstream_unreachable` | The provider did not answer |
| 503 | `proxy_unavailable` | The proxy cannot read its state right now; retry |

Provider answers, including their own errors, pass through unchanged.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.