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

# Count message tokens (Anthropic)

> Estimate the input tokens of a Messages request, including tool definitions. Uses the same estimator the Grid uses for its context check and price quote, not a model tokenizer. Free, no auth needed, rate-limited per IP.



## OpenAPI

````yaml /api-reference/openapi.json post /v1/messages/count_tokens
openapi: 3.1.0
info:
  title: x402 Singularity Layer API
  description: >-
    OpenAPI-backed reference for marketplace discovery, payment routes,
    webhooks, wallet-first auth, agent endpoints, and ERC-8004 flows.
  version: 1.0.0
servers:
  - url: https://api.x402layer.cc
security: []
tags:
  - name: Marketplace
    description: Public discovery and listing lookup
  - name: Public Endpoints
    description: Public endpoint metadata and hosted checkout context
  - name: Public Payment Links
    description: Hosted public payment-link lookup
  - name: Payments
    description: Hosted x402 payment challenge routes
  - name: Receipts
    description: Signed receipt lookup and verification helpers
  - name: Ratings
    description: Public listing ratings and authenticated rating actions
  - name: Webhooks
    description: Seller webhook management API
  - name: Agent Auth
    description: Wallet-first challenge and verification routes
  - name: Agent Endpoints
    description: Create, read, top up, and delete agent endpoints
  - name: ERC-8004
    description: Agent registry and registration lifecycle routes
  - name: Marketplace Agents
    description: Public ERC-8004 marketplace discovery routes
  - name: Compute Catalog
    description: Compute plans, regions, and OS catalog
  - name: Compute Instances
    description: Provision, inspect, extend, and destroy compute instances
  - name: Compute API Keys
    description: API keys for compute agent access
  - name: Fundraiser Campaigns
    description: List, view, create, and edit fundraiser campaigns
  - name: Fundraiser Contributions
    description: Record and list campaign contributions
  - name: Fundraiser Comments
    description: Campaign comment threads
  - name: Fundraiser Media
    description: Campaign image uploads and OG images
  - name: Enterprise
    description: >-
      Enterprise partner configuration, endpoint listing, revenue stats, and
      transaction ledger
  - name: Staking
    description: Agentic $SGL staking
  - name: SGL Grid
    description: >-
      Decentralized, confidential, OpenAI-compatible inference served by
      attested TEE nodes.
  - name: Compute Credits
    description: Prepaid USDC credit balance shared across Machines and Grid.
  - name: Agent Pods
    description: >-
      Deploy and manage hosted agents (Agent Pods), plus the pod's
      OpenAI-compatible adapter for external clients.
  - name: Agent Pods API
    description: >-
      The programmatic `/pods/v1` surface: create, drive and destroy Agent Pods
      with a single `X-API-Key`. Distinct from the Agent Pods dashboard routes,
      which expect a wallet signature or a browser session.
paths:
  /v1/messages/count_tokens:
    servers:
      - url: https://grid.x402compute.cc
    post:
      tags:
        - SGL Grid
      summary: Count message tokens (Anthropic)
      description: >-
        Estimate the input tokens of a Messages request, including tool
        definitions. Uses the same estimator the Grid uses for its context check
        and price quote, not a model tokenizer. Free, no auth needed,
        rate-limited per IP.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - messages
              properties:
                model:
                  type: string
                  example: qwen-2.5-32b
                  description: >-
                    A Grid model id from `GET /v1/models`, never a Claude model
                    id.
                messages:
                  type: array
                  description: >-
                    Conversation turns, `user` and `assistant`. Must not end
                    with an assistant message (no prefill). A mid-conversation
                    `system` message is folded into the user turn it follows.
                  items:
                    type: object
                    required:
                      - role
                      - content
                    properties:
                      role:
                        type: string
                        enum:
                          - user
                          - assistant
                      content:
                        oneOf:
                          - type: string
                          - type: array
                            items:
                              type: object
                              description: >-
                                A content block. Supported types: `text`,
                                `image` (base64 source, vision models only),
                                `tool_use`, `tool_result` (text content,
                                optional `is_error`).
                              properties:
                                type:
                                  type: string
                                  enum:
                                    - text
                                    - image
                                    - tool_use
                                    - tool_result
                                text:
                                  type: string
                                source:
                                  type: object
                                  description: >-
                                    For `image`: `{type: "base64", media_type,
                                    data}`. URLs and Files API images are not
                                    supported.
                                id:
                                  type: string
                                name:
                                  type: string
                                input:
                                  type: object
                                tool_use_id:
                                  type: string
                                content:
                                  oneOf:
                                    - type: string
                                    - type: array
                                      items:
                                        type: object
                                is_error:
                                  type: boolean
                  example:
                    - role: user
                      content: Hello
                system:
                  oneOf:
                    - type: string
                    - type: array
                      items:
                        type: object
                  description: System prompt, as a string or text blocks.
                tools:
                  type: array
                  items:
                    type: object
                    properties:
                      name:
                        type: string
                      description:
                        type: string
                      input_schema:
                        type: object
                  description: >-
                    Your own tools with an `input_schema`. Server tools and
                    Anthropic-defined tools are refused.
                tool_choice:
                  type: object
                  description: >-
                    `{type: auto | any | tool | none}`.
                    `disable_parallel_tool_use: true` is refused.
      responses:
        '200':
          description: Token estimate.
          content:
            application/json:
              schema:
                type: object
                properties:
                  input_tokens:
                    type: integer
                    example: 4
        '400':
          description: '`invalid_request_error`.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
        '429':
          description: '`rate_limit_error`.'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnthropicError'
components:
  schemas:
    AnthropicError:
      type: object
      description: >-
        Anthropic-shaped error. The Grid's own detail (for example `code`,
        `context_limit`, `job_id`) rides along inside `error`.
      properties:
        type:
          type: string
          example: error
        error:
          type: object
          properties:
            type:
              type: string
              enum:
                - invalid_request_error
                - authentication_error
                - billing_error
                - permission_error
                - not_found_error
                - rate_limit_error
                - timeout_error
                - api_error
                - overloaded_error
            message:
              type: string
        request_id:
          type: string

````

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