# AI Gateway

Your agents' model calls, routed through kosa8 and Cloudflare AI Gateway. Store your organization's provider keys once; a sandbox gets base URLs and a token bound to it, never the keys. Every call is logged, costed and tagged with the organization and the sandbox that made it, so you can see which agent spent what.

**Early access.** The AI Gateway is turned on per organization. Until it is on for yours, these routes answer `403 not_enabled`. Ask for it from the web app or at contact@kosa8.com.

## How it works

1. You store a provider key with an `admin` key. It is sealed (encrypted) at rest, never returned by any route, and never enters a sandbox.

2. A sandbox created with `"ai_gateway": true` gets each provider's base URL pointing at `https://api.kosa8.com/v1/ai/gw/{provider}`, and, as its "API key", a `k8ai_…` token that names only your organization and that sandbox.

3. kosa8 checks the token, attaches your provider key, tags the call with the organization and sandbox, and sends it through Cloudflare AI Gateway to the provider. The token stops working when the sandbox is deleted.

## Store a provider key

```
curl -s -X PUT https://api.kosa8.com/v1/ai/keys/openai \
  -H "Authorization: Bearer $KOSA8_ADMIN_KEY" -H 'Content-Type: application/json' \
  -d '{"key":"'"$OPENAI_API_KEY"'"}'
# {"key":{"provider":"openai","hint":"a1b2","updated_at":…}}

curl -s https://api.kosa8.com/v1/ai/keys -H "Authorization: Bearer $KOSA8_ADMIN_KEY"
# {"keys":[{"provider":"openai","hint":"a1b2","created_at":…,"updated_at":…}],"providers":["openai",…]}

curl -s -X DELETE https://api.kosa8.com/v1/ai/keys/openai -H "Authorization: Bearer $KOSA8_ADMIN_KEY"
# {"ok":true,"provider":"openai"}
```

Providers: `openai`, `anthropic`, `groq`, `mistral`, `deepseek`, `openrouter` and `google-ai-studio`. A call to a provider with no key stored answers `409 no_ai_key`; nothing is ever billed to kosa8's own account. A provider's own error (a wrong key, say) comes back with the provider's status and JSON body.

Workers AI (provider `workers-ai`, models named `@cf/…`, OpenAI chat shape) runs on kosa8's Cloudflare account and needs no key, so it is on only for some organizations. When it is, `GET /v1/ai/settings` shows `"workers_ai":true`.

## From the CLI

```
kosa8 cloud ai keys set openai                       # asks for the key, input hidden
printf %s "$ANTHROPIC_API_KEY" | kosa8 cloud ai keys set anthropic
kosa8 cloud ai keys ls                               # last 4 characters only
kosa8 cloud ai keys rm openai
kosa8 cloud ai settings --all-sandboxes=true         # no flags: show the settings
kosa8 cloud ai usage --month 2026-11
kosa8 cloud sandbox create --ai-gateway
```

The CLI reads a key from a hidden prompt, from standard input, or with `--from-env NAME` from a variable of your shell. It never takes the key on the command line, so it stays out of your shell history. Available from CLI v0.21.0.

## Create a sandbox with it

```
curl -s -X POST https://api.kosa8.com/v1/sandboxes \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H 'Content-Type: application/json' \
  -d '{"ai_gateway":true}'
# {"sandbox":{"id":"sbx_…",…,"ai_gateway":{"providers":["anthropic","openai"],
#   "base_url":"https://api.kosa8.com/v1/ai/gw"}}}
```

With no provider key stored (and no Workers AI), this answers `409 no_ai_keys`. `"ai_gateway":false` opts one sandbox out when `all_sandboxes` is on.

Or turn it on for every new sandbox of the organization:

```
curl -s -X PATCH https://api.kosa8.com/v1/ai/settings \
  -H "Authorization: Bearer $KOSA8_ADMIN_KEY" -H 'Content-Type: application/json' \
  -d '{"all_sandboxes":true}'
```

The sandbox gets, for each provider with a key:

| Variable | Value |
|---|---|
| `OPENAI_BASE_URL` | `https://api.kosa8.com/v1/ai/gw/openai` |
| `ANTHROPIC_BASE_URL` | `https://api.kosa8.com/v1/ai/gw/anthropic` |
| `GROQ_BASE_URL`, `MISTRAL_BASE_URL`, `DEEPSEEK_BASE_URL`, `OPENROUTER_BASE_URL`, `GEMINI_BASE_URL` | The same, ending in the provider's name (`google-ai-studio` for Gemini). |
| `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, `KOSA8_AI_TOKEN` | The sandbox's `k8ai_…` token, not your provider key. |
| `KOSA8_AI_GATEWAY_URL` | `https://api.kosa8.com/v1/ai/gw` |

The OpenAI and Anthropic SDKs read these, so code in the sandbox runs unchanged:

```
python -c 'import anthropic; print(anthropic.Anthropic().messages.create(
  model="claude-sonnet-4-5", max_tokens=64, messages=[{"role":"user","content":"hi"}]).content[0].text)'
```

For another provider, send the token as that SDK's API key. Variables you pass in the create's `env` win over these. If the sandbox has an egress allow list, it must include `api.kosa8.com`.

## Settings

```
curl -s https://api.kosa8.com/v1/ai/settings -H "Authorization: Bearer $KOSA8_API_KEY"
# {"settings":{"enabled":true,"all_sandboxes":false,"cache_ttl_s":0,"rate_per_min":600},
#  "available":true,"base_url":"https://api.kosa8.com/v1/ai/gw","gateway_id":"default",
#  "providers":["openai","anthropic",…],"workers_ai":false,"configured":["openai"]}
```

`available` says whether the gateway is on for your organization; `configured` lists the providers with a key. Reading needs `sandboxes:read`; changing (`PATCH` with any of the fields below) needs `admin`.

- **enabled**: turn the gateway off for the organization without deleting keys.

- **all_sandboxes**: every new sandbox gets it, as if created with `ai_gateway: true`.

- **cache_ttl_s**: cache identical requests for up to this many seconds (0, the default, is off; at most 86400).

- **rate_per_min**: model calls a minute for the whole organization (default 600, from 1 to 6000). Past it, a call answers `429 ai_rate_limit`.

## Spend

```
curl -s 'https://api.kosa8.com/v1/ai/usage?month=2026-11' -H "Authorization: Bearer $KOSA8_API_KEY"
# {"month":"2026-11","total":{"calls":…,"cost_usd":…,"tokens_in":…,"tokens_out":…,"cached":…,"errors":…},
#  "by_model":[…],"by_sandbox":[…],"recent":[…],"note":"…"}
```

The same figures are on the web app's **Usage** page. The cost is what AI Gateway priced each call at. The provider charges it to your own key: it is not on your kosa8 bill.

---
Source: https://docs.kosa8.com/guides/cloud-ai-gateway/
