synthetic
    API
    • Overview
    • Getting Started
    • Models
    OpenAI Reference
    • /models
    • /chat/completions
    • /completions
    • /embeddings
    Anthropic Reference
    • /messages
    • /messages/count_tokens
    Synthetic Reference
    • /quotas
    Guides
    • Claude Code
    • Octofriend by Synthetic
    • GitHub Copilot
    • Xcode Intelligence
    • Back to app

    Count Tokens API

    POST https://api.synthetic.new/anthropic/v1/messages/count_tokens

    Count the number of input tokens in a set of messages before sending them to a model.

    Tip

    /messages/count_tokens requests do not count against your subscription limits!

    Subscriber Feature

    This endpoint is currently available to subscribers only.

    Request Body

    ParameterTypeRequiredDescription
    modelstringYesModel name (must be prefixed with hf:). See supported Models.
    messagesarrayYesArray of message objects to count tokens for

    Message Object

    ParameterTypeRequiredDescription
    rolestringYesRole: user or assistant
    contentstringYesMessage content (text only for the /count_tokens endpoint)

    Example Usage

    • Python
    • TypeScript
    • curl
    import anthropic
    
    client = anthropic.Anthropic(
      api_key="SYNTHETIC_API_KEY",
      base_url="https://api.synthetic.new/anthropic/v1"
    )
    
    # Count tokens for a simple text message
    response = client.messages.count_tokens(
      model="hf:deepseek-ai/DeepSeek-V3-0324",
      messages=[
        {"role": "user", "content": "What is the capital of France?"}
      ]
    )
    
    print(f"Input tokens: {response.input_tokens}")

    Example Response

    • json
    {
      "input_tokens": 20
    }