API reference

Streaming

How Abliterator streams responses as Server-Sent Events in OpenAI and Anthropic formats, where the final usage and cost arrive, and client code for Python and Node.

Reviewed APIStreamingSSE

Quick answer

Set stream: true. OpenAI-format streams always end with a usage chunk carrying usage.cost, then data: [DONE]. Anthropic-format streams end with message_delta (usage) and message_stop.

How Abliterator streams

Add "stream": true to a Chat Completions or Messages request. The response is text/event-stream, relayed event by event as the model generates it. Abliterator changes only three things:

  • model in every event is the canonical model ID.
  • In the OpenAI format, the usage chunk gets a cost field in USD.
  • SSE comment lines (lines starting with :) from the serving engine are dropped.

The X-Abl-Generation-Id, X-Abl-Model and X-Abl-Provider headers arrive with the stream. X-Abl-Cost-Usd is not sent on streams, because the cost is not known until the end. Read it from the final usage chunk or from GET /api/v1/generation.

OpenAI format

Each event is a data: line holding a chat.completion.chunk. Content arrives in choices[0].delta.content, reasoning in choices[0].delta.reasoning_content, and tool calls in choices[0].delta.tool_calls. The last chunk with choices carries finish_reason.

Abliterator always sets stream_options.include_usage to true, so after the content the model sends one chunk with empty choices and a usage object. Then the stream ends with data: [DONE]:

data: {"id":"chatcmpl-1a2b","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}

data: {"id":"chatcmpl-1a2b","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{"content":"Rotate the key"}}]}

data: {"id":"chatcmpl-1a2b","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-abliterated","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-1a2b","object":"chat.completion.chunk","model":"deepseek-v4.1-flash-abliterated","choices":[],"usage":{"prompt_tokens":24,"completion_tokens":182,"total_tokens":206,"cost":0.000388000}}

data: [DONE]

Any other stream_options you send are kept.

Python

import os

from openai import OpenAI

client = OpenAI(base_url="https://abliterator.ai/v1", api_key=os.environ["ABL_API_KEY"])

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash-abliterated",
    max_tokens=4096,
    stream=True,
    messages=[{"role": "user", "content": "Write a five-step runbook for revoking a leaked cloud access key."}],
)

for chunk in stream:
    if chunk.choices:
        delta = chunk.choices[0].delta
        # Reasoning streams first, in delta.reasoning_content; print only the answer here.
        if delta.content:
            print(delta.content, end="", flush=True)
    if chunk.usage:
        print(f"\n\ntokens: {chunk.usage.total_tokens}, cost: ${chunk.usage.cost}")

Node

import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://abliterator.ai/v1", apiKey: process.env.ABL_API_KEY });

const stream = await client.chat.completions.create({
  model: "deepseek-v4.1-flash-abliterated",
  max_tokens: 4096,
  stream: true,
  messages: [{ role: "user", content: "Write a five-step runbook for revoking a leaked cloud access key." }],
});

for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content;
  if (text) process.stdout.write(text);
  if (chunk.usage) console.log(`\n\ntokens: ${chunk.usage.total_tokens}, cost: $${chunk.usage.cost}`);
}

curl

curl -N https://abliterator.ai/v1/chat/completions \
  -H "Authorization: Bearer $ABL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "deepseek-v4.1-flash-abliterated", "max_tokens": 1024, "stream": true,
       "messages": [{"role": "user", "content": "Define lateral movement in one paragraph."}]}'

-N turns off curl's output buffering so events print as they arrive.

Anthropic format

The Messages stream uses named events, as Anthropic's API does:

Event Contains
message_start The message envelope and input usage (input_tokens, cache_read_input_tokens)
content_block_start A new text, thinking or tool_use block
content_block_delta text_delta, thinking_delta or input_json_delta
content_block_stop End of a block
message_delta stop_reason and the output token count
message_stop End of the message

Usage arrives in message_start (input) and message_delta (output). There is no cost field; look up the generation ID afterward.

import os

import anthropic
import requests

client = anthropic.Anthropic(base_url="https://abliterator.ai", api_key=os.environ["ABL_API_KEY"])

with client.messages.stream(
    model="deepseek-v4.1-flash-abliterated",
    max_tokens=4096,
    messages=[{"role": "user", "content": "List the indicators that a phishing email impersonates an internal IT team."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    generation_id = stream.response.headers["x-abl-generation-id"]

record = requests.get(
    "https://abliterator.ai/api/v1/generation",
    params={"id": generation_id},
    headers={"Authorization": f"Bearer {os.environ['ABL_API_KEY']}"},
    timeout=30,
).json()
print("\ncost:", record["data"]["cost"])

When is a stream complete

A complete OpenAI-format stream has a chunk with finish_reason, a usage chunk and data: [DONE]. A complete Anthropic stream has message_stop. If the connection closes before those arrive, the response was cut off, usually by a network failure or the model's connection breaking. Treat it as failed and retry if you need the full answer.

A finish_reason of length (max_tokens in Anthropic stop_reason) means the output hit max_tokens. With reasoning on, the reasoning may have used most of it.

Disconnects and billing

When your client stops reading a stream, the model has already been asked for up to max_tokens of output. Abliterator keeps reading the model's stream for up to 60 seconds after the disconnect so it can bill the real usage, then cancels the request. During that time the request still holds a concurrency slot. If no usage report arrives, Abliterator bills an estimate from the prompt and the text streamed so far.

The generation record's status shows what happened: client_closed_metered (usage reported) or client_closed_estimated (estimated).

Cancelling is not a reliable way to cap cost. Set max_tokens to what you need.

Proxies and timeouts

Abliterator sends Cache-Control: no-cache and X-Accel-Buffering: no so that proxies pass events through without buffering. If you put your own reverse proxy in front of Abliterator, turn off response buffering for these paths.

Long reasoning can take minutes before the first content token, though reasoning deltas stream during that time. Set client read timeouts to several minutes for kimi-k3.1-pro-abliterated at high effort.

Frequently asked questions

Do I need stream_options.include_usage?

No. Abliterator always requests usage from the model, so every OpenAI-format stream ends with a usage chunk that includes the cost, whether or not you set stream_options.