Skip to main content

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.

MethodPathSummary
POST/usage/ai/chatAsk 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):

FieldTypeRequiredDescription
messagesarray of ChatMessageYesChat messages (user/assistant history)
modelstring or nullNoModel 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:

StatusDescriptionBody
200A server-sent event stream with the assistant's answer and the tools it calledtext/event-stream string
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
403An admin viewer tried to change something, and admin viewers can only readapplication/json ProxyError
422Validation Errorapplication/json HTTPValidationError