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

# Count input tokens for an Anthropic-compatible request

> Does not generate a completion or consume AI credits.



## OpenAPI

````yaml /openapi/v1.json post /messages/count_tokens
openapi: 3.1.0
info:
  title: HASP AI API
  version: '2026-07-12'
  description: >-
    The HASP Public AI API is the regulated-AI substrate for healthcare and
    other

    regulated industries — identity, policy, audit, compliance, and PHI handling

    for AI inference, exposed via two surfaces:


    - **Native (`/v1/ai/*`)**: HASP-native chat with full event taxonomy, run
    lifecycle,
      and PHI metadata.
    - **Anthropic-compat (`/v1/messages`)**: Drop-in replacement for
    `@anthropic-ai/sdk` —
      change only `baseURL`. All Gateway compliance checks (BAA, credits, PHI policy) apply.

    All requests require an API key (`Authorization: Bearer
    hasp_api_live_<key>`). See

    [Authentication](https://docs.usehasp.com/ai-api/authentication) for key
    management.
servers:
  - url: https://api.usehasp.com/v1
    description: Production
security:
  - http: []
paths:
  /messages/count_tokens:
    post:
      tags:
        - CountTokens
      summary: Count input tokens for an Anthropic-compatible request
      description: Does not generate a completion or consume AI credits.
      operationId: v1.messages.count_tokens
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountTokensRequest'
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                anyOf:
                  - type: object
                    properties:
                      input_tokens:
                        type: integer
                    required:
                      - input_tokens
                  - type: string
        '401':
          description: >-
            Anthropic-shaped error — bearer token missing, malformed, or
            revoked.
          content:
            application/json:
              schema:
                type: string
                examples:
                  - type: error
                    error:
                      type: authentication_error
                      message: Bearer token is missing, malformed, or revoked.
                      hasp_code: INVALID_API_KEY
                      hasp_request_id: 01JQREQ7XZQK5N6PZ1VVXHYB8T
        '403':
          $ref: '#/components/responses/AuthorizationException'
        '422':
          $ref: '#/components/responses/ValidationException'
        '429':
          description: Anthropic-shaped error — rate limit exceeded.
          content:
            application/json:
              schema:
                type: string
                examples:
                  - type: error
                    error:
                      type: rate_limit_error
                      message: Rate limit exceeded.
                      hasp_code: RATE_LIMITED
                      hasp_request_id: 01JQREQ7XZQK5N6PZ1VVXHYB8T
components:
  schemas:
    CountTokensRequest:
      type: object
      description: >-
        Validates POST /v1/messages/count_tokens (Anthropic-compat) per
        ADR-YGE00M.


        Accepts the same request body shape as /v1/messages (minus stream and

        max_tokens, which don't affect token counting) so a client can count

        tokens for the exact same payload it's about to send for a real call.
      properties:
        model:
          type: string
          maxLength: 100
        tool_choice:
          type:
            - string
            - 'null'
        stop_sequences:
          type:
            - array
            - 'null'
          items:
            type: string
        messages:
          type: array
          items:
            type: object
            properties:
              role:
                type: string
                enum:
                  - user
                  - assistant
              content:
                type: string
                description: >-
                  Restricted to string|array (not int/bool/etc.) so a malformed

                  request 422s instead of 500ing when Message::$content
                  type-checks it.
            required:
              - role
              - content
          minItems: 1
        system:
          type: array
          items:
            type: object
            properties:
              type:
                type:
                  - string
                  - 'null'
                enum:
                  - text
              text:
                type:
                  - string
                  - 'null'
              cache_control:
                type:
                  - object
                  - 'null'
                properties:
                  type:
                    type:
                      - string
                      - 'null'
                    enum:
                      - ephemeral
        tools:
          type:
            - array
            - 'null'
          items:
            type: object
            properties:
              name:
                type: string
              description:
                type:
                  - string
                  - 'null'
              input_schema:
                type:
                  - object
                  - 'null'
                properties:
                  type:
                    type:
                      - string
                      - 'null'
                    description: >-
                      Nested key so the generated OpenAPI spec renders
                      input_schema as

                      a JSON Schema object rather than a bare array.
              cache_control:
                type:
                  - object
                  - 'null'
                description: |-
                  Same cache_control support as MessagesRequest — count_tokens
                  accepts the identical tool-definition shape.
                properties:
                  type:
                    type:
                      - string
                      - 'null'
                    enum:
                      - ephemeral
      required:
        - model
        - messages
      title: CountTokensRequest
  responses:
    AuthorizationException:
      description: Authorization error
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
                type: string
                description: Error overview.
            required:
              - message
    ValidationException:
      description: Validation error
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
                type: string
                description: Errors overview.
              errors:
                type: object
                description: A detailed description of each field that failed validation.
                additionalProperties:
                  type: array
                  items:
                    type: string
            required:
              - message
              - errors
  securitySchemes:
    http:
      type: http
      scheme: bearer

````