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

# Stream Chat

> Streams an agent loop running as a durable [Vercel Workflow](https://vercel.com/docs/workflow) against the session's sandbox. The agent uses sandbox-only tools (`bash`, `read`, `write`, `grep`, `glob`, `todo`, `task`, `ask_user_question`, `skill`, `fetch`) — no MCP or Composio. Requires a sandbox provisioned via [`POST /api/sandbox`](/api-reference/sandbox/create).

**Related endpoints**
- [`POST /api/chat/runs`](/api-reference/chat/runs) — the **headless** counterpart: it provisions its own session + sandbox and runs without a streaming client. Use that for tasks/cron/programmatic runs; use this for **interactive, streaming** turns on an existing chat.
- [`GET /api/chat/{chatId}/stream`](/api-reference/chat/workflow-stream) — reconnect to an in-progress response.
- [`GET /api/chat/runs/{runId}`](/api-reference/chat/runs-status) — check **whether** a headless run finished (status, not content).



## OpenAPI

````yaml POST /api/chat
openapi: 3.1.0
info:
  title: Recoup API - Research
  description: >-
    API documentation for the Recoup platform - an AI agent platform for the
    music industry
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://api.recoupable.dev
security: []
paths:
  /api/chat:
    post:
      summary: Stream sandbox-driven chat (Vercel Workflow)
      description: >-
        Streams an agent loop running as a durable [Vercel
        Workflow](https://vercel.com/docs/workflow) against the session's
        sandbox. The agent uses sandbox-only tools (`bash`, `read`, `write`,
        `grep`, `glob`, `todo`, `task`, `ask_user_question`, `skill`, `fetch`) —
        no MCP or Composio. Requires a sandbox provisioned via [`POST
        /api/sandbox`](/api-reference/sandbox/create).


        **Related endpoints**

        - [`POST /api/chat/runs`](/api-reference/chat/runs) — the **headless**
        counterpart: it provisions its own session + sandbox and runs without a
        streaming client. Use that for tasks/cron/programmatic runs; use this
        for **interactive, streaming** turns on an existing chat.

        - [`GET /api/chat/{chatId}/stream`](/api-reference/chat/workflow-stream)
        — reconnect to an in-progress response.

        - [`GET /api/chat/runs/{runId}`](/api-reference/chat/runs-status) —
        check **whether** a headless run finished (status, not content).
      requestBody:
        description: Chat workflow request
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatWorkflowRequest'
      responses:
        '200':
          description: >-
            Server-Sent Events stream of UI message parts compatible with the
            Vercel AI SDK `createUIMessageStreamResponse`. The
            `x-workflow-run-id` response header carries the durable workflow run
            ID for resume/stop operations.
          headers:
            x-workflow-run-id:
              description: >-
                Durable workflow run ID. Persist alongside the chat so a later
                `GET /api/chat/{chatId}/stream` request can resume this run
                after a disconnect.
              schema:
                type: string
          content:
            text/event-stream:
              schema:
                type: string
                description: Server-Sent Events stream containing UI message parts
        '400':
          description: >-
            Bad request — missing required fields, invalid body, or sandbox not
            initialized for the session.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatStreamErrorResponse'
        '401':
          description: Missing or invalid API key.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatStreamErrorResponse'
        '403':
          description: >-
            The API key's account does not own the referenced `sessionId` or
            `chatId`.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatStreamErrorResponse'
        '404':
          description: Session or chat not found.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatStreamErrorResponse'
        '409':
          description: >-
            A response is already streaming for this chat. Reconnect to it via
            `GET /api/chat/{chatId}/stream` instead of POSTing again.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatStreamErrorResponse'
      security:
        - apiKeyAuth: []
components:
  schemas:
    ChatWorkflowRequest:
      type: object
      description: >-
        Request body for the sandbox-driven, Workflow-backed chat endpoint.
        Requires an existing session, chat, and provisioned sandbox. The Bearer
        key from the `Authorization` header is forwarded into the sandbox for
        skill-based callbacks to the Recoup API — no separate access-token field
        needed.
      required:
        - messages
        - chatId
        - sessionId
      properties:
        messages:
          type: array
          items:
            $ref: '#/components/schemas/UIMessage'
          description: >-
            Conversation history as an array of `UIMessage` objects, matching
            the [Vercel AI SDK](https://sdk.vercel.ai/) `UIMessage` shape. The
            last entry is typically the new user turn — the agent loop reads the
            entire array and persists the latest user message before the
            workflow starts.
        chatId:
          type: string
          format: uuid
          description: >-
            UUID of an existing chat under `sessionId`. The chat's
            `active_stream_id` column is used for compare-and-set race control
            across concurrent requests.
        sessionId:
          type: string
          format: uuid
          description: >-
            UUID of the session that owns both the chat and the sandbox. The
            session must have a non-null `sandbox_state` — call [POST
            /api/sandbox](/api-reference/sandbox/create) first if not.
        context:
          type: object
          description: Optional per-prompt context overrides forwarded into the agent loop.
          properties:
            contextLimit:
              type: integer
              description: >-
                Upper bound on the number of tokens / messages of conversation
                history the agent should consider for this prompt. Overrides the
                model's default for this call only.
    ChatStreamErrorResponse:
      type: object
      required:
        - status
        - message
      properties:
        status:
          type: string
          enum:
            - error
          description: Status of the request
        message:
          type: string
          description: Error message describing what went wrong
    UIMessage:
      type: object
      description: >-
        A message in the chat conversation. See
        https://ai-sdk.dev/docs/reference/ai-sdk-core/ui-message for details.
      properties:
        id:
          type: string
          description: Unique identifier for the message
        role:
          type: string
          enum:
            - user
            - assistant
            - system
          description: The role of the message sender
        content:
          type: string
          description: The text content of the message
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: Your Recoup API key. [Learn more](/quickstart#api-keys).

````