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

# Ask a question (RAG)

> Retrieves relevant knowledge chunks from the collection and uses an LLM to generate a grounded answer. Supports both synchronous JSON responses and streaming via SSE (`stream: true`). Token usage is tracked and counted against the team's quota.



## OpenAPI

````yaml https://staging.aigmented.io/api/v1/openapi.json post /api/v1/collections/{id}/ask
openapi: 3.1.0
info:
  title: Aigmented API
  version: 1.0.0
  description: >-
    REST API for Aigmented — manage knowledge collections, upload files, search
    knowledge cards, and ask questions with LLM-powered answers.
  contact:
    name: Aigmented Support
    url: https://aigmented.com
servers:
  - url: https://aigmented.io
    description: Production
security:
  - BearerAuth: []
tags:
  - name: Collections
    description: Create and manage knowledge collections
  - name: Files
    description: Upload and manage source documents
  - name: Search
    description: Semantic search over knowledge cards in a collection
  - name: Ask
    description: LLM-powered question answering over a collection
paths:
  /api/v1/collections/{id}/ask:
    post:
      tags:
        - Ask
      summary: Ask a question (RAG)
      description: >-
        Retrieves relevant knowledge chunks from the collection and uses an LLM
        to generate a grounded answer. Supports both synchronous JSON responses
        and streaming via SSE (`stream: true`). Token usage is tracked and
        counted against the team's quota.
      operationId: askCollection
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
          description: Collection ID
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AskRequest'
            example:
              question: What does the agreement say about liability?
              model: gpt-4o-mini
              mode: fast
              stream: false
              top_k: 10
              current_only: true
      responses:
        '200':
          description: >-
            Answer generated successfully. When `stream=false`, returns a JSON
            body. When `stream=true`, returns an SSE stream (`Content-Type:
            text/event-stream`).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AskResponse'
            text/event-stream:
              schema:
                type: string
                description: >-
                  Newline-delimited SSE events. Each event is a JSON object with
                  `type` and `data` fields. See the `AskStreamEvent` schema for
                  details.
        '400':
          description: Missing or invalid parameters
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '404':
          description: Collection not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '429':
          description: Team token limit exceeded
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TokenLimitError'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    AskRequest:
      type: object
      required:
        - question
      properties:
        question:
          type: string
          description: The question to answer
          example: What does the document say about data retention?
        model:
          type: string
          default: gpt-4o-mini
          description: LLM model identifier to use for answering
          example: gpt-4o
        mode:
          type: string
          enum:
            - fast
            - full
          default: fast
          description: >-
            Answer mode. `fast` uses fewer retrieved chunks for a quicker
            response; `full` retrieves more context for a thorough answer.
        stream:
          type: boolean
          default: false
          description: >-
            If true, the response is streamed as Server-Sent Events (SSE). Each
            event has a `type` field: `delta` (text chunk), `done` (final
            metadata), or `error`.
        top_k:
          type: integer
          default: 10
          minimum: 1
          maximum: 50
          description: Number of knowledge chunks to retrieve before generating the answer
        current_only:
          type: boolean
          default: false
          description: Restrict retrieval to the most current document versions only
        chat_history:
          type: array
          items:
            $ref: '#/components/schemas/ChatMessage'
          description: Optional prior conversation turns for multi-turn context
          example:
            - role: user
              content: Summarise the document.
            - role: assistant
              content: The document covers...
        filters:
          type: object
          description: Optional metadata filters to scope retrieval
          additionalProperties: true
    AskResponse:
      type: object
      properties:
        answer:
          type: string
          description: The LLM-generated answer
        sources:
          type: array
          items:
            $ref: '#/components/schemas/AskSource'
          description: Knowledge chunks used to generate the answer
        model:
          type: string
          description: Model used to generate the answer
        tokens_used:
          type: object
          description: Token usage breakdown
          properties:
            llm_prompt:
              type: integer
            llm_completion:
              type: integer
            embedding:
              type: integer
            model_id:
              type: string
    Error:
      type: object
      properties:
        error:
          type: string
          description: Human-readable error message
      required:
        - error
    TokenLimitError:
      type: object
      properties:
        error:
          type: string
          example: Token limit exceeded
        remaining:
          type: integer
          description: Remaining included tokens
        reason:
          type: string
          description: Reason for the limit
      required:
        - error
    ChatMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - user
            - assistant
          description: Message author role
        content:
          type: string
          description: Message text content
    AskSource:
      type: object
      properties:
        document_id:
          type: string
        file_name:
          type: string
        page:
          type: integer
        chunk_index:
          type: integer
        score:
          type: number
          format: float
        content_preview:
          type: string
          description: First ~200 characters of the source chunk
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API key (sk-...)
      description: >-
        Pass your API key as a Bearer token. Example: `Authorization: Bearer
        sk-xxxxxxxxxxxx`

````