Chat Completions

POST /api/v1/chat/completions — the OpenAI-compatible chat completions endpoint. Send messages, get a response. Supports every model in the AnyRouter catalog.

Chat Completions

Send a sequence of messages to a model and receive a completion. This is the primary AnyRouter endpoint and is a drop-in replacement for OpenAI's /v1/chat/completions.

TEXT
text
POST https://anyrouter.dev/api/v1/chat/completions

Request body

FieldTypeRequiredDescription
modelstringyesprovider/model id (e.g. openai/gpt-4-turbo). See List models.
messagesarrayyesOrdered conversation. Each message has role (system / user / assistant / tool) and content.
temperaturenumberno0–2. Lower is more deterministic. Default: 1.
top_pnumberno0–1. Nucleus sampling. Default: 1.
max_tokensintegernoUpper bound on output tokens.
streambooleannoIf true, returns a streaming SSE response.
stopstring | string[]noStop sequences.
toolsarraynoFunction-calling / tool-use spec.
tool_choicestring | objectnoForce a specific tool or auto.
response_formatobjectno{ "type": "json_object" } for guaranteed JSON.
seedintegernoDeterministic sampling seed (provider-dependent).
userstringnoStable end-user identifier for abuse monitoring.

Example

BASH
bash
curl https://anyrouter.dev/api/v1/chat/completions \
  -H "Authorization: Bearer ar-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4.6",
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Explain NAT in one sentence."}
    ],
    "max_tokens": 200
  }'

Response

JSON
json
{
  "id": "chatcmpl-8yWq4JqLfEjJ9L",
  "object": "chat.completion",
  "created": 1760000000,
  "model": "anthropic/claude-sonnet-4.6",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "NAT maps many private IP addresses to one public IP so devices behind a router can share a single internet connection."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 30,
    "total_tokens": 58
  }
}

Finish reasons

ValueMeaning
stopModel completed naturally or hit a stop sequence.
lengthHit max_tokens.
tool_callsModel invoked one or more tools; caller must run them and resume.
content_filterUpstream safety filter triggered.
errorUpstream provider error; see error on the response.

Tool use

Pass a tools array with function schemas, then handle any returned tool_calls:

TYPESCRIPT
typescript
const response = await client.chat.completions.create({
  model: "openai/gpt-4-turbo",
  messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
  tools: [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get the current weather for a city",
        parameters: {
          type: "object",
          properties: { city: { type: "string" } },
          required: ["city"],
        },
      },
    },
  ],
})

See Tool use for a deeper walkthrough.

  • Streaming — receive tokens incrementally
  • Errors — status codes + retry guidance
  • Models — list and inspect every supported model