Streaming responses
Chat Completions can stream incremental server-sent events when the selected model and requested features support streaming. Set stream: true; with cURL use -N to disable output buffering.
curl -N https://api.thecodeapi.com/v1/chat/completions \
-H "Authorization: Bearer $THE_CODE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"REPLACE_WITH_A_CHAT_MODEL_ID","messages":[{"role":"user","content":"Say hello."}],"stream":true}'Replace the placeholder with an ID returned by model discovery. Parse SSE incrementally: ignore comment frames such as : ping, handle each data: frame, and finish on data: [DONE]. Do not parse the entire SSE stream as one JSON response. OpenAI SDK streaming iterators handle the framing for you.
Accumulate text deltas. For tool calls, assemble argument fragments by tool-call index and preserve provider metadata when replaying the assistant message. See the agent guide for tool replay details.
Completion, cancellation, and errors
Inspect the HTTP status before reading a successful stream, and handle error events or interrupted streams after headers have been sent. An interrupted stream is not a completed answer. Do not replay paid requests blindly; use the documented idempotency rules.
Close or abort the client connection when the user cancels. Cancellation does not guarantee that upstream processing stops immediately or that no credits are consumed. Check recorded usage for the request.
The documented Responses subset is non-streaming. Structured-output validation may also require stream: false. Check the live model contract and API guide for the combination you use.