Using the API

Streaming

Receive the article token by token with server-sent events, and get usage in the last chunk.

Set stream to true and the API sends the article as server-sent events while it is being written. The format matches OpenAI, so SDK stream helpers work unchanged.

curl https://api.seosorted.ai/v1/chat/completions \
  --no-buffer \
  -H "Authorization: Bearer $SEOSORTED_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "inkwell-1",
  "stream": true,
  "stream_options": {
    "include_usage": true
  },
  "messages": [
    {
      "role": "user",
      "content": "Topic: Why most home espresso tastes sour\nAudience: people who just bought their first espresso machine\nKey points: grind finer in small steps; weigh the dose; aim for 25 to 30 seconds\nLength: about 600 words"
    }
  ]
}'

Event format

Each event is a line that starts with data: followed by a chat.completion.chunk object. The first chunk carries the assistant role, the following chunks carry text in delta.content, and the stream ends with data: [DONE].

Event stream
data: {"id":"chatcmpl-5f0c","object":"chat.completion.chunk","model":"inkwell-1","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-5f0c","object":"chat.completion.chunk","model":"inkwell-1","choices":[{"index":0,"delta":{"content":"Your new"},"finish_reason":null}]}

data: {"id":"chatcmpl-5f0c","object":"chat.completion.chunk","model":"inkwell-1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-5f0c","object":"chat.completion.chunk","model":"inkwell-1","choices":[],"usage":{"prompt_tokens":74,"completion_tokens":812,"total_tokens":886}}

data: [DONE]

Usage in the final chunk

Add stream_options: {"include_usage": true} to receive token counts. They arrive in one extra chunk just before [DONE], with an empty choices array. Check for an empty array before reading choices[0].

Cold starts and keepalives

When a model has been idle, the first request can wait a few minutes for a GPU. During that time the API sends comment lines that start with a colon to keep the connection open. SSE clients ignore them. Set your HTTP client timeout to at least 10 minutes.

Errors that happen after the stream has started arrive as a data: event that contains an error object, followed by [DONE]. Handle both in your stream loop.