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

# Count tokens

> Count a Messages request's input tokens without generating.

Takes the same headers and the same body as `/messages` (`model`, `messages`, and optionally `system` and `tools`) and returns the input token count without generating anything: `{"input_tokens": 9}`. It isn't billed, but it counts toward your request rate limit.

Errors use Anthropic's shape, with the request ID in the body and in a `request-id` header; see the [Inference API overview](/docs/reference/inference.md#anthropic).


## OpenAPI

````yaml /openapi/inference.yaml post /messages/count_tokens
openapi: 3.1.0
info:
  title: Pearl Inference API
  version: '1.0'
  description: >-
    The data-plane API your applications call to run models: OpenAI- and
    Anthropic-compatible endpoints behind one base URL.
servers:
  - url: https://inference.pearlresearch.ai/v1
security:
  - inferenceKey: []
paths:
  /messages/count_tokens:
    post:
      summary: Count tokens
      description: Count a Messages request's input tokens without generating.
      operationId: countMessageTokens
      parameters:
        - $ref: '#/components/parameters/AnthropicVersion'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountTokensRequest'
            example:
              model: YOUR_MODEL_ID
              messages:
                - role: user
                  content: Hello
      responses:
        '200':
          description: The input token count.
          headers:
            X-Request-ID:
              $ref: '#/components/headers/X-Request-ID'
            request-id:
              $ref: '#/components/headers/request-id'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TokenCount'
              examples:
                tokenCount:
                  summary: Token count
                  value:
                    input_tokens: 9
        '400':
          $ref: '#/components/responses/AnthropicBadRequest'
        '401':
          $ref: '#/components/responses/AnthropicUnauthorized'
        '402':
          $ref: '#/components/responses/AnthropicOutOfCredits'
        '403':
          $ref: '#/components/responses/AnthropicWrongRegion'
        '404':
          $ref: '#/components/responses/AnthropicModelNotFound'
        '413':
          $ref: '#/components/responses/AnthropicPayloadTooLarge'
        '429':
          $ref: '#/components/responses/AnthropicRateLimited'
        '500':
          $ref: '#/components/responses/AnthropicInternalError'
        '502':
          $ref: '#/components/responses/AnthropicBadGateway'
        '503':
          $ref: '#/components/responses/AnthropicServiceUnavailable'
        '504':
          $ref: '#/components/responses/AnthropicTimeout'
        '529':
          $ref: '#/components/responses/AnthropicNoCapacity'
      security:
        - xApiKey: []
        - inferenceKey: []
      x-codeSamples:
        - lang: bash
          label: cURL
          source: |
            curl https://inference.pearlresearch.ai/v1/messages/count_tokens \
              -H "x-api-key: $PEARL_API_KEY" \
              -H "anthropic-version: 2023-06-01" \
              -H "Content-Type: application/json" \
              -d '{"model":"YOUR_MODEL_ID","messages":[{"role":"user","content":"Hello"}]}'
components:
  parameters:
    AnthropicVersion:
      name: anthropic-version
      in: header
      required: true
      description: One value, for example `2023-06-01`. Missing returns `400`.
      schema:
        type: string
        example: '2023-06-01'
      example: '2023-06-01'
  schemas:
    CountTokensRequest:
      type: object
      required:
        - model
        - messages
      properties:
        model:
          type: string
          description: >-
            A chat model ID from `GET /models`. A model that doesn't serve
            Messages returns `404`.
        messages:
          type: array
          description: >-
            The conversation, alternating `user` and `assistant` turns. Content
            is a string or an array of blocks; replay returned `thinking` and
            `tool_use` blocks, and answer each tool use with a `tool_result`
            block carrying its ID.
          items:
            type: object
            properties:
              role:
                type: string
                enum:
                  - user
                  - assistant
              content:
                oneOf:
                  - title: string
                    type: string
                  - title: array
                    type: array
                    items:
                      type: object
        system:
          description: The system prompt.
          oneOf:
            - title: string
              type: string
            - title: array
              type: array
              items:
                type: object
        tools:
          type: array
          description: Tool definitions. Support varies by model.
          items:
            type: object
    TokenCount:
      type: object
      properties:
        input_tokens:
          type: integer
          description: The input token count.
    AnthropicError:
      type: object
      description: Errors in Anthropic's shape, with the request ID in the body.
      properties:
        type:
          type: string
          enum:
            - error
        error:
          type: object
          properties:
            type:
              type: string
            message:
              type: string
            code:
              type: string
              description: Present only when set, for example `no_capacity`.
        request_id:
          type: string
          description: The request ID, also in the `request-id` header.
  headers:
    X-Request-ID:
      description: >-
        The request ID. Send your own UUID in `X-Request-ID` to use it as the
        ID. Include the ID when you contact support.
      schema:
        type: string
      example: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    request-id:
      description: The request ID, the same as `X-Request-ID`.
      schema:
        type: string
      example: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    Retry-After:
      description: Seconds to wait before retrying.
      schema:
        type: integer
  responses:
    AnthropicBadRequest:
      description: >-
        Invalid request: a missing `anthropic-version` or JSON `Content-Type`,
        malformed JSON, a missing or wrongly typed field, or an unsupported
        thinking setting.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            missingAnthropicVersion:
              summary: Missing anthropic-version
              value:
                type: error
                error:
                  type: invalid_request_error
                  message: missing anthropic-version header
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicUnauthorized:
      description: >-
        Missing, unknown, revoked, or expired API key, a key over its credit
        limit, or a management key.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            unknownKey:
              summary: Unknown API key
              value:
                type: error
                error:
                  type: authentication_error
                  message: unknown API key
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicOutOfCredits:
      description: >-
        Your organization is out of credits. Top up on the
        [Billing](https://platform.pearlresearch.ai/billing) page, then retry.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            outOfCredits:
              summary: Out of credits
              value:
                type: error
                error:
                  type: billing_error
                  message: your organization is out of credits
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicWrongRegion:
      description: >-
        Wrong endpoint for your organization's [inference
        region](/data-residency).
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            wrongRegion:
              summary: Wrong endpoint for the region
              value:
                type: error
                error:
                  type: permission_error
                  message: wrong endpoint for your organization's inference region
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicModelNotFound:
      description: >-
        Unknown model (code `model_not_found`), or one this endpoint doesn't
        serve.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            unknownModel:
              summary: Unknown model
              value:
                type: error
                error:
                  type: not_found_error
                  message: unknown model
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicPayloadTooLarge:
      description: Request body over 20 MiB.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            bodyTooLarge:
              summary: Body over 20 MiB
              value:
                type: error
                error:
                  type: request_too_large
                  message: request body over 20 MiB
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicRateLimited:
      description: >-
        A [rate limit](/plans) for your organization and the model was reached.
        Wait for the `Retry-After` header's seconds.
      headers:
        Retry-After:
          $ref: '#/components/headers/Retry-After'
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            rateLimited:
              summary: Rate limit reached
              value:
                type: error
                error:
                  type: rate_limit_error
                  message: rate limit reached
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicInternalError:
      description: Internal error. Safe to retry with backoff.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            internalError:
              summary: Internal error
              value:
                type: error
                error:
                  type: api_error
                  message: internal error
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicBadGateway:
      description: >-
        The model server failed or returned a malformed response. Retry with
        backoff.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            modelServerFailed:
              summary: Model server failed
              value:
                type: error
                error:
                  type: api_error
                  message: the model server failed
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicServiceUnavailable:
      description: >-
        Temporarily unavailable, for example during a deploy. Retry after the
        `Retry-After` header's seconds.
      headers:
        Retry-After:
          $ref: '#/components/headers/Retry-After'
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            unavailable:
              summary: Temporarily unavailable
              value:
                type: error
                error:
                  type: api_error
                  message: temporarily unavailable
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicTimeout:
      description: The model server timed out. Retry with backoff.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            timedOut:
              summary: Model server timed out
              value:
                type: error
                error:
                  type: timeout_error
                  message: the model server timed out
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
    AnthropicNoCapacity:
      description: >-
        No capacity for the model right now (code `no_capacity`). Retry with
        backoff.
      headers:
        X-Request-ID:
          $ref: '#/components/headers/X-Request-ID'
        request-id:
          $ref: '#/components/headers/request-id'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/AnthropicError'
          examples:
            noCapacity:
              summary: No capacity
              value:
                type: error
                error:
                  type: overloaded_error
                  message: no capacity for the model right now
                  code: no_capacity
                request_id: 6f0b6e0a-6a8f-4c62-9b1e-3d2c1a0f9e87
  securitySchemes:
    inferenceKey:
      type: http
      scheme: bearer
      description: >-
        `Bearer` followed by an inference API key (`sk-prl-infapi-`). Create
        keys on the [API Keys](https://platform.pearlresearch.ai/api-keys) page.
        Management keys (`sk-prl-mgmt-`) cannot call models.
    xApiKey:
      type: apiKey
      in: header
      name: x-api-key
      description: >-
        Your inference API key. Send this or `Authorization`; if you send both,
        they must carry the same key.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.