---
title: "Create chat completion"
method: POST
path: "/chat/completions"
tags: ["Chat"]
---

# Create chat completion

`POST /chat/completions`

Generate conversational AI responses using the OpenAI-compatible chat completions API

## Request body

- ChatCompletionRequest
  - `model` string, required — Model ID (e.g., gpt-4, claude-3.5-sonnet)
  - `messages` ChatMessage[], required — Array of message objects
    - `role` 'system' | 'user' | 'assistant' | 'tool', required — Message role
    - `content` union, required — Message content (text or multi-part)
      - string
      - object[]
    - `name` string — Participant name
    - `tool_calls` ToolCall[] — Tool calls made by assistant
      - `id` string — Tool call ID
      - `type` 'function'
      - `function` object
        - `name` string
        - `arguments` string — JSON string of arguments
    - `tool_call_id` string — ID of tool call (for tool role)
  - `temperature` number — Sampling temperature (0.0 - 2.0)
  - `max_tokens` integer — Maximum tokens to generate
  - `stream` boolean — Enable streaming responses
  - `tools` Tool[] — Available functions/tools
    - `type` 'function', required — Tool type
    - `function` object, required
      - `name` string, required — Function name
      - `description` string — Function description
      - `parameters` object, required — JSON Schema for parameters
  - `tool_choice` union — Controls tool calling behavior
    - 'auto' | 'required' | 'none'
    - object
  - `top_p` number — Nucleus sampling parameter
  - `frequency_penalty` number — Penalize frequent tokens
  - `presence_penalty` number — Penalize repeated tokens
  - `n` integer — Number of completions to generate
  - `stop` union — Stop sequences
    - string
    - string[]
  - `user` string — Unique user identifier

## Response `200`

Successful response

- ChatCompletionResponse
  - `id` string
  - `object` 'chat.completion'
  - `created` integer — Unix timestamp
  - `model` string
  - `choices` object[]
    - `index` integer
    - `message` ChatMessage
      - `role` 'system' | 'user' | 'assistant' | 'tool', required — Message role
      - `content` union, required — Message content (text or multi-part)
        - string
        - object[]
      - `name` string — Participant name
      - `tool_calls` ToolCall[] — Tool calls made by assistant
        - `id` string — Tool call ID
        - `type` 'function'
        - `function` object
          - `name` string
          - `arguments` string — JSON string of arguments
      - `tool_call_id` string — ID of tool call (for tool role)
    - `finish_reason` 'stop' | 'length' | 'tool_calls' | 'content_filter'
  - `usage` Usage
    - `prompt_tokens` integer
    - `completion_tokens` integer
    - `total_tokens` integer

## Other responses

- `400` — Bad request - Invalid parameters
- `401` — Unauthorized - Invalid or missing API key
- `429` — Rate limit exceeded
- `500` — Internal server error

---

[API](https://skmtc.net/megallm/apis/megallm-api.md) · [All operations](https://skmtc.net/megallm/apis/megallm-api/llms.txt) · [OpenAPI document](https://skmtc-service-staging.skmtc.workers.dev/v1/apis/megallm/megallm-api/revisions/a365a5b98d60/schema)
