API
Streaming
Consume normalized server-sent events without buffering the response.
Stream with Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SKILLROUTER_API_KEY"],
base_url="https://api.useskillrouter.com/v1",
)
stream = client.chat.completions.create(
model="skillrouter/seo-audit",
messages=[{"role": "user", "content": "Audit example.com"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
payload = chunk.to_dict()
if payload.get("error"):
error = payload["error"]
raise RuntimeError(f"{error.get('code')}: {error.get('message')}")
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)The wire endpoint uses data-only SSE records and terminates with data: [DONE]. Request stream_options.include_usage when you need the final usage chunk.
Cancellation and usage
- Close the response body when the client no longer needs output.
- SkillRouter aborts the upstream request when the disconnect can be detected.
- A final usage chunk is emitted when the upstream provider supplies streaming usage.
- Partial streams can create partial usage and a reconciled ledger charge.
- An error before the first model event uses its normal HTTP status. A later error is a chat.completion.chunk with finish_reason error and a top-level SkillRouter error, followed by [DONE].
- Do not parse SSE by splitting arbitrary network chunks on newlines without buffering incomplete frames.