TokenCalculator.com
Home API Documentation

API Documentation

Access the Token Calculator API to integrate token counting and cost estimation into your own applications.

Overview

The TokenCalculator.com API provides programmatic access to our token counting and cost estimation features. With our API, you can:

  • Count tokens for a given text using various tokenizers
  • Calculate estimated costs for different LLM providers
  • Get comprehensive model information and pricing data
  • Compare costs across multiple models

Authentication

All API requests require authentication using an API key. You can obtain your API key by contacting us.

Include your API key in the request header:

Authorization: Bearer YOUR_API_KEY

Base URL

All API requests should be made to:

https://tokencalculator.com/api/v1

Endpoints

Count Tokens

Count the number of tokens in a given text.

POST /count-tokens

Request Body:

{
  "text": "Your text to be tokenized",
  "tokenizer": "cl100k_base" // Optional, defaults to cl100k_base
}

Response:

{
  "success": true,
  "token_count": 5,
  "characters": 25,
  "words": 5,
  "tokenizer": "cl100k_base"
}

Calculate Cost

Calculate the cost for a specific model based on input and output tokens.

POST /calculate-cost

Request Body:

{
  "model_id": "gpt-4o",
  "input_tokens": 1000,
  "output_tokens": 500
}

Response:

{
  "success": true,
  "model_id": "gpt-4o",
  "input_cost": 0.01,
  "output_cost": 0.03,
  "total_cost": 0.04,
  "currency": "USD"
}

Get Models

Retrieve a list of all available models or models from a specific provider.

GET /models

Query Parameters:

provider: (optional) Filter by provider name (e.g., openai, anthropic)

Response:

{
  "success": true,
  "models": [
    {
      "id": "gpt-4o",
      "name": "GPT-4o",
      "provider": "OpenAI",
      "context": "128K",
      "input_price_per_1m": 5.0,
      "output_price_per_1m": 15.0,
      "tokenizer_id": "cl100k_base"
    },
    // ... more models
  ]
}

Get Model Details

Retrieve detailed information about a specific model.

GET /models/{model_id}

Path Parameters:

model_id: The ID of the model to retrieve

Response:

{
  "success": true,
  "model": {
    "id": "gpt-4o",
    "name": "GPT-4o",
    "provider": "OpenAI",
    "description": "GPT-4o is OpenAI's latest multimodal model...",
    "context": "128K",
    "input_price_per_1m": 5.0,
    "output_price_per_1m": 15.0,
    "tokenizer_id": "cl100k_base",
    "released": "2024-05",
    "capabilities": ["text", "images", "audio"],
    "url": "https://openai.com/gpt-4o"
  }
}

Rate Limits

The API is rate-limited to protect our services from excessive usage. The current limits are:

  • Free tier: 100 requests per day
  • Basic tier: 1,000 requests per day
  • Pro tier: 10,000 requests per day
  • Enterprise tier: Custom limits

Error Handling

When an error occurs, the API will return an appropriate HTTP status code and a JSON response with details about the error.

{
  "success": false,
  "error": {
    "code": "invalid_request",
    "message": "The request was invalid. Please check your parameters and try again."
  }
}

Support

If you need help with the API or have questions, please contact our support team.