Streaming
Standard server-sent events: set stream: true. Ask for stream_options.include_usage and the final chunk carries the same token counts a non-streaming response would, which is also exactly what you are billed.
stream = client.chat.completions.create(
model="claude-sonnet-5",
messages=[{"role": "user", "content": "Count to ten"}],
stream=True,
stream_options={"include_usage": True}, # final chunk carries token usage
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
if chunk.usage:
print("\n", chunk.usage)pythonWhat passes through untouched
We proxy, we do not reinterpret. All of this reaches the model exactly as you sent it:
- Tool calling and structured output
- Vision inputs and system prompts
- Prompt caching
- Sampling parameters and stop sequences
