# Ask a model through one of the project's routes

`POST /models/v1/chat/completions` · Rivier · models

Ask a model through one of the project's routes. The body and the answer follow the OpenAI chat completions format; `model` is a route name from the model list, never a model's own name. With `stream: true` the answer arrives as server-sent events. Uses the project's token allowance. The answer carries `cost.usd`, what the call cost; a stream carries it in a last piece when `stream_options.include_usage` is set. `purpose` is a short tag of your own that the day's usage is counted under.

- Scope: models:write
- Sdk: models.completion.create
- Cli: models completion create

## Request body

`application/json`

```json
{
  "type": "object",
  "properties": {
    "model": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "messages": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "role": {
            "type": "string",
            "minLength": 1,
            "maxLength": 20
          }
        },
        "required": [
          "role"
        ],
        "additionalProperties": true
      },
      "minItems": 1,
      "maxItems": 2000
    },
    "stream": {
      "type": [
        "boolean",
        "null"
      ]
    },
    "n": {
      "anyOf": [
        {
          "type": "integer"
        },
        {
          "type": "null"
        }
      ]
    },
    "max_tokens": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 65536
        },
        {
          "type": "null"
        }
      ]
    },
    "max_completion_tokens": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 65536
        },
        {
          "type": "null"
        }
      ]
    },
    "stream_options": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "include_usage": {
              "type": [
                "boolean",
                "null"
              ]
            }
          },
          "additionalProperties": true
        },
        {
          "type": "null"
        }
      ]
    },
    "purpose": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9._-]{0,39}$"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "model",
    "messages"
  ],
  "additionalProperties": true
}
```

## Responses

- `200`: Success
- `400`: The request was malformed
- `401`: No valid credential
- `403`: The credential lacks the scope, origin or address
- `429`: Over the key's rate limit; see Retry-After

## Example

```sh
curl -X POST 'https://api.rivier.ai/models/v1/chat/completions' \
  -H 'Authorization: Bearer $KEY' \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "string",
  "messages": [
    {
      "role": "string"
    }
  ]
}'
```
