Anthropic Claude Token Counting API

Count tokens for a prospective Messages request.

Operations 1

POST /v1/messages/count_tokens Count tokens for a Messages request #

Work with this as data

Every API here is available over the APIs.io API and to AI agents over MCP.

MCP server

One button, every client — Claude, Cursor, VS Code and the rest.

https://apis.io/mcp

Tools for apis

7 MCP tools reach this
  • find_apisBrowse and filter every API in the catalog.
  • get_api_artifactsOne API's artifacts, grouped by type.
  • get_openapiThe primary OpenAPI for this API.
  • find_similar_apisAPIs that look like this one.
  • apis_io_searchSTART HERE — APIs, providers and tags for one query, each with its total.
  • resolveTurn a domain, URL or GitHub org into the provider it belongs to.
  • find_cohortsEvery scored population of providers in the catalog.
All 92 tools →

Call it yourself

curl for this page
This API
curl "https://apis.io/api/v1/apis/anthropic-claude-token-counting-api"
All apis
curl "https://apis.io/api/v1/apis?limit=25"

Discovery needs no key. Ratings and market analysis are Pro.

Get an API key

Free tier, no form to fill in. Signing in shares your email address with us — we store it to create your key and to recognise you if you sign in with another provider. See our Privacy Policy and Terms.

A second provider on the same verified email joins the account you already have.

OpenAPI Specification

anthropic-claude-token-counting-api-openapi.yml Raw ↑
openapi: 3.2.0
info:
  title: Anthropic Claude Messages Message Batches Token Counting API
  version: '2023-06-01'
  description: 'REST API for sending messages to Claude models with support for streaming,

    tool use, vision, system prompts, prompt caching, and extended thinking.

    Authentication uses an x-api-key header along with an anthropic-version header.

    '
  contact:
    name: Anthropic Support
    url: https://support.anthropic.com
  license:
    name: Anthropic Commercial Terms of Service
    url: https://www.anthropic.com/legal/commercial-terms
servers:
- url: https://api.anthropic.com
  description: Anthropic API production
security:
- ApiKeyAuth: []
tags:
- name: Token Counting
  description: Count tokens for a prospective Messages request.
paths:
  /v1/messages/count_tokens:
    post:
      tags:
      - Token Counting
      summary: Count tokens for a Messages request
      description: Count the number of input tokens that a Messages request would consume, without actually creating a Message.
      operationId: countMessageTokens
      parameters:
      - $ref: '#/components/parameters/AnthropicVersion'
      - $ref: '#/components/parameters/AnthropicBeta'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountTokensRequest'
      responses:
        '200':
          description: Token count response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CountTokensResponse'
        '400':
          $ref: '#/components/responses/ErrorResponse'
components:
  schemas:
    CountTokensRequest:
      type: object
      required:
      - model
      - messages
      properties:
        model:
          type: string
        messages:
          type: array
          items:
            $ref: '#/components/schemas/InputMessage'
        system:
          oneOf:
          - type: string
          - type: array
            items:
              type: object
        tools:
          type: array
          items:
            $ref: '#/components/schemas/Tool'
    InputMessage:
      type: object
      required:
      - role
      - content
      properties:
        role:
          type: string
          enum:
          - user
          - assistant
        content:
          oneOf:
          - type: string
          - type: array
            items:
              type: object
    Error:
      type: object
      properties:
        type:
          type: string
        error:
          type: object
          properties:
            type:
              type: string
            message:
              type: string
    CountTokensResponse:
      type: object
      properties:
        input_tokens:
          type: integer
        cache_creation_input_tokens:
          type: integer
        cache_read_input_tokens:
          type: integer
    Tool:
      type: object
      required:
      - name
      - input_schema
      properties:
        name:
          type: string
        description:
          type: string
        input_schema:
          type: object
  parameters:
    AnthropicVersion:
      name: anthropic-version
      in: header
      required: true
      description: API version, e.g. `2023-06-01`.
      schema:
        type: string
        default: '2023-06-01'
    AnthropicBeta:
      name: anthropic-beta
      in: header
      required: false
      description: Optional comma-separated list of beta feature flags.
      schema:
        type: string
  responses:
    ErrorResponse:
      description: An error response.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: Anthropic API key. Send as the `x-api-key` header on every request.