Create a chat completion
Use CometAPI POST /v1/chat/completions to send multi-message conversations to chat models with streaming, structured output, and tool calling.
base_url="https://api.cometapi.com/v1" and your CometAPI API key.
The basic, image, streaming, and structured-output examples use gpt-6-astra. The function-calling example uses gpt-5.6-sol, and the log-probability example uses gpt-4.1.
Message roles
Send multimodal input
Use an array of content parts to send an image with text:image_url.detail to select image processing detail. This example uses high.
Stream responses
To receive incremental output, setstream to true. The response is delivered as Server-Sent Events (SSE). Text arrives in chat.completion.chunk objects; this abbreviated example includes the final usage chunk:
Request structured output
Useresponse_format to request structured output:
Call tools and functions
Usegpt-5.6-sol with a function definition:
finish_reason: "tool_calls" and a message.tool_calls array. Parse the function’s JSON-encoded arguments, execute your function, then append the assistant message and a tool result message with its matching tool_call_id.
Choose request parameters
Model-specific parameters
Model-specific parameters
reasoning_effort and max_completion_tokens with GPT-6 Astra. The Functions and Logprobs examples show the request options for gpt-5.6-sol and gpt-4.1, respectively.For Claude- or Gemini-specific request formats, see Anthropic Messages and Gemini Generate content.max_tokens and max_completion_tokens
max_tokens and max_completion_tokens
max_completion_tokens to limit generated tokens for the GPT-6 Astra examples. This includes reasoning and visible output, so allow room for both. max_tokens is a legacy parameter.Instruction roles
Instruction roles
developer for application instructions in the GPT-6 Astra examples. Keep end-user content in user messages and preserve previous assistant replies when continuing a conversation.FAQ
How to handle rate limits?
When encountering429 Too Many Requests, implement exponential backoff:
How to maintain conversation context?
Include the full conversation history in themessages array:
What does finish_reason mean?
How to control costs?
- Use
max_completion_tokensto cap output length. - Compare model pricing and choose a model that meets your workload requirements.
- Keep prompts concise — avoid redundant context.
- Monitor token usage in the
usageresponse field.
Authorizations
Bearer token authentication. Use your CometAPI key.
Body
Model ID to use for this request. See the Models page for current options.
"gpt-6-astra"
Conversation messages, including instructions, user input, assistant replies, and tool results.
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
Sampling temperature. Omit this field for GPT-6 Astra.
0 <= x <= 2Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Stop string or list of up to four strings, for models that support stop sequences.
Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.
Number between -2.0 and 2.0. Positive values penalize tokens based on whether they have already appeared, encouraging the model to explore new topics.
-2 <= x <= 2Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.
-2 <= x <= 2A JSON object mapping token IDs to bias values from -100 to 100. The bias is added to the model's logits before sampling. Values between -1 and 1 subtly adjust likelihood; -100 or 100 effectively ban or force selection of a token.
A unique identifier for your end-user. Helps with abuse detection and monitoring.
Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
Function definitions for a model that supports tool calling on Chat Completions. The function example uses GPT-5.6 Sol; use Responses for GPT-6 Astra tool calls.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Reasoning effort supported by the selected model. The GPT-6 Astra examples use low; the GPT-5.6 Sol function example uses none. See the model reference for other supported levels.
Options for streaming. Only valid when stream is true.
Specifies the processing tier.
auto, default, flex, priority Response
Successful chat completion response.
Unique completion identifier.
"chatcmpl_example"
Object type. Non-streaming responses use chat.completion.
chat.completion "chat.completion"
Unix timestamp of creation.
1788763703
The model used (may include version suffix).
"gpt-6-astra"
Array of completion choices.
Token accounting for this request. Billing uses these counts.
Service tier that processed the request, when returned.
Model configuration fingerprint, when returned.