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

# Count message tokens



## OpenAPI

````yaml https://raw.githubusercontent.com/PaloAltoNetworks/openapi/refs/heads/main/openapi.yaml post /messages/count_tokens
openapi: 3.0.0
info:
  title: Prisma AIRS AI Gateway API
  version: 3.0.0
  description: ''
servers:
  - url: https://aigw.portkey.ai/v1
    description: Managed
  - url: https://self-hosted-gateway-url.example.com/v1
    description: Self-hosted -- replace with your own gateway host
security:
  - Authorization: []
tags:
  - name: Assistants
    description: ''
  - name: Audio
    description: ''
  - name: Batch
    description: ''
  - name: Chat
    description: ''
  - name: Completions
    description: ''
  - name: Decisions
    description: ''
  - name: Embeddings
    description: ''
  - name: Files
    description: ''
  - name: Fine-Tuning
    description: ''
  - name: Images
    description: ''
  - name: Messages
    description: ''
  - name: Model Pricing
    description: ''
  - name: Models
    description: ''
  - name: Moderations
    description: ''
  - name: OCR
    description: ''
  - name: Realtime
    description: ''
  - name: Rerank
    description: ''
  - name: Responses
    description: ''
  - name: Vector Stores
    description: ''
  - name: Configs
    description: ''
  - name: Guardrails
    description: ''
  - name: Integrations
    description: ''
  - name: Integrations > Models
    description: ''
  - name: Integrations > Workspaces
    description: ''
  - name: Providers
    description: ''
  - name: Secret References
    description: ''
  - name: MCP Integrations
    description: ''
  - name: MCP Integrations > Capabilities
    description: ''
  - name: MCP Integrations > Metadata
    description: ''
  - name: MCP Integrations > Workspaces
    description: ''
  - name: MCP Servers
    description: ''
  - name: MCP Servers > Capabilities
    description: ''
  - name: MCP Servers > Connections
    description: ''
  - name: MCP Servers > User Access
    description: ''
  - name: API Keys
    description: ''
  - name: Access Policies
    description: ''
  - name: Deployments
    description: ''
  - name: Egress Allowlist
    description: ''
  - name: Org Guardrails
    description: ''
  - name: Rate Limit Policies
    description: ''
  - name: Usage Limit Policies
    description: ''
  - name: Workspaces
    description: ''
  - name: Analytics > Graphs
    description: ''
  - name: Analytics > Groups
    description: ''
  - name: Analytics > Summary
    description: ''
  - name: Feedback
    description: ''
  - name: Logs
    description: ''
paths:
  /messages/count_tokens:
    post:
      tags:
        - Messages
      operationId: countMessageTokens
      parameters:
        - $ref: '#/components/parameters/PortkeyTraceId'
        - $ref: '#/components/parameters/PortkeySpanId'
        - $ref: '#/components/parameters/PortkeyParentSpanId'
        - $ref: '#/components/parameters/PortkeySpanName'
        - $ref: '#/components/parameters/PortkeyMetadata'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountMessageTokensRequest'
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CountMessageTokensResponse'
components:
  parameters:
    PortkeyTraceId:
      in: header
      name: x-portkey-trace-id
      schema:
        type: string
      required: false
    PortkeySpanId:
      in: header
      name: x-portkey-span-id
      schema:
        type: string
      required: false
    PortkeyParentSpanId:
      in: header
      name: x-portkey-parent-span-id
      schema:
        type: string
      required: false
    PortkeySpanName:
      in: header
      name: x-portkey-span-name
      schema:
        type: string
      required: false
    PortkeyMetadata:
      in: header
      name: x-portkey-metadata
      schema:
        type: object
      required: false
  schemas:
    CountMessageTokensRequest:
      type: object
      required:
        - model
        - messages
      description: ''
      properties:
        model:
          description: ''
          type: string
        messages:
          type: array
          items:
            $ref: '#/components/schemas/AnthropicMessage'
        system:
          $ref: '#/components/schemas/AnthropicSystemContent'
        tools:
          type: array
          items:
            $ref: '#/components/schemas/AnthropicTool'
        tool_choice:
          type: object
          properties:
            type:
              type: string
              enum:
                - auto
                - any
                - tool
                - none
            name:
              type: string
        thinking:
          $ref: '#/components/schemas/AnthropicThinkingConfig'
    CountMessageTokensResponse:
      type: object
      properties:
        input_tokens:
          type: integer
          description: ''
    AnthropicMessage:
      type: object
      required:
        - role
        - content
      properties:
        role:
          type: string
          enum:
            - user
            - assistant
        content:
          oneOf:
            - type: string
            - type: array
              items:
                $ref: '#/components/schemas/AnthropicContentBlock'
    AnthropicSystemContent:
      oneOf:
        - description: ''
          type: string
        - type: array
          items:
            type: object
            required:
              - type
              - text
            properties:
              type:
                type: string
                enum:
                  - text
              text:
                type: string
              cache_control:
                type: object
                properties:
                  type:
                    type: string
                    enum:
                      - ephemeral
    AnthropicTool:
      type: object
      required:
        - name
        - input_schema
      properties:
        name:
          type: string
        description:
          type: string
        input_schema:
          type: object
          description: ''
        cache_control:
          type: object
          properties:
            type:
              type: string
              enum:
                - ephemeral
    AnthropicThinkingConfig:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - enabled
            - disabled
            - adaptive
        budget_tokens:
          type: integer
          description: ''
    AnthropicContentBlock:
      oneOf:
        - $ref: '#/components/schemas/AnthropicTextContent'
        - $ref: '#/components/schemas/AnthropicImageContent'
        - $ref: '#/components/schemas/AnthropicDocumentContent'
        - $ref: '#/components/schemas/AnthropicToolUseContent'
        - $ref: '#/components/schemas/AnthropicToolResultContent'
        - $ref: '#/components/schemas/AnthropicThinkingContent'
        - $ref: '#/components/schemas/AnthropicRedactedThinkingContent'
    AnthropicTextContent:
      type: object
      required:
        - type
        - text
      properties:
        type:
          type: string
          enum:
            - text
        text:
          type: string
    AnthropicImageContent:
      type: object
      required:
        - type
        - source
      properties:
        type:
          type: string
          enum:
            - image
        source:
          $ref: '#/components/schemas/AnthropicImageSource'
    AnthropicDocumentContent:
      type: object
      required:
        - type
        - source
      properties:
        type:
          type: string
          enum:
            - document
        source:
          $ref: '#/components/schemas/AnthropicDocumentSource'
        title:
          type: string
        context:
          type: string
        citations:
          type: object
          properties:
            enabled:
              type: boolean
    AnthropicToolUseContent:
      type: object
      required:
        - type
        - id
        - name
        - input
      properties:
        type:
          type: string
          enum:
            - tool_use
        id:
          type: string
        name:
          type: string
        input:
          type: object
    AnthropicToolResultContent:
      type: object
      required:
        - type
        - tool_use_id
      properties:
        type:
          type: string
          enum:
            - tool_result
        tool_use_id:
          type: string
        content:
          oneOf:
            - type: string
            - type: array
              items:
                oneOf:
                  - $ref: '#/components/schemas/AnthropicTextContent'
                  - $ref: '#/components/schemas/AnthropicImageContent'
        is_error:
          type: boolean
    AnthropicThinkingContent:
      type: object
      required:
        - type
        - thinking
        - signature
      properties:
        type:
          type: string
          enum:
            - thinking
        thinking:
          type: string
        signature:
          type: string
    AnthropicRedactedThinkingContent:
      type: object
      required:
        - type
        - data
      properties:
        type:
          type: string
          enum:
            - redacted_thinking
        data:
          type: string
    AnthropicImageSource:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - base64
            - url
        media_type:
          type: string
          enum:
            - image/jpeg
            - image/png
            - image/gif
            - image/webp
          description: ''
        data:
          description: ''
          type: string
        url:
          description: ''
          type: string
    AnthropicDocumentSource:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          enum:
            - base64
            - url
            - text
            - content
        media_type:
          type: string
          enum:
            - application/pdf
            - text/plain
          description: ''
        data:
          description: ''
          type: string
        url:
          description: ''
          type: string
        text:
          description: ''
          type: string
  securitySchemes:
    Authorization:
      type: http
      scheme: bearer

````

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