Chat completions

POST /v1/chat/completions is the OpenAI-compatible path and the one most clients call. This page is the request and response shape, plus the parameters that change behaviour and cost.

The request

Three fields are required: model, messages and nothing else. messages is an ordered list, and each item has a role (system, user, assistant or tool) and content.

The system message sets behaviour for the whole conversation. Put durable instructions there - output format, language, coding conventions - and keep the user message about the current task.

  • model: the exact id from /v1/models, including the provider prefix
  • messages: role plus content, oldest first
  • stream: true returns server-sent events instead of one JSON body
  • max_tokens: caps the output; output is the expensive side
  • temperature: lower for code and extraction, higher for ideation
  • tools: function definitions the model may call, answered with a tool message

The response

The body carries choices, and each choice carries a message plus a finish_reason. Read finish_reason before you read the content: length means max_tokens cut the answer, tool_calls means the model wants a function result, stop means it finished normally.

Usage reports prompt_tokens, completion_tokens and total_tokens. Those are the numbers the bill is computed from, and the only ones worth logging if you want to explain a bill later.

Streaming

With stream set to true the response arrives as data events, each carrying a delta instead of a full message. Accumulate deltas on the content field and stop when you see the done marker.

Streaming does not change the price; it changes the perceived latency. For a command-line tool it adds complexity for nothing; for a UI where a person waits, it is the difference between five seconds of nothing and immediate feedback.

Tools and structured output

When you pass tools, the model may answer with tool_calls instead of text. You execute the function, then send the result back as a message with role tool and the matching tool_call_id. The loop ends when the model returns content with finish_reason stop.

For machine-readable answers, response_format with a JSON schema is more reliable than asking for JSON in the prompt, because the shape is enforced rather than requested. Validate the parsed object anyway - schema enforcement is not a substitute for checking your own invariants.

Related pages