You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Understanding tokens is fundamental to building cost-effective AI applications. This lesson explains how tokenisation works, how to count and estimate costs, and how to work within rate limits.
Tokens are the fundamental units that language models process. A token is not the same as a word — it is a subword unit determined by the model's tokeniser.
| Text | Approximate Tokens |
|---|---|
| "Hello" | 1 token |
| "Hello, world!" | 3 tokens |
| "Tokenisation" | 2–3 tokens |
| "supercalifragilistic" | 5–6 tokens |
Rules of thumb:
Modern LLMs use Byte Pair Encoding (BPE) or similar subword tokenisation:
"unbelievable" → ["un", "believ", "able"] (3 tokens)
"AI" → ["AI"] (1 token)
"GPT-4" → ["G", "PT", "-", "4"] (4 tokens)
import tiktoken
encoding = tiktoken.encoding_for_model("gpt-4o-mini")
tokens = encoding.encode("Hello, how are you?")
print(f"Token count: {len(tokens)}") # 5
print(f"Token IDs: {tokens}") # [9906, 11, 1268, 527, 499, 30]
import anthropic
client = anthropic.Anthropic()
result = client.count_tokens(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "Hello, how are you?"}],
)
print(f"Input tokens: {result.input_tokens}")
LLM pricing is based on tokens processed, split into input (prompt) tokens and output (completion) tokens.
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| GPT-4o mini | $0.15 | $0.60 |
| GPT-4o | $2.50 | $10.00 |
| Claude Haiku | $0.25 | $1.25 |
| Claude Sonnet | $3.00 | $15.00 |
Total cost = (input_tokens × input_price) + (output_tokens × output_price)
def estimate_cost(input_tokens, output_tokens,
input_price_per_m=0.15, output_price_per_m=0.60):
input_cost = (input_tokens / 1_000_000) * input_price_per_m
output_cost = (output_tokens / 1_000_000) * output_price_per_m
return input_cost + output_cost
# Example: 1,000 input tokens, 500 output tokens with GPT-4o mini
cost = estimate_cost(1000, 500)
print(f"Estimated cost: ${cost:.6f}") # $0.000450
API providers impose rate limits to ensure fair usage and system stability.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.