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

# Count tokens

> Counts the tokens a Messages request would use, without sending it to the model.



## OpenAPI

````yaml /api-reference/openapi.json post /anthropic/v1/messages/count_tokens
openapi: 3.1.0
info:
  title: Altrum AI Gateway API
  version: 1.0.0
  description: >-
    The public API of the Altrum AI Gateway. It accepts the OpenAI, Anthropic
    and Gemini request formats, so an existing client needs only a new base URL
    and a virtual key.
servers:
  - url: https://api.altrum.ai
security:
  - bearer: []
  - apiKey: []
  - googleApiKey: []
  - altrumKey: []
tags:
  - name: OpenAI
  - name: Anthropic
  - name: Gemini
  - name: MCP
  - name: Health
paths:
  /anthropic/v1/messages/count_tokens:
    post:
      tags:
        - Anthropic
      summary: Count tokens
      description: >-
        Counts the tokens a Messages request would use, without sending it to
        the model.
      operationId: countTokens
      parameters:
        - $ref: '#/components/parameters/RequestIdIn'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              description: An Anthropic token count request.
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  description: >-
                    The provider and the model, such as `openai/gpt-4o-mini`. A
                    name without a provider is matched to one your organization
                    can serve.
                  example: openai/gpt-4o-mini
                messages:
                  type: array
                  items:
                    type: object
                    additionalProperties: true
              additionalProperties: true
            example:
              model: anthropic/claude-sonnet-4-5
              messages:
                - role: user
                  content: Say hello.
      responses:
        '200':
          description: The count.
          headers:
            x-request-id:
              $ref: '#/components/headers/RequestId'
          content:
            application/json:
              schema:
                type: object
                properties:
                  input_tokens:
                    type: integer
                example:
                  input_tokens: 10
              example:
                input_tokens: 10
        '400':
          description: >-
            The request was refused: a guardrail stopped it, or the body or a
            header is not valid.
          headers:
            x-request-id:
              $ref: '#/components/headers/RequestId'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
        '401':
          description: No virtual key was sent, or the key does not exist or was revoked.
          headers:
            x-request-id:
              $ref: '#/components/headers/RequestId'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
        '403':
          description: >-
            The key, AI system, organization, model, provider or tool is not
            allowed.
          headers:
            x-request-id:
              $ref: '#/components/headers/RequestId'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
        '429':
          description: A rate limit that covers this key was reached.
          headers:
            x-request-id:
              $ref: '#/components/headers/RequestId'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
components:
  parameters:
    RequestIdIn:
      name: x-request-id
      in: header
      required: false
      description: >-
        Your own reference for the request. Altrum uses it instead of making
        one, and returns it.
      schema:
        type: string
  headers:
    RequestId:
      description: The request's reference. Quote it to support.
      schema:
        type: string
  schemas:
    AnthropicError:
      type: object
      description: An error on the Anthropic routes.
      properties:
        type:
          type: string
          const: error
        error:
          type: object
          properties:
            type:
              type: string
              description: A refusal code or a stable code.
            message:
              type: string
        request_id:
          type: string
      example:
        type: error
        error:
          type: model_blocked
          message: Model 'claude-opus-4' is not allowed
        request_id: 4f9c...
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      description: >-
        Your virtual key, `sk-alt-...`. What every OpenAI and Anthropic SDK
        sends.
    apiKey:
      type: apiKey
      in: header
      name: x-api-key
      description: Your virtual key, as Anthropic clients send it.
    googleApiKey:
      type: apiKey
      in: header
      name: x-goog-api-key
      description: Your virtual key, as Gemini clients send it.
    altrumKey:
      type: apiKey
      in: header
      name: x-alt-vk
      description: >-
        Your virtual key. Needed when `Authorization` carries your own provider
        key (`x-alt-direct-key`).

````

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