Usage ai API
This page documents the 1 gateway endpoint tagged usage_ai in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.
| Method | Path | Summary |
|---|---|---|
| POST | /usage/ai/chat | Ask questions about usage in plain language |
Endpoints
POST /usage/ai/chat
Ask questions about usage in plain language
Answer a question about spend, models, keys and trends from the daily activity rollups, as a streamed reply.
This is the Ask AI panel of the Usage page, and only proxy admins may call it: the route check refuses admin
viewers with a 403 and other roles with a 401. model picks the model that writes the answer, a model group from
the model hub is answered through the router, and the gateway's default is used when it is left out. The last 20
messages are sent with each question. A failure after the stream started arrives as a type: error event.
Docs (https://docs.spanway.ai/docs/admin_ui/usage)
Request body (application/json, schema UsageAIChatRequest):
| Field | Type | Required | Description |
|---|---|---|---|
messages | array of ChatMessage | Yes | Chat messages (user/assistant history) |
model | string or null | No | Model to use for AI chat |
Example request:
curl -X POST "http://localhost:4000/usage/ai/chat" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"content": "Which team spent the most this week?",
"role": "user"
}
]
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | A server-sent event stream with the assistant's answer and the tools it called | text/event-stream string |
| 401 | No valid key was sent, or the key belongs to someone who is not a proxy admin | application/json ProxyError |
| 403 | An admin viewer tried to change something, and admin viewers can only read | application/json ProxyError |
| 422 | Validation Error | application/json HTTPValidationError |