Skip to main content
Docs
Chat Completions

Chat Completions

Use the chat completions endpoint to create conversational AI responses.

The chat completions endpoint creates AI responses for conversational messages. This is the primary endpoint for most applications.

Endpoint

POST https://api.elyxir.ai/v1/chat/completions

Request

Headers

HeaderRequiredDescription
AuthorizationYesBearer token: Bearer elyxir_xxx
Content-TypeYesMust be application/json

Body Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID, or alvin for auto-routing
messagesarrayYesArray of message objects
temperaturenumberNoRandomness (0-2, default: 1)
max_tokensintegerNoMaximum response tokens
streambooleanNoEnable streaming (default: false)
stream_optionsobjectNoe.g. {"include_usage": true} to get token counts on the final chunk
top_pnumberNoNucleus sampling (0-1)
stopstring/arrayNoStop sequences
presence_penaltynumberNoPenalize new topics (-2 to 2)
frequency_penaltynumberNoPenalize repetition (-2 to 2)
toolsarrayNoFunction tools the model may call
tool_choicestring/objectNoauto, none, required, or a named tool
response_formatobjectNoe.g. {"type": "json_object"} for JSON output

Not every model supports every parameter — tool calling and structured output in particular vary by provider. Check the model in the Model Hub if a parameter appears to be ignored.

Message Object

FieldTypeRequiredDescription
rolestringYessystem, user, assistant, or tool
contentstring | arrayYesText, or an array of content parts for vision

Vision

Pass an array of content parts to send images to a vision-capable model:

{
  "model": "gpt-5.5",
  "messages": [
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What is in this image?"},
        {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
      ]
    }
  ]
}

A base64 data URI works in place of an HTTPS URL.

Example Request

curl -X POST https://api.elyxir.ai/v1/chat/completions \
  -H "Authorization: Bearer elyxir_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is the capital of France?"}
    ],
    "temperature": 0.7,
    "max_tokens": 150
  }'

Response

Success (200 OK)

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1234567890,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 10,
    "total_tokens": 35
  }
}

Response Fields

FieldDescription
idUnique completion ID
objectObject type (chat.completion)
createdUnix timestamp
modelModel used for completion
choicesArray of completion choices
usageToken usage statistics

Finish Reasons

ReasonDescription
stopNatural completion or stop sequence
lengthHit max_tokens limit
content_filterFiltered by content moderation

Multi-turn Conversations

Include previous messages for context:

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is Python?"},
    {"role": "assistant", "content": "Python is a programming language..."},
    {"role": "user", "content": "How do I install it?"}
]
 
response = requests.post(
    "https://api.elyxir.ai/v1/chat/completions",
    headers=headers,
    json={"model": "gpt-4o-mini", "messages": messages}
)

Streaming

Enable streaming for real-time responses:

curl -X POST https://api.elyxir.ai/v1/chat/completions \
  -H "Authorization: Bearer elyxir_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "Tell me a story"}],
    "stream": true
  }'

Streaming returns Server-Sent Events (SSE):

data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":"The"}}]}
data: {"id":"chatcmpl-abc","choices":[{"delta":{"content":" capital"}}]}
data: [DONE]

Add "stream_options": {"include_usage": true} to receive token counts on the final chunk — otherwise a streamed response carries no usage block.

Alvin auto-routing

Set model to alvin and Alvin picks the model per request. The response reports what it chose:

HeaderDescription
X-Alvin-ModelThe model that handled the request
X-Alvin-Task-TypeThe task class assigned to the prompt
X-Alvin-ConfidenceClassification confidence, 0.001.00

The same information appears in a routing field in the response body. See Alvin routing.

Available Models

ModelProviderBest For
alvinElyxirAuto-routing — Alvin picks the model
gpt-5.5OpenAICurrent flagship, complex reasoning
gpt-5-miniOpenAIBalanced, cost-effective
gpt-4o-miniOpenAICheapest general-purpose chat
claude-opus-4.8AnthropicAgentic coding, hardest tasks
claude-sonnet-4.6AnthropicCoding and agents
claude-haiku-4.5AnthropicFast, affordable
gemini-3.5-flashGoogleFast, long context
gemini-2.5-proGoogleBalanced, 1M context
grok-4.3xAICost-efficient reasoning
perplexity-sonarPerplexityWeb-grounded answers

This is a shortlist. Call GET /v1/models for the live catalog, or see Supported models for the full list with pricing.

Error Responses

StatusMeaning
400Bad request (invalid parameters)
401Authentication failed
402Insufficient credits
429Rate limit exceeded
500Server error

See Error Handling for details.

Chat Completions | Alvin