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

# Query a knowledge base

> Runs semantic retrieval against a knowledge base and returns the most relevant chunks. This is the same retrieval an agent performs — use it to test relevance or to query from a workflow. Requires a token with read access.



## OpenAPI

````yaml /openapi-knowledge-base.json post /knowledge-base/datasets/{dataset_id}/query
openapi: 3.0.3
info:
  title: AnyReach Knowledge Base API
  version: 1.0.0
  description: Manage knowledge bases, their sources, and run retrieval queries.
servers:
  - url: https://api.anyreach.ai
    description: Production
security:
  - bearerAuth: []
tags:
  - name: Knowledge bases
    description: >-
      A knowledge base (a `dataset` in the API) is a collection of embedded
      content an agent can retrieve from.
  - name: Knowledge base sources
    description: Attach and detach sources to a knowledge base.
  - name: Sources
    description: >-
      A source is a file or URL whose content is ingested, chunked, and
      embedded.
paths:
  /knowledge-base/datasets/{dataset_id}/query:
    post:
      tags:
        - Knowledge bases
      summary: Query a knowledge base
      description: >-
        Runs semantic retrieval against a knowledge base and returns the most
        relevant chunks. This is the same retrieval an agent performs — use it
        to test relevance or to query from a workflow. Requires a token with
        read access.
      operationId: queryKnowledgeBase
      parameters:
        - name: dataset_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: The knowledge base ID.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QueryRequest'
            example:
              query: what is the return window?
              results_count: 5
      responses:
        '200':
          description: Matching chunks, most relevant first.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/QueryResponse'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  schemas:
    QueryRequest:
      type: object
      required:
        - query
      properties:
        query:
          type: string
          description: The natural-language query to embed and search with.
        results_count:
          type: integer
          default: 5
          description: Maximum number of chunks to return.
    QueryResponse:
      type: object
      properties:
        results:
          type: array
          items:
            $ref: '#/components/schemas/QueryResult'
    QueryResult:
      type: object
      properties:
        content:
          type: string
          description: The matching chunk text.
        score:
          type: number
          format: float
          description: Similarity score (higher is more relevant).
        source_id:
          type: string
          description: The source the chunk came from.
        dataset_id:
          type: string
          description: The knowledge base searched.
    Error:
      type: object
      properties:
        detail:
          type: string
          example: Dataset not found
  responses:
    Forbidden:
      description: The token's role doesn't grant access to this action.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
    NotFound:
      description: The resource does not exist.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Personal access token. See Authentication.

````