pythonbeginner
Token Counting and Cost Estimation
Count tokens accurately and estimate API costs before making LLM calls.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
Token Counting with tiktoken
Count tokens, split text by token limits, and estimate API costs using the tiktoken library.
Best for: cost estimation
#tiktoken#tokens
pythonbeginner
Token Counter with Tiktoken
Count tokens and estimate costs for OpenAI API calls using the tiktoken tokenizer library.
Best for: Cost estimation
#tokens#tiktoken
typescriptadvanced
AI Model Router for Cost Optimization
Route prompts to different LLM models based on complexity to optimize cost and response quality.
Best for: Reducing AI API costs for production apps
#routing#optimization
pythonintermediate
Prompt Caching with OpenAI API
Reduce costs by up to 50% using OpenAI's automatic prompt caching for repeated context prefixes.
Best for: cost reduction
#openai#caching