Skip to main content

AI Gateway: Chat Completions

OpenAI-compatible chat completions endpoint backed by Mersal's multi-provider AI Pool.

3 min read

The AI Gateway is a front door to Mersal's internal AI Pool — a multi-provider AI router with automatic failover. Instead of integrating against a single AI provider and hardcoding it into your app, you send requests to Mersal's OpenAI-compatible endpoint, and the AI Pool routes the request to a healthy underlying provider. If one provider is degraded or down, the pool fails over automatically, so you still get a working completion.

The request and response shapes follow the OpenAI chat completions convention, so existing OpenAI-compatible tooling generally works with minimal changes — just point it at Mersal's base URL and use your AI Pool key.

Endpoint

POSThttps://mersal.it/api/ai/v1/chat/completions

Authentication

This endpoint uses a different authentication mechanism from the channel-sending endpoints: a Bearer token, not the Api-key header.

Authorization: Bearer <ai-pool-key>

AI Pool keys are created specifically for AI Gateway access, separate from your channel API key. See Authentication for details on where to generate one.

Request parameters

NameTypeRequiredDescription
messagesarrayYesArray of chat messages, each with role (e.g. user, assistant, system) and content.
temperaturenumberNoSampling temperature controlling response randomness.
max_tokensnumberNoMaximum number of tokens to generate in the completion.

Example request

curl -X POST https://mersal.it/api/ai/v1/chat/completions \
  -H "Authorization: Bearer YOUR_AI_POOL_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      { "role": "user", "content": "Hello" }
    ],
    "temperature": 0.5,
    "max_tokens": 600
  }'

Example response

{
  "id": "chatcmpl-xxxxxxx",
  "object": "chat.completion",
  "created": 1730000000,
  "model": "mersal-pool",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "Hi there!" },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 12, "completion_tokens": 8, "total_tokens": 20 },
  "mersal_pool": { "provider": "...", "model": "...", "latency_ms": 420, "attempts": 1 }
}

The mersal_pool object reports which underlying provider actually served the request, how long it took, and how many attempts the pool made (relevant if the first provider it tried failed over to another).

Listing available models

GEThttps://mersal.it/api/ai/v1/models

Returns the models currently available through the AI Pool, using the same Bearer authentication.

Errors

Common failure modes are a missing/invalid Bearer token, or throttling if you exceed your key's configured request limits — see Rate Limits. For the general error format, see Errors and Status Codes.

Was this page helpful?
AI Gateway: Chat Completions · Mersal Docs