UniterRouter
API ReferenceGoogle Compatible

Count Tokens

Gemini Token Counter API - Estimate token usage and costs before API calls. Calculate prompt tokens for Gemini Pro, Gemini 1.5 to optimize spending.

Count Tokens

Count the number of tokens in content before sending it to a model. Useful for estimating costs and ensuring content fits within context limits.

Cost Estimation: Use this endpoint to estimate API costs before making generation requests.

Endpoint

POST https://api.uniterouter.ai/v1beta/models/{model}:countTokens

Authentication

x-goog-api-key: your_uniterouter_api_key

Path Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID to use for tokenization

Request Body

ParameterTypeRequiredDescription
contentsarrayYesContent to count tokens for
generateContentRequestobjectNoFull request to count tokens for

Example Request

curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:countTokens" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $UNITEROUTER_API_KEY" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "Write a detailed essay about artificial intelligence and its impact on society."}]
      }
    ]
  }'
import google.generativeai as genai

genai.configure(
    api_key="your_uniterouter_api_key",
    transport="rest",
    client_options={"api_endpoint": "api.uniterouter.ai"}
)

model = genai.GenerativeModel("gemini-2.0-flash")

# Count tokens for a simple prompt
token_count = model.count_tokens("Hello, how are you?")
print(f"Token count: {token_count.total_tokens}")

# Count tokens for a conversation
token_count = model.count_tokens([
    {"role": "user", "parts": ["What is AI?"]},
    {"role": "model", "parts": ["AI is artificial intelligence..."]},
    {"role": "user", "parts": ["Tell me more about machine learning"]}
])
print(f"Conversation tokens: {token_count.total_tokens}")
import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
  baseUrl: "https://api.uniterouter.ai"
});

const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

const result = await model.countTokens({
  contents: [
    {
      role: "user",
      parts: [{ text: "Hello, how are you?" }]
    }
  ]
});

console.log(`Total tokens: ${result.totalTokens}`);

Response

{
  "totalTokens": 15
}

Model Context Limits

ModelContext WindowMax Output
gemini-2.0-flash1,048,5768,192
gemini-2.0-flash-thinking32,7678,192
gemini-1.5-pro2,097,1528,192
gemini-1.5-flash1,048,5768,192

Use Cases

Pre-flight Check

Ensure your content fits within the model's context window:

def check_fits_context(content, model_name, max_context=1000000):
    model = genai.GenerativeModel(model_name)
    token_count = model.count_tokens(content)
    
    if token_count.total_tokens > max_context:
        raise ValueError(f"Content too large: {token_count.total_tokens} > {max_context}")
    
    return token_count.total_tokens

# Check before sending
try:
    tokens = check_fits_context(long_document, "gemini-2.0-flash")
    print(f"Content fits: {tokens} tokens")
except ValueError as e:
    print(f"Error: {e}")

Cost Estimation

# Pricing (example - check current rates)
INPUT_COST_PER_MILLION = 0.075  # $0.075 per 1M input tokens
OUTPUT_COST_PER_MILLION = 0.30  # $0.30 per 1M output tokens

def estimate_cost(prompt, estimated_output_tokens=500):
    model = genai.GenerativeModel("gemini-2.0-flash")
    input_tokens = model.count_tokens(prompt).total_tokens
    
    input_cost = (input_tokens / 1_000_000) * INPUT_COST_PER_MILLION
    output_cost = (estimated_output_tokens / 1_000_000) * OUTPUT_COST_PER_MILLION
    
    return {
        "input_tokens": input_tokens,
        "estimated_output_tokens": estimated_output_tokens,
        "estimated_cost": input_cost + output_cost
    }

estimate = estimate_cost("Write a 500 word essay about AI")
print(f"Estimated cost: ${estimate['estimated_cost']:.6f}")

Truncation Strategy

def truncate_to_fit(content, max_tokens, model_name="gemini-2.0-flash"):
    model = genai.GenerativeModel(model_name)
    
    # Binary search for the right length
    words = content.split()
    low, high = 0, len(words)
    
    while low < high:
        mid = (low + high + 1) // 2
        test_content = " ".join(words[:mid])
        tokens = model.count_tokens(test_content).total_tokens
        
        if tokens <= max_tokens:
            low = mid
        else:
            high = mid - 1
    
    return " ".join(words[:low])

# Truncate long content to fit
truncated = truncate_to_fit(long_document, max_tokens=100000)

How is this guide?