Output format
Every chat completion response from Spanway is a ModelResponse, which has the same fields as an OpenAI chat completion. Code that reads response.choices[0].message.content works for every provider, and the usage block always reports tokens the same way, which is what makes cost tracking and budgets work across providers. Provider specific information is added in extra fields so that nothing standard changes shape.
Scenario
A finance team builds a monthly report of model spend per project and needs every response to report tokens in the same place regardless of provider. Their application reads usage.prompt_tokens and usage.completion_tokens from each response and the x-spanway-response-cost header, and the gateway fills both in for every provider, including streaming responses where the provider sends no usage of its own.
Steps
-
Send a request and keep the headers.
curl -i -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "Name a planet."}]}' -
Read the response. The body is the chat completion object below, and the headers include
x-spanway-response-costwith the cost in USD,x-spanway-model-idwith the deployment that answered andx-spanway-call-idwith the id to look the call up later. -
Compare with the log. In the Admin UI open Logs (
/ui/logs), paste the call id into the search box of the Request Logs tab, open the row and check that the Tokens and Cost in the detail drawer match the body and the header. -
For a streaming request add
"stream": trueand"stream_options": {"include_usage": true}to the body. The lastchat.completion.chunkevent carriesusage, which the gateway computes when the provider does not send it.
Expected result
{
"id": "chatcmpl-8f6c2a1e",
"object": "chat.completion",
"created": 1760441234,
"model": "claude-sonnet-5-5",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "Mars.", "tool_calls": null, "function_call": null}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 11, "completion_tokens": 3, "total_tokens": 14}
}
The Logs row shows the same token counts and a cost equal to the header value.
Fields
| Field | Meaning |
|---|---|
id | Response id. On the gateway it can be used to look the request up in the logs. |
object | chat.completion, or chat.completion.chunk when streaming. |
created | Unix timestamp. |
model | The model that produced the response. |
choices | List of choices, each with index, message and finish_reason. |
usage | prompt_tokens, completion_tokens, total_tokens, plus prompt_tokens_details and completion_tokens_details with cached, audio and reasoning token counts when the provider reports them. |
system_fingerprint | Provider fingerprint when available. |
The message has role, content, tool_calls and, for reasoning models, reasoning_content with the model's reasoning text where the provider exposes it. Provider fields that have no OpenAI equivalent are kept under provider_specific_fields.
Finish reasons
finish_reason is normalised to stop, length, tool_calls, content_filter or function_call, so a truncated Anthropic response and a truncated OpenAI response both report length.
Streaming
With stream: true the gateway returns server-sent events. Each event is a chat.completion.chunk with a delta instead of a message. Tool calls arrive as incremental tool_calls deltas. When stream_options.include_usage is set, the last chunk carries usage, which the gateway computes for providers that do not send it.
Cost and gateway headers
The response headers from the gateway include x-spanway-response-cost, x-spanway-model-id, x-spanway-call-id and timing headers, see calling the gateway. The cost comes from the model's prices in the price map, or from custom pricing on the deployment.
Errors
| Status | What it means | Fix |
|---|---|---|
Response without usage | The provider sent no usage and the request was a stream without stream_options.include_usage. | Add "stream_options": {"include_usage": true}. The gateway then counts tokens itself. |
x-spanway-response-cost is 0 | The model has no price in the price map and the deployment sets none. | Add input_cost_per_token and output_cost_per_token to the deployment, see custom pricing. |
finish_reason is length | The reply hit max_tokens. | Raise max_tokens or ask for a shorter answer. |
finish_reason is content_filter | The provider's safety filter stopped the reply. | Change the prompt, or route to another deployment with fallbacks. |
Failures are returned as OpenAI-shaped error bodies with matching status codes, described in exception mapping.
Related
- Chat completions for the request.
- Input parameters.
- Cost tracking for how the cost in the header is computed and stored.
- Logs page for finding a response by its call id.