Docs

Create Chat Completion

POST/v1/chat/completions

Creates a completion for the chat message

On this page
  1. Request Body
  2. Response
  3. Request Example
  4. Multimodal Example
  5. Response Example
  6. Error Response Example

Request Body

Parameter model

Type: string

Location: body

Required: yes

ID of the model to use. View available models in Console or with GET /v1/models.

Parameter messages

Type: array

Location: body

Required: yes

A list of messages comprising the conversation.

Each message object contains:

  • role (string): system, user, or assistant
  • content (string | array): The message content

When content is an array, AnyPath supports structured multimodal blocks for compatible models:

  • text: { "type": "text", "text": "..." }
  • image: { "type": "image_url", "image_url": { "url": "https://..." } }

For multimodal production traffic, prefer public https URLs.

Parameter temperature

Type: number

Location: body

Sampling temperature between 0 and 2. Higher values make output more random.

Parameter max_tokens

Type: integer

Location: body

Maximum number of tokens to generate.

Parameter stream

Type: boolean

Location: body

If true, partial message deltas will be sent as SSE events.

Parameter stream_options

Type: object

Location: body

Options for streaming. Set include_usage: true to receive token usage in stream chunks.

Parameter top_p

Type: number

Location: body

Nucleus sampling parameter. We recommend altering this or temperature, not both.

Parameter frequency_penalty

Type: number

Location: body

Number between -2.0 and 2.0. Positive values penalize repeated tokens.

Parameter presence_penalty

Type: number

Location: body

Number between -2.0 and 2.0. Positive values penalize tokens already in the text.

Parameter stop

Type: string | array

Location: body

Up to 4 sequences where the API will stop generating tokens.

Parameter tools

Type: array

Location: body

A list of tools the model may call (function calling).

Parameter tool_choice

Type: string | object

Location: body

Controls how the model uses tools. Options: auto, none, required, or a specific tool object.

Parameter parallel_tool_calls

Type: boolean

Location: body

Whether to enable parallel function calling. Set to false to call functions sequentially.

Parameter max_completion_tokens

Type: integer

Location: body

Maximum tokens for the completion. Alternative to max_tokens, useful for newer reasoning-enabled model families.

Parameter reasoning_effort

Type: string

Location: body

Reasoning effort for reasoning-enabled models. Options: low, medium, high.

Parameter seed

Type: integer

Location: body

Random seed for deterministic sampling.

Parameter n

Type: integer

Location: body

Number of completions to generate (1-128).

Parameter logprobs

Type: boolean

Location: body

Whether to return log probabilities.

Parameter top_logprobs

Type: integer

Location: body

Number of top log probabilities to return (0-20). Requires logprobs: true.

Parameter top_k

Type: integer

Location: body

Top-K sampling parameter (for Anthropic/Gemini models).

Parameter response_format

Type: object

Location: body

Response format specification. Use {"type": "json_object"} for JSON mode. Treat {"type": "json_schema", "json_schema": {...}} as a best-effort path that depends on the selected model and routed behavior.

Parameter logit_bias

Type: object

Location: body

Modify the likelihood of specified tokens appearing. Map token IDs (as strings) to bias values from -100 to 100.

Parameter user

Type: string

Location: body

A unique identifier representing your end-user for abuse monitoring.

Response

Response Field id

Type: string

Unique identifier for the completion.

Response Field object

Type: string

Always chat.completion.

Response Field created

Type: integer

Unix timestamp of when the completion was created.

Response Field model

Type: string

The model used for completion.

Response Field choices

Type: array

List of completion choices.

Each choice contains:

  • index (integer): Index of the choice
  • message (object): The generated message
  • finish_reason (string): Why the model stopped (stop, length, tool_calls)

Response Field usage

Type: object

Token usage statistics.

  • prompt_tokens (integer): Tokens in the prompt
  • completion_tokens (integer): Tokens in the completion
  • total_tokens (integer): Total tokens used

Request Example

curl -X POST "https://api.anypath.dev/v1/chat/completions" \
  -H "Authorization: Bearer sk-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": "Hello!"}
    ],
    "temperature": 0.7,
    "max_tokens": 1000
  }'
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://api.anypath.dev/v1"
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    temperature=0.7,
    max_tokens=1000
)

print(response.choices[0].message.content)
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-your-api-key',
  baseURL: 'https://api.anypath.dev/v1'
});

const response = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello!' }
  ],
  temperature: 0.7,
  max_tokens: 1000
});

console.log(response.choices[0].message.content);
<?php
$ch = curl_init('https://api.anypath.dev/v1/chat/completions');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer sk-your-api-key'
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'gpt-4o-mini',
        'messages' => [
            ['role' => 'system', 'content' => 'You are a helpful assistant.'],
            ['role' => 'user', 'content' => 'Hello!']
        ],
        'temperature' => 0.7,
        'max_tokens' => 1000
    ])
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo $data['choices'][0]['message']['content'];

Multimodal Example

{
  "model": "gemini-2.5-pro",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "Describe this image briefly." },
        { "type": "image_url", "image_url": { "url": "https://example.com/demo.jpg" } }
      ]
    }
  ],
  "max_tokens": 64
}

Response Example

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1706000000,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 9,
    "total_tokens": 29
  }
}

Error Response Example

Insufficient shared-route balance or a provider-side error returns a standard error body:

{
  "error": {
    "message": "API key spending limit exceeded.",
    "type": "spending_limit_exceeded",
    "code": "spending_limit_exceeded"
  }
}
{
  "error": {
    "message": "Upstream provider returned an error.",
    "type": "provider_error",
    "code": "provider_error"
  }
}

At runtime type is always identical to code; both carry the canonical code from the Error Codes registry.

Always check error.code over the HTTP status alone — it distinguishes caller‑fixable errors (invalid model, over‑limit) from transient provider errors.