> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flex.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a chat completion

> Given a list of messages, returns a model-generated response. Supports
streaming (SSE), tool use, and (on some models) vision input.

**Streaming:** set `stream: true` and `stream_options: { include_usage: true }`
to receive token-by-token deltas. The final chunk (the one with `finish_reason`
set) carries the `usage` block; you need it for accurate cost tracking.




## OpenAPI

````yaml /inference-api/openapi.yaml post /v1/chat/completions
openapi: 3.1.0
info:
  title: FlexAI Inference API
  version: 1.0.0
  description: |
    OpenAI-compatible inference API for text, code, reasoning, vision, and
    embedding models hosted by FlexAI. Use any OpenAI SDK by pointing the
    `base_url` at this service.
  contact:
    name: FlexAI Support
    email: support@flex.ai
    url: https://docs.flex.ai/inference-api/overview
servers:
  - url: https://tokens.flex.ai
    description: Production
security:
  - bearerAuth: []
tags:
  - name: Chat
    description: Chat completions (streaming, tool use, vision).
  - name: Completions
    description: Legacy text completions.
  - name: Embeddings
    description: Generate vector embeddings for text.
  - name: Models
    description: Discover available models.
  - name: Usage
    description: Programmatic spend and quota for the calling key.
paths:
  /v1/chat/completions:
    post:
      tags:
        - Chat
      summary: Create a chat completion
      description: >
        Given a list of messages, returns a model-generated response. Supports

        streaming (SSE), tool use, and (on some models) vision input.


        **Streaming:** set `stream: true` and `stream_options: { include_usage:
        true }`

        to receive token-by-token deltas. The final chunk (the one with
        `finish_reason`

        set) carries the `usage` block; you need it for accurate cost tracking.
      operationId: createChatCompletion
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  description: Model id. See `GET /v1/models` for available models.
                  example: Meta-Llama-3.1-8B-Instruct-FP8
                messages:
                  type: array
                  items:
                    type: object
                    required:
                      - role
                    properties:
                      role:
                        type: string
                        enum:
                          - system
                          - user
                          - assistant
                          - tool
                      content:
                        oneOf:
                          - type: string
                          - type: array
                            items:
                              type: object
                      name:
                        type: string
                      tool_calls:
                        type: array
                        items:
                          type: object
                      tool_call_id:
                        type: string
                stream:
                  type: boolean
                  default: false
                stream_options:
                  type: object
                  properties:
                    include_usage:
                      type: boolean
                      description: Required for per-request spend tracking when streaming.
                temperature:
                  type: number
                  minimum: 0
                  maximum: 2
                  default: 1
                top_p:
                  type: number
                  minimum: 0
                  maximum: 1
                max_tokens:
                  type: integer
                  minimum: 1
                tools:
                  type: array
                  items:
                    type: object
                    required:
                      - type
                      - function
                    properties:
                      type:
                        type: string
                        enum:
                          - function
                      function:
                        type: object
                        required:
                          - name
                        properties:
                          name:
                            type: string
                          description:
                            type: string
                          parameters:
                            type: object
                tool_choice:
                  oneOf:
                    - type: string
                      enum:
                        - none
                        - auto
                        - required
                    - type: object
                response_format:
                  type: object
                  properties:
                    type:
                      type: string
                      enum:
                        - text
                        - json_object
                seed:
                  type: integer
                stop:
                  oneOf:
                    - type: string
                    - type: array
                      items:
                        type: string
                user:
                  type: string
      responses:
        '200':
          description: >-
            Successful response. When `stream: true`, body is an SSE stream of
            `text/event-stream` chunks.
          content:
            application/json:
              schema:
                type: object
                required:
                  - id
                  - object
                  - created
                  - model
                  - choices
                properties:
                  id:
                    type: string
                  object:
                    type: string
                    enum:
                      - chat.completion
                  created:
                    type: integer
                  model:
                    type: string
                  choices:
                    type: array
                    items:
                      type: object
                      required:
                        - index
                        - message
                        - finish_reason
                      properties:
                        index:
                          type: integer
                        message:
                          type: object
                          properties:
                            role:
                              type: string
                            content:
                              type:
                                - string
                                - 'null'
                            tool_calls:
                              type: array
                              items:
                                type: object
                        finish_reason:
                          type: string
                          enum:
                            - stop
                            - length
                            - tool_calls
                            - content_filter
                  usage:
                    type: object
                    properties:
                      prompt_tokens:
                        type: integer
                      completion_tokens:
                        type: integer
                      total_tokens:
                        type: integer
                      cache_read_input_tokens:
                        type: integer
                      cache_creation_input_tokens:
                        type: integer
            text/event-stream:
              schema:
                type: string
                description: >-
                  SSE stream. Each event is `data: <json>`. Terminates with
                  `data: [DONE]`.
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '404':
          $ref: '#/components/responses/NotFound'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/ServerError'
components:
  responses:
    BadRequest:
      description: Invalid request — malformed body, unknown model, or schema violation.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    Unauthorized:
      description: Missing or invalid API key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    PaymentRequired:
      description: Key budget exhausted — add credit in the dashboard to continue.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: Resource not found (e.g., unknown model or job id).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    RateLimited:
      description: |
        Per-key RPM exceeded. Response includes `Retry-After` and
        `x-ratelimit-*` headers.
      headers:
        Retry-After:
          schema:
            type: integer
          description: Seconds until the next window.
        x-ratelimit-limit-requests:
          schema:
            type: integer
        x-ratelimit-remaining-requests:
          schema:
            type: integer
        x-ratelimit-reset-requests:
          schema:
            type: integer
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    ServerError:
      description: Upstream or gateway error. Safe to surface `error.message` to users.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          required:
            - message
            - type
          properties:
            message:
              type: string
            type:
              type: string
              example: invalid_request_error
            code:
              type: string
              description: |
                Stable machine-readable code for the failure. Present on
                most error paths; OMITTED on FastAPI default-validation
                bodies. Do not assume always present.
            doc_url:
              type: string
              format: uri
              description: |
                FlexAI extension. Link to the relevant dashboard / docs
                page for the failure mode (e.g. invalid key → /dashboard/keys).
                Present on auth and quota errors.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: sk-xxxx
      description: |
        Virtual API key. Create one from the
        [FlexAI dashboard](https://tokens.flex.ai/dashboard/keys). Pass as
        `Authorization: Bearer sk-xxxx`.

````