pythonbeginner

Token Counting and Cost Estimation

Count tokens accurately and estimate API costs before making LLM calls.

python
import tiktoken

def count_tokens(text: str, model: str = "gpt-4o") -> int:
    """Count tokens for a given text and model."""
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))

def estimate_cost(
    input_tokens: int,
    output_tokens: int,
    model: str = "gpt-4o"
) -> float:
    """Estimate API cost in USD."""
    pricing = {
        "gpt-4o":      {"input": 2.50, "output": 10.00},
        "gpt-4o-mini": {"input": 0.15, "output": 0.60},
        "gpt-3.5-turbo": {"input": 0.50, "output": 1.50},
    }
    rates = pricing.get(model, pricing["gpt-4o"])
    cost = (
        (input_tokens / 1_000_000) * rates["input"] +
        (output_tokens / 1_000_000) * rates["output"]
    )
    return round(cost, 6)

# Usage
prompt = "Explain the theory of relativity in detail."
input_tokens = count_tokens(prompt)
estimated_output = 500  # expected output tokens

cost = estimate_cost(input_tokens, estimated_output)
print(f"Input tokens: {input_tokens}")
print(f"Estimated cost: ${cost:.4f}")

Use Cases

  • Budget management
  • Cost optimization
  • Token limit checks

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.