Skip to main content

Create Chat Completion

POST /v1/chat/completions
Generates a model response based on the input message list. Supports both streaming and non-streaming response modes.

Authentication

Pass the API Key in the request header:
Authorization: Bearer YOUR_API_KEY

Request Parameters

ParameterTypeRequiredDefaultDescription
modelstringYes-Model name, e.g. gpt-4o, claude-sonnet-4-20250514, gemini-2.5-flash
messagesarrayYes-Message list, each message contains role and content
temperaturenumberNo1Sampling temperature, range 0-2. Higher values produce more random output
top_pnumberNo1Nucleus sampling parameter, range 0-1. Use either this or temperature
nintegerNo1Number of responses to generate
streambooleanNofalseWhether to enable streaming output
stream_optionsobjectNonullStreaming options. Set {"include_usage": true} to return usage in the last chunk
stopstring|arrayNonullStop generation tokens, up to 4
max_tokensintegerNoModel defaultMaximum number of tokens to generate
presence_penaltynumberNo0Presence penalty, range -2.0 to 2.0
frequency_penaltynumberNo0Frequency penalty, range -2.0 to 2.0
logprobsbooleanNofalseWhether to return logprobs
top_logprobsintegerNonullNumber of top logprobs to return, 0-20
response_formatobjectNonullSpecify output format, e.g. {"type": "json_object"}
seedintegerNonullRandom seed for reproducible output
toolsarrayNonullAvailable tools list (Function Calling)
tool_choicestring|objectNo”auto”Tool selection strategy
userstringNonullEnd-user identifier

messages Format

[
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "Hello"},
  {"role": "assistant", "content": "Hello! How can I help you?"},
  {"role": "user", "content": "Tell me about yourself"}
]
Supported role values: system, user, assistant, tool.

Non-Streaming Request

curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Explain artificial intelligence in one sentence"}
    ],
    "temperature": 0.7
  }'

Non-Streaming Response

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1709123456,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Artificial intelligence is the technology that enables computers to simulate human intelligent behavior, allowing machines to learn, reason, and solve problems."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 32,
    "total_tokens": 60
  }
}

Streaming Request

curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Explain artificial intelligence in one sentence"}
    ],
    "stream": true,
    "stream_options": {"include_usage": true}
  }'

Streaming Response

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"Artificial intelligence"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":" is"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[],"usage":{"prompt_tokens":28,"completion_tokens":32,"total_tokens":60}}

data: [DONE]
When stream_options.include_usage is set to true, the last chunk will contain the complete usage information.
The max_tokens parameter limits the number of output tokens, not including input. If the output is truncated, finish_reason will return length.