File size: 30,258 Bytes
928fda2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9703543
928fda2
 
9703543
 
 
 
 
928fda2
 
 
 
 
 
 
 
 
 
9703543
 
 
 
 
 
 
 
 
 
 
928fda2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575d5bd
ecb5fa6
9703543
928fda2
575d5bd
ecb5fa6
9703543
575d5bd
 
 
928fda2
 
 
9703543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928fda2
 
 
 
 
 
99d4eee
575d5bd
928fda2
 
 
 
 
 
 
 
 
 
9703543
 
928fda2
 
 
 
 
 
 
abc6175
928fda2
 
 
575d5bd
928fda2
 
 
 
 
 
 
 
575d5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928fda2
 
 
 
 
 
 
 
 
99d4eee
928fda2
 
 
 
575d5bd
 
 
 
 
 
 
 
 
 
 
 
 
928fda2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9703543
928fda2
 
 
9703543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575d5bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9703543
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# wiwi β€” API Reference

All HTTP endpoints exposed by the wiwi gateway. Base URL is the wiwi process (default `http://localhost:4000`).

Auth summary:
- **Client traffic** (`/v1/*`): virtual key `sk-wiwi-…` via `Authorization: Bearer` (or `x-api-key` on `/v1/messages`).
- **Admin API** (`/admin/*`): master key via `Authorization: Bearer`.
- **User accounts** (`/auth/*`): signed HttpOnly session cookie.
- **Public** (`/public/*`): no auth.

---

## 1. Client surfaces (`/v1/*`)

### `POST /v1/chat/completions` β€” OpenAI Chat dialect

Accepts the OpenAI Chat Completions request shape; returns OpenAI Chat responses (or SSE stream when `"stream": true`). Routing, failover, budgets, rate limits, and logging all apply.

- Auth: `Authorization: Bearer sk-wiwi-…`
- Body: OpenAI Chat JSON (`model`, `messages`, `stream`, `tools`, `tool_choice`, `max_tokens`/`max_completion_tokens`, `temperature`, `top_p`, `stop`, `reasoning_effort`, `response_format`, …)
- Errors: OpenAI-shaped error body.

```bash
curl http://localhost:4000/v1/chat/completions \
  -H "Authorization: Bearer sk-wiwi-…" -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}],"stream":true}'
```

### `POST /v1/responses` β€” OpenAI Responses dialect

OpenAI Responses API shape, used by Codex CLI and the OpenAI Agents SDK. Hosted tools (`web_search`, `code_interpreter`, …) translate through the IR builtin-tool registry to the backing provider.

- Auth: `Authorization: Bearer sk-wiwi-…`
- Body: Responses JSON (`model`, `input`, `stream`, `instructions`, `tools`, `store`, `previous_response_id`, …)
- Events (streaming): Responses SSE event names (`response.created`, `response.output_text.delta`, `response.completed`, …)

**State (default on).** A completed response is stored and may be continued by id: send `previous_response_id` with the next `input` instead of resending the transcript. The id in the response (`resp_<request-id>`) is wiwi's, not the upstream's, and reads/deletes are **scoped to the presenting key** β€” another key's id is a `404`, exactly as if it never existed. `store: false` opts a single request out of persistence, and `previous_response_id: <gone id>` is a `404 not_found_error`. Turn it off globally with `wiwi_settings.store_responses: false`.

- `GET /v1/responses/{id}` β€” replay the stored response object (404 when absent, expired, or another key's).
- `DELETE /v1/responses/{id}` β€” `{"id": …, "object": "response.deleted", "deleted": true}`; 404 when nothing was removed.

### `POST /v1/messages` β€” Anthropic Messages dialect

Anthropic Messages API shape, used by Claude Code and the Anthropic SDK. Also accepts `x-api-key` instead of `Authorization`.

- Auth: `Authorization: Bearer sk-wiwi-…` **or** `x-api-key: sk-wiwi-…` + `anthropic-version` header
- Body: Anthropic JSON (`model`, `messages`, `system`, `max_tokens`, `stream`, `tools`, `thinking`, …)
- Streaming events: `message_start`, `content_block_start/delta/stop`, `message_delta`, `message_stop` (+ `ping`, `error`).

A client that calls `/v1/messages` gets Anthropic-format responses even if the backing deployment is OpenAI β€” cross-dialect translation happens through the IR.

### `POST /v1/completions` β€” legacy OpenAI Completions dialect

The original OpenAI `text_completion` shape: a bare `prompt`, no roles, no tool protocol. Used by older SDKs and by clients that predate `chat/completions`.

- Auth: `Authorization: Bearer sk-wiwi-…`
- Body: `model`, `prompt` (string), `suffix`, `max_tokens`/`max_completion_tokens`, `temperature`, `top_p`, `stop`, `seed`, `n`, `stream`, `stream_options.include_usage`
- Returns: `{"id":"cmpl-<request-id>","object":"text_completion","model":…,"choices":[{"index":0,"text":…,"logprobs":null,"finish_reason":…}],"usage":{…}}`; streaming emits `text_completion` chunks then `data: [DONE]`
- Errors: OpenAI-shaped error body.

Refused with `400 invalid_request_error` (no IR representation β€” refusing beats silently ignoring a parameter the caller sent): `logprobs`, `best_of > 1`, `echo`, `n > 1`, and `prompt` as a token-id array or a multi-element string array. `prompt` is mapped onto one user message; a prompt string plus `suffix` is concatenated.

### `POST /v1/messages/count_tokens`

Anthropic token counting. Estimates tokens for the given Messages request (chars/4 heuristic when the provider doesn't expose a counting endpoint).

- Auth: same as `/v1/messages`
- Body: Anthropic Messages-shaped request
- Returns: `{"input_tokens": <int>}`

### `GET /v1/models`

OpenAI-style model list of the model groups visible to the authenticated key.

- Auth: `Authorization: Bearer sk-wiwi-…` (or master key)
- Returns: `{"object":"list","data":[{"id":"gpt-4o","object":"model",…}, …]}`

### Streaming headers & reconnect (all surfaces)

| Header | Meaning |
|---|---|
| `x-wiwi-request-id` | Request ID, echoed as a response header on every reply |
| `x-wiwi-cache` | `HIT` **only** when the reply was served from the gateway's exact-match response cache (see `docs/CONFIG.md` Β§ `cache_settings`). Absent on a miss. A hit still carries its own `x-wiwi-request-id`, and the body's `id` / `created` describe the request being answered, not the one that populated the entry |
| `x-wiwi-trace-id` | Trace ID (32 hex), only when `telemetry.enabled`. Pair it with the collector to pull the exact trace; see `docs/CONFIG.md` Β§ `telemetry` |
| `x-wiwi-stream-id` | Stream journal ID β€” pass back on reconnect to replay |
| `x-wiwi-translation-warnings` | Present **only** when the request asked for something the chosen provider could not honour β€” a provider-hosted tool the backend cannot run, a sampling parameter it has no field for, a dropped attachment, an unsigned thinking block. The messages are `;`-separated. The same list is always in the request log (`translation_warnings`). Absent when nothing was dropped. |
| `x-wiwi-tool-args-warnings` | Present **only** when the model returned tool arguments that do not match the declared JSON Schema (wrong type, missing required property, unparseable JSON). The call is still forwarded as the model produced it β€” this is an advisory, not a rejection, because some clients repair the arguments themselves. `;`-separated, same encoding as the translation warnings. Absent when every call validated. Unlike the translation warnings this list is **not** in the request log; the operator sees it as the `tool_args_*` proxy-log lines. |
| `x-wiwi-session-id` | Client session identity, used only when `router_settings.session_affinity` is on: pins the session to the deployment that served it so upstream prompt caches stay warm. A pin is dropped the moment that deployment is unhealthy or above this request's lane ceiling. Also accepted as a `session_id` query parameter for clients that cannot set headers; truncated to 128 chars. |
| `Last-Event-ID` | SSE standard header; reconnect replays missed frames. The value is the `id:` field of the last frame the client received β€” a monotonic per-frame sequence number, not the request id |

Every SSE frame carries an `id:` line holding a monotonic per-frame sequence number (`stream_event_ids`, on by default). That is the value a reconnecting client sends back as `Last-Event-ID`.

Stream journals are ON by default (`.wiwi/journals/`, 600 s TTL, 1 MiB cap): a client reconnecting with `x-wiwi-stream-id` + `Last-Event-ID` replays missed frames even after a wiwi restart. Mid-stream provider death resumes from the tape on a fallback deployment (continuation messages are synthesized from partial output).

### `WS /v1/realtime`

Relays a Realtime WebSocket session to an OpenAI-shaped upstream. **Off by
default** (`realtime.enabled`); with it off the route does not exist.

```
ws://host/v1/realtime?model=<group>[&session_id=…]
Authorization: Bearer sk-wiwi-…
```

Every admission check runs **before** the upgrade, so a refusal is a readable
HTTP status rather than an opaque close: `401` bad key, `402` budget exhausted,
`403` model not allowed, `404` unknown model or surface disabled, `429` rate
limited, `501` the provider has no realtime surface, `503` every deployment is
cooling or at its concurrency cap. Once the client sees `101`, a session exists
upstream and errors arrive as close frames.

The refusal is a literal HTTP response written before the handshake completes,
not a close frame: uvicorn answers **403** to *any* close issued before accept
and discards the code, which would make a bad key indistinguishable from an
unknown model. Writing `websocket.http.response.start` by hand is what preserves
the status.

Frames pass through byte-for-byte in both directions β€” the session protocol is
stateful and ordered, so wiwi relays it and never rewrites it. A session holds
its deployment's concurrency slot for its whole life, so `max_inflight`,
priority lanes and session affinity apply to sessions exactly as they do to HTTP
requests.

**Auth:** header only by default. `?key=` is accepted only when
`realtime.allow_key_in_query` is on β€” a query string lands in proxy logs,
browser history and access logs.

**Billing:** a session is charged once, at close, on the **peak** usage its
events reported. Not the sum: the protocol restates overlapping windows of the
same conversation, so summing inflates every multi-turn session. One
request-log row is written per session.

---

## 2. Health & metrics

| Endpoint | Description |
|---|---|
| `GET /health` | Liveness. Always HTTP 200 when the process is up (the Docker healthcheck probes it). `status` is derived, not constant: `ok` when at least one provider is configured and at least one group has an available deployment, else `degraded`. Also reports `groups`, `available_groups`, `providers`, and the log-loss counters: `dropped_request_logs` (queue full), `failed_request_log_writes` (DB write failed), `dropped_proxy_logs`, `failed_audit_log_writes`, `dropped_log_events` (their sum), plus `spend_charge_failures`. A non-zero loss counter means durable accounting is missing rows. |
| `GET /metrics` (default; path configurable) | Prometheus text exposition: `wiwi_requests_total`, `wiwi_request_duration_ms`, `wiwi_tokens_total{type=in\|out\|cached\|reasoning}`, `wiwi_cost_total`, `wiwi_ttft_ms`, `wiwi_tps`, `wiwi_stream_errors_total`, `wiwi_prompt_cache_hits_total`, `wiwi_response_cache_hits_total`, `wiwi_request_logs_dropped_total`, `wiwi_spend_charge_failures_total`. Process-lifetime counters (requests/tokens/cost/cache hits) are monotonic across scrapes; the windowed gauges and the three quantile families come from the in-memory LogEvent ring. `wiwi_request_duration_ms`/`wiwi_ttft_ms`/`wiwi_tps` are `summary` (percentiles computed at scrape time β€” no `_bucket`/`_sum`/`_count` series, so `histogram_quantile()` does not apply). `wiwi_tps` is output **generation** speed (completion tokens over the first-to-last-content-bearing-delta span, excluding queueing and prefill β€” text, thinking, a tool-call open, or a tool-call argument fragment, so tool-call-only streams are measured too and the window covers every token the numerator counts), so only streaming requests with a window over 0.05s contribute a sample; `wiwi_tps_sample_ratio` is a gauge for the share of the current window that `wiwi_tps` describes. |

---

## 3. Admin API (`/admin/*`, master-key auth)

### Virtual keys

| Endpoint | Method | Description |
|---|---|---|
| `/admin/keys` | GET | List virtual keys (hashed at rest; never returns plaintext). |
| `/admin/keys/generate` | POST | Mint a key. Body: name, models, budget, rpm/tpm, expiry, `priority` (lane name). **Plaintext `sk-wiwi-…` returned only here.** |
| `/admin/keys/{key_id}` | PATCH | Update name/models/budget/limits/lane. `priority` is operator-only: a key's owner cannot change their own lane. |
| `/admin/keys/{key_id}` | DELETE | Revoke a key. |
| `/admin/keys/{key_id}/disable` | POST | Disable without deleting. |

### Providers & key pools

| Endpoint | Method | Description |
|---|---|---|
| `/admin/provider-catalog` | GET | Catalog of the 13 provider types (from `PROVIDER_TYPES`) with required fields. |
| `/admin/providers` | GET | List configured providers + key pools + health states. |
| `/admin/providers` | POST | Add a provider account. |
| `/admin/providers/{name}` | PATCH | Edit provider (name/type/base_url/timeout). |
| `/admin/providers/{name}` | DELETE | Delete provider + its keys, cascading away its deployments. |
| `/admin/providers/{name}/keys` | POST | Add a key to the provider's pool (label, secret, weight). |
| `/admin/providers/{name}/keys/{label}` | PATCH | Enable/disable, adjust weight. |
| `/admin/providers/{name}/keys/{label}` | DELETE | Remove one key from the pool. |
| `/admin/providers/{name}/keys/{label}/secret` | GET | Reveal key secret (admin action, audit-logged). |
| `/admin/providers/{name}/models` | GET | Models the provider exposes (live fetch where supported). |
| `/admin/providers/export` | GET | Export provider config (secrets masked or included per flag). |
| `/admin/providers/import` | POST | Import provider config. |

### Backup and restore

Whole-gateway JSON, covering every table the console can mutate β€” not just
providers. The logic is `wiwi/server/backup.py`; the console surfaces it at
**Settings β†’ Import / Export**.

| Endpoint | Method | Description |
|---|---|---|
| `/admin/backup` | GET | Export the whole gateway as one merge-restorable document. |
| `/admin/backup/import` | POST | Merge a document. Merge only β€” never deletes. |

`GET /admin/backup` query parameters (all optional):

| Param | Default | Description |
|---|---|---|
| `include_secrets` | off | Include plaintext provider key secrets and Cline refresh tokens. The only live credentials in the file. |
| `include_bodies` | off | Include `request_body`/`response_body`. A **separate** opt-in, so pulling credentials can never also pull user prompts. |
| `sections` | all non-bulk | Comma-separated subset of `providers,keys,deployments,aliases,settings,prices,users,vkeys,logs,rollups,audit,responses`. Bulk sections are excluded by default β€” the request log grows without limit. |
| `log_days` | `30` | Age bound on log sections. `0` = all time. |
| `log_limit` | `0` | Cap on raw log rows. `0` = no cap. A cap that bites is reported in `truncated`. |

`POST /admin/backup/import` takes the document verbatim. `?dry_run=1` runs the
full validation and returns the per-section plan **without writing** β€” the
console's Import panel shows that before enabling its Merge button. Any
validation error rejects the whole document with nothing applied.

Validation is not atomicity. The three bulk sections (`logs`, `rollups`,
`responses`) are applied first in one transaction, so a malformed row rolls
that whole pass back. The configuration sections then apply in order, each
persisting in its own transaction β€” so a failure partway leaves the earlier
config sections committed. The per-section counts in the response are the
report of what actually landed.

Merge keys, one per section: providers `name` Β· keys `(provider_name, label)` Β·
deployments `(group_name, provider_name, model_id)` Β· aliases the alias name Β·
settings the setting key Β· prices `model_id` (+ `(model_id, scope)` per scoped
override) Β· users `username` Β· virtual keys `key_hash` Β· request logs
`request_id` Β· rollups `(bucket_ts, key_id, model_group, provider, serving_model)`.

Four things a backup deliberately does not do:

- **It does not reveal virtual keys or passwords.** Only their hashes are
  stored (`hash_key` is unsalted SHA-256), so the file carries digests.
  Importing a digest *does* restore a working key β€” the same plaintext
  authenticates on the target β€” but no key can be printed again afterwards.
- **It never replays the audit log.** `audit_logs` has no natural key, and
  restoring another instance's mutation trail would forge this one's operator
  history. The rows are exported and reported as `audit_skipped`.
- **It does not carry the master key.** That lives in `wiwi.yaml` and the
  environment; a restored instance needs `WIWI_MASTER_KEY` set separately or
  it fails closed at startup.
- **It does not delete.** A deployment or alias the *source* recorded as
  removed is removed here too, but nothing is ever dropped merely for being
  absent from the file.

### OAuth providers (Cline, WorkBuddy)

| Endpoint | Method | Description |
|---|---|---|
| `/admin/cline/models` | GET | Models available to the connected Cline account. |
| `/admin/cline/settings` | GET / PUT | Cline account settings. |
| `/admin/cline/settings/default-models/{model_id}` | DELETE | Remove a default-model mapping. |
| `/admin/cline/oauth/login-url` | POST | Get OAuth login URL. |
| `/admin/cline/oauth/connect` | POST | Complete OAuth connect. |
| `/admin/cline/oauth/auto-connect` | POST | Initiate the redirect-based connect. **Dormant:** Cline's Google OAuth ignores `callback_url`, so the redirect never returns a `?code=`; both Cline UIs use the paste-code flow (`login-url` + `connect`) instead. The route works and is tested; it becomes usable if Cline honours `callback_url`. |
| `/admin/cline/oauth/status` | GET | Connection/refresh state. |
| `/admin/cline/oauth/refresh` | POST | Force token refresh. |
| `/admin/cline/oauth/disconnect` | DELETE | Disconnect account. |
| `/cline/oauth/callback` | GET | OAuth redirect target (browser). |

### OAuth providers (Hermes / Nous Portal β€” device-code flow)

Hermes uses OAuth 2.0 device authorization (RFC 8628). Unlike Cline's paste-code flow there is no callback URL: wiwi mints a device code and verification URL, the operator approves in a browser, and wiwi polls the token endpoint. On approval the account is **appended as its own pool key** holding a `hermes:<json>` credential blob (the adapter resolves the `Authorization: Bearer` from it), so multiple Nous accounts share one provider's key pool and round-robin β€” each account has its own rotating refresh token and its own rate-limit bucket. **No API key is required** β€” the device-code call bootstraps the account (creating it with a placeholder key if it does not exist). Tokens are rotated automatically by a background sweeper (~2 min before expiry) and on a mid-session `401`; the refresh endpoints below force one.

| Endpoint | Method | Description |
|---|---|---|
| `/admin/hermes/oauth/device-code?provider=<name>` | POST | Start device authorization. Returns `{state, user_code, verification_uri, verification_uri_complete, expires_in, interval}`. Creates the account if it does not exist. |
| `/admin/hermes/oauth/poll` | POST | One poll attempt for a pending session (`{"state": …}`). Returns `{"pending": true}` while unapproved, or the connected account (`{"connected": true, "key_label", "access_token_masked", "expires_at"}`) on approval β€” the account is added as a new pool key. `410` when the session's 10-minute TTL lapses; `404` for an unknown state. |
| `/admin/hermes/oauth/status?provider=<name>` | GET | `{connected, accounts[]}` β€” one entry per connected account with `{key_label, managed, label, expires_at, needs_refresh, has_refresh_token}`. |
| `/admin/hermes/oauth/refresh` | POST | Force a token refresh (`{"provider": …, "key_label"?: …}`). `key_label` targets one account (default: the first managed one). `401` on a terminal grant (`invalid_grant`/reused refresh token) β€” that account needs re-login; others keep serving. |
| `/admin/hermes/oauth/disconnect` | DELETE | Remove accounts (`{"provider": …, "key_label"?: …}`). With `key_label` removes just that account; without it, removes every managed account. Returns `{disconnected: <count>}`. |

| `/admin/workbuddy/accounts` | GET | List connected WorkBuddy accounts. |
| `/admin/workbuddy/import` | POST | Import WorkBuddy account credentials. |
| `/admin/workbuddy/export` | GET | Export account data. |
| `/admin/workbuddy/refresh` | POST | Force token refresh. |

### Model groups, deployments, aliases

| Endpoint | Method | Description |
|---|---|---|
| `/admin/models` | GET | All model groups + deployments. |
| `/admin/model-groups/{name}/deployments` | POST | Add a deployment to a group. |
| `/admin/model-groups/{name}/deployments` | DELETE | Remove a deployment. |
| `/admin/model-groups/{name}` | PATCH | Edit group (routing/alias settings). |
| `/admin/aliases` | POST | Create a model alias β†’ group mapping. |

### Pricing & alerts

| Endpoint | Method | Description |
|---|---|---|
| `/admin/pricing` | GET | Per-model token prices (DB-backed `model_prices`), each with its per-provider `scopes`. |
| `/admin/pricing/{model_id}` | PUT | Set/override price for a model. Add `?provider=<account-or-type>` to scope it to one provider; omit for the all-providers base rate. |
| `/admin/pricing/{model_id}` | DELETE | Remove override (fall back to bundled table). With `?provider=` removes only that scope. |
| `/admin/alert-rules` | GET / PUT | Spend/alert rule configuration. |

### Logs, stats, realtime

| Endpoint | Method | Description |
|---|---|---|
| `/admin/logs/requests` | GET | Request logs (paginated; supports key/model/status/time filters). |
| `/admin/logs/proxy` | GET | Proxy stream log (upstream request/response metadata). |
| `/admin/stats/overview` | GET | Rollup: requests/min, token totals, cache-hit %, avg TPS, p95 TTFT, error rate, spend. Ranges ≀ 24 h read the in-memory ring; 7d/30d/all-time read DB aggregates. |
| `/admin/stats/timeseries` | GET | Bucketed token/tps series (bucket size scales with range: 1 min β†’ 1 day). |
| `/admin/stream` | GET (SSE) | Realtime event stream consumed by the UI (live stats, log events). |

### Users

| Endpoint | Method | Description |
|---|---|---|
| `/admin/users` | GET | List user accounts. |
| `/admin/users/{uid}` | PATCH | Change role, disable, reset. |

---

## 4. User accounts (`/auth/*`)

Cookie-session auth for the multi-user UI (separate from virtual keys). Passwords hashed with stdlib PBKDF2; sessions are signed HMAC cookies.

| Endpoint | Method | Description |
|---|---|---|
| `/auth/signup` | POST | Create an account (username, password). |
| `/auth/login` | POST | Log in; sets the session cookie. |
| `/auth/logout` | POST | Clear session. |
| `/auth/me` | GET | Current user + role. |
| `/auth/playground-key` | POST | Mint a scoped temporary virtual key for the playground. |

Roles: normal users see only their own keys' usage in `/app/*`; admins see the full console.

---

## 5. Public (`/public/*`, no auth)

| Endpoint | Method | Description |
|---|---|---|
| `/public/models` | GET | Secret-free model catalog for the public front (names + metadata, no keys, no prices beyond published ones). |

---

## 6. Error bodies

`WiwiError` is the unified internal error type; each wire dialect owns its `error_body()` mapping so clients always see errors in their own dialect:

| Surface | Shape |
|---|---|
| OpenAI Chat | `{"error": {"message", "type", "code"}}` |
| OpenAI Completions | `{"error": {"message", "type", "code"}}` (same envelope as Chat) |
| OpenAI Responses | Responses-style error event / object |
| Anthropic Messages | `{"type":"error","error":{"type":"<anthropic_error_type>","message":…}}` |

Common status codes: `401` (bad key), `402` (budget cap exceeded), `404` (unknown model/group), `413` (body too large), `429` (rate limit β€” a per-key or per-deployment quota with a horizon), `502`/`503` (upstream unavailable after retries/failover), `503` + `Retry-After` (shed: every deployment for the group is at its concurrency ceiling for this request's lane β€” saturated now, not quota spent).

## 7. Worked examples

The tables above are the contract; these are copy-pasteable equivalents.

```bash
MK="Authorization: Bearer $WIWI_MASTER_KEY"
```

### Virtual keys

```bash
# mint β€” budget / RPM / TPM / model allowlist / TTL / optional custom_key
curl -X POST localhost:4000/admin/keys/generate -H "$MK" \
  -d '{"name": "team-a", "max_budget": 10, "rpm": 60, "tpm": 100000,
       "models": ["gpt-4o"], "ttl_seconds": 86400}'
# β†’ {"key":"sk-wiwi-...","id":"k...","note":"store this key now..."}

# mint into a priority lane β€” must name a lane in router_settings.priority_lanes,
# or the call is a 400 rather than a silent fall back to default_lane
curl -X POST localhost:4000/admin/keys/generate -H "$MK" \
  -d '{"name": "nightly", "priority": "bulk"}'

curl localhost:4000/admin/keys -H "$MK"
curl -X PATCH  localhost:4000/admin/keys/<id> -H "$MK" -d '{"max_budget": 20}'
curl -X POST   localhost:4000/admin/keys/<id>/disable -H "$MK"
curl -X DELETE localhost:4000/admin/keys/<id> -H "$MK"
```

### Providers & key pools

```bash
curl localhost:4000/admin/provider-catalog -H "$MK"     # 11 built-in cards + configured?
curl localhost:4000/admin/providers -H "$MK"            # pool status: health + cooldowns
curl -X POST localhost:4000/admin/providers -H "$MK" \
  -d '{"name": "openai-backup", "provider_type": "openai",
       "base_url": "https://api.openai.com/v1", "key": "os.environ/BACKUP_KEY"}'
curl -X PATCH  localhost:4000/admin/providers/<name> -H "$MK" -d '{"name": "openai-primary"}'
curl -X DELETE localhost:4000/admin/providers/<name> -H "$MK"   # 409 while groups reference it

# key pool
curl -X POST  localhost:4000/admin/providers/<name>/keys -H "$MK" \
  -d '{"label": "extra", "key": "os.environ/EXTRA_KEY", "weight": 2}'
curl -X PATCH localhost:4000/admin/providers/<name>/keys/<label> -H "$MK" \
  -d '{"disabled": true, "weight": 5}'          # + reset_status: true clears cooldown
curl -X DELETE localhost:4000/admin/providers/<name>/keys/<label> -H "$MK"
curl localhost:4000/admin/providers/<name>/keys/<label>/secret -H "$MK"  # audit-logged reveal
curl localhost:4000/admin/providers/<name>/models -H "$MK"   # live upstream model ids
```

### Models, groups, aliases

```bash
curl localhost:4000/admin/models -H "$MK"
curl -X PATCH localhost:4000/admin/model-groups/<name> -H "$MK" \
  -d '{"weights": {"openai-main/gpt-4o": 3}, "strategy": "least-busy"}'
curl -X POST localhost:4000/admin/model-groups/<name>/deployments -H "$MK" \
  -d '{"group": "gpt-4o", "provider": "openrouter", "model_id": "openai/gpt-4o", "weight": 1}'
curl -X DELETE localhost:4000/admin/model-groups/<name>/deployments -H "$MK" -d '{...}'
curl -X POST localhost:4000/admin/aliases -H "$MK" \
  -d '{"set": {"gpt-4": "gpt-4o"}, "unset": ["gpt-3.5"]}'
```

### Pricing, logs, stats, users

```bash
curl localhost:4000/admin/pricing -H "$MK"
curl -X PUT    localhost:4000/admin/pricing/<model_id> -H "$MK" -d '{...}'
# Per-provider prices: add ?provider=<account-or-type>. Omit it to set the base rate.
curl -X PUT "localhost:4000/admin/pricing/<model_id>?provider=openai-main" -H "$MK" \
  -d '{"input_per_1m": 1.0, "output_per_1m": 2.0}'

curl localhost:4000/admin/logs/requests -H "$MK"     # DB-backed
curl localhost:4000/admin/logs/proxy -H "$MK"        # ring buffer
curl localhost:4000/admin/stats/overview -H "$MK"    # p50/p95/p99, cost, tokens
curl "localhost:4000/admin/stats/timeseries?bucket=minute&metric=cost&minutes=60" -H "$MK"
curl localhost:4000/admin/stream -H "$MK"            # SSE live tail
curl localhost:4000/admin/alert-rules -H "$MK"
curl -X PUT localhost:4000/admin/alert-rules -H "$MK" -d '{...}'

curl localhost:4000/admin/users -H "$MK"
curl -X PATCH localhost:4000/admin/users/<uid> -H "$MK" -d '{"role": "admin"}'
```

### Session auth

```bash
curl -X POST localhost:4000/auth/signup  -d '{"email": "...", "password": "..."}'
curl -X POST localhost:4000/auth/login   -d '{"email": "...", "password": "..."}'
curl -X POST localhost:4000/auth/logout
curl localhost:4000/auth/me
curl -X POST localhost:4000/auth/playground-key       # scoped 24h session key
```

### OAuth providers (Cline / WorkBuddy)

```bash
# Cline
curl -X POST localhost:4000/admin/cline/oauth/login-url -H "$MK" -d '{}'   # {auth_url, state}
curl -X POST localhost:4000/admin/cline/oauth/connect -H "$MK" -d '{"code": "..."}'
curl -X POST localhost:4000/admin/cline/oauth/auto-connect -H "$MK" -d '{}'
curl localhost:4000/admin/cline/oauth/status -H "$MK"
curl -X POST localhost:4000/admin/cline/oauth/refresh -H "$MK" -d '{"provider": "cline-main"}'
curl -X DELETE localhost:4000/admin/cline/oauth/disconnect -H "$MK" -d '{"provider": "cline-main"}'

# Cline global model list β€” pick ids once, auto-deploy to every Cline account
curl localhost:4000/admin/cline/models -H "$MK"
curl -X PUT localhost:4000/admin/cline/settings -H "$MK" \
  -d '{"default_models": ["anthropic/claude-sonnet-4-5", "openai/gpt-4o"]}'

# Hermes (Nous Portal) β€” device-code OAuth. No API key needed: the first call
# bootstraps the account. Open verification_uri_complete in a browser, approve,
# then poll until it returns connected:true. Each approval ADDS an account as a
# new pool key (key_label in the response); repeat to connect more accounts and
# they round-robin. Tokens auto-refresh ~2 min before expiry and on a 401.
curl -X POST "localhost:4000/admin/hermes/oauth/device-code?provider=hermes-main" -H "$MK"
# -> {state, user_code, verification_uri_complete, expires_in, interval}
curl -X POST localhost:4000/admin/hermes/oauth/poll -H "$MK" -d '{"state": "..."}'
#   -> {"pending": true} until approved, then {"connected": true, "key_label": ...}
curl localhost:4000/admin/hermes/oauth/status?provider=hermes-main -H "$MK"
#   -> {"connected": true, "accounts": [{"key_label", "expires_at", ...}, ...]}
# Force a rotation for one account (key_label optional; defaults to the first):
curl -X POST localhost:4000/admin/hermes/oauth/refresh -H "$MK" -d '{"provider": "hermes-main", "key_label": "acct@example.com"}'
# Disconnect one account, or the whole pool when key_label is omitted:
curl -X DELETE localhost:4000/admin/hermes/oauth/disconnect -H "$MK" -d '{"provider": "hermes-main", "key_label": "acct@example.com"}'

# WorkBuddy (CodeBuddy) β€” parallel API
curl localhost:4000/admin/workbuddy/accounts -H "$MK"
curl -X POST localhost:4000/admin/workbuddy/import -H "$MK" -d '{"accounts": [...]}'
curl -X POST localhost:4000/admin/workbuddy/export -H "$MK"
curl -X POST localhost:4000/admin/workbuddy/refresh -H "$MK" -d '{"label": "main"}'
```