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

# Create chat completion

> Runs a chat completion against a workspace agent and persists the conversation. Creates a new chat when `chatId` is omitted. Returns JSON by default, or a Server-Sent Events stream of AI SDK UI-message chunks when `stream` is `true`. Agents with multi-agent orchestration enabled require `stream: false`.




## OpenAPI

````yaml POST /chat
openapi: 3.1.0
info:
  title: TeamAI API
  version: '1.0'
  description: >
    REST API for TeamAI. Run agents and chat completions, and manage projects,
    files, and skills programmatically.
servers:
  - url: https://app.teamai.com/api/v1
security:
  - apiKey: []
tags:
  - name: Chat
  - name: Agents
  - name: Projects
  - name: Skills
paths:
  /chat:
    post:
      tags:
        - Chat
      summary: Create chat completion
      description: >
        Runs a chat completion against a workspace agent and persists the
        conversation. Creates a new chat when `chatId` is omitted. Returns JSON
        by default, or a Server-Sent Events stream of AI SDK UI-message chunks
        when `stream` is `true`. Agents with multi-agent orchestration enabled
        require `stream: false`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - messages
                - agentId
              properties:
                messages:
                  type: array
                  description: >
                    Conversation messages in AI SDK UI-message format. Must
                    contain at least one message with role `user`.
                  items:
                    type: object
                    properties:
                      id:
                        type: string
                        description: Unique message identifier.
                      role:
                        type: string
                        enum:
                          - user
                          - assistant
                          - system
                      parts:
                        type: array
                        description: >
                          Message content parts. Text parts use `{"type":
                          "text", "text": "..."}`. File parts use `{"type":
                          "file", "url": "...", "mediaType": "...", "filename":
                          "..."}`.
                        items:
                          type: object
                agentId:
                  type: string
                  description: Agent to run. Must belong to the API key's workspace.
                chatId:
                  type: string
                  description: >
                    Existing chat to continue. Omit to create a new chat titled
                    from the first user message. Providing an unknown ID returns
                    404.
                projectId:
                  type: string
                  description: >
                    Project whose instructions, memory, and files are injected
                    into the conversation context.
                stream:
                  type: boolean
                  default: false
                  description: Return a Server-Sent Events stream instead of JSON.
                temperature:
                  type: number
                  default: 0.7
                model:
                  type: string
                  description: >
                    Model identifier. Falls back to the workspace default model
                    when the requested model is not enabled for the workspace.
                variables:
                  type: object
                  additionalProperties: true
                  description: Prompt template variables.
                systemVariables:
                  type: object
                  properties:
                    user:
                      type: object
                      description: Acting user identity for tools and prompts.
                      properties:
                        id:
                          type: string
                        name:
                          type: string
                        email:
                          type: string
      responses:
        '200':
          description: >
            Completion result. Note this endpoint returns the payload directly,
            without the `data` envelope used by other endpoints. When `stream:
            true`, the response is `text/event-stream` instead.
          content:
            application/json:
              schema:
                type: object
                properties:
                  text:
                    type: string
                    description: Assistant reply text.
                  messageId:
                    type: string
                    description: Persisted assistant message ID.
                  chatId:
                    type: string
                  warnings:
                    type: array
                    description: Present only when tool setup errors occurred.
                    items:
                      type: object
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/NotAuthenticated'
        '402':
          description: Workspace usage limit or chat spend cap exceeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  responses:
    BadRequest:
      description: Request body or parameters failed validation.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotAuthenticated:
      description: >
        API key missing or invalid (`not_authenticated`), or valid but not
        permitted to access this resource (`unauthorized`).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: Requested resource does not exist or is not visible to this key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      properties:
        code:
          type: string
          description: Machine-readable error code.
          example: bad_request
        message:
          type: string
          description: Human-readable error description.
        details:
          type: object
          additionalProperties: true
          description: Additional error context.
  securitySchemes:
    apiKey:
      type: apiKey
      in: header
      name: x-teamai-api-key
      description: >
        Secret API key created in Workspace Settings → API Keys (workspace keys)
        or Organization Admin → API Keys (organization keys).

````