OpenAI Token Counter

Count tokens for OpenAI models and estimate API cost efficiently.

Platform

Model

0 characters
OpenAI Token Guide

OpenAI Token Counter
GPT-4o, GPT-4, o1 & o3 — Complete Guide

Understand exactly how OpenAI tokenizes your prompts across GPT-4o, GPT-4, o1, o3, and o4-mini. Learn the hidden token costs that inflate your API bills — and the optimization strategies that cut them by 30–60%.

20+

OpenAI Models

tiktoken

Tokenizer

128K

Max Context

Exact

Count Accuracy

Under the Hood

How OpenAI Tokenization Actually Works

BPE, vocabulary sizes, and why 'hello world' isn't always 2 tokens

Byte Pair Encoding (BPE)

OpenAI's tokenization algorithm

OpenAI uses Byte Pair Encoding (BPE) — an algorithm originally developed for text compression, adapted for neural language models by researchers at the University of Edinburgh in 2016. BPE starts with individual bytes (256 units) and iteratively merges the most frequent adjacent pairs until it reaches the desired vocabulary size.

The result: common words like "the", "is", "are" become single tokens. Rare words like "subterranean" get split into subword units: sub + terr + anean. This means the same English character count produces vastly different token counts depending on vocabulary frequency.

Tokenizer Comparison: cl100k vs o200k

Hello, world!
cl100k: 4o200k: 4Simple sentence — same
ChatGPT is amazing!
cl100k: 5o200k: 4o200k merges 'ChatGPT'
JSON.stringify(data)
cl100k: 7o200k: 6o200k: larger vocab wins
https://openai.com/api
cl100k: 9o200k: 7URLs vary significantly
antidisestablishmentarianism
cl100k: 8o200k: 6Long words split less on o200k

* Approximate counts for illustration purposes

The Whitespace & Punctuation Trap

OpenAI's tokenizers handle leading whitespace specially — a space before a word is merged into the token (e.g., " world" is one token, not " " + "world"). This is why "hello world" ≠ 2 tokens; it's actually 3: "hello" + " world". Non-ASCII characters (accented letters, Chinese/Arabic scripts) tokenize as multiple bytes — a single Chinese character can cost 2–4 tokens, making non-English prompts significantly more expensive per character.

Hidden Costs

Token Usage You Didn't Know You Were Paying For

System messages, function schemas, and JSON mode all silently inflate your bill

System Message Overhead

Always Billed

Your system prompt is prepended to every single API call. A 400-token system prompt across 10,000 daily calls = 4 million input tokens per day. Trim ruthlessly — remove pleasantries, redundant instructions, and example conversations from the system prompt.

Function / Tool Schemas

Per-Request Cost

Every function definition you register is serialized into the context. A well-described function with 3 parameters typically adds 50–120 tokens. Ten functions = 500–1,200 tokens added to every call, whether invoked or not. Only include the tools needed for the current turn.

JSON Mode Inflation

15–30% Overhead

Structured output forces the model to produce syntactically valid JSON, adding brackets, commas, quotes, and key names as literal tokens. A 100-token prose answer can become 130–145 tokens in JSON. Use it only when downstream parsing genuinely requires it.

Conversation History

Grows Unbounded

Multi-turn chat applications that send the entire message history on every call compound costs rapidly. A 20-turn conversation might carry 3,000+ tokens of history into each new request. Implement a sliding window or periodic summarization to cap history size.

Reasoning Tokens (o1/o3)

Invisible Billing

OpenAI's reasoning models (o1, o3, o4-mini) use internal 'thinking' tokens before producing an answer. These reasoning tokens are billed at output rates but not shown in your response. A complex math problem might trigger thousands of hidden reasoning tokens.

API Framing Overhead

~7 Tokens/Turn

The OpenAI API adds ~3 tokens of framing per message (role markers, separators) and ~3 tokens at the start of every assistant reply. In a 10-turn conversation, that's ~70 extra tokens of infrastructure overhead on top of your actual content.

Cost Anatomy

Real Cost Breakdown — Input vs Output

Why output tokens are the silent budget killer most developers ignore

2025 OpenAI Pricing (per 1M tokens)

GPT-4o
IN: $2.50OUT: $10.004× output
GPT-4o mini
IN: $0.15OUT: $0.604× output
o4-mini
IN: $1.10OUT: $4.404× output
o3
IN: $10.00OUT: $40.004× output
GPT-4 Turbo
IN: $10.00OUT: $30.003× output

* Prices as of mid-2025. Verify at platform.openai.com/pricing

Real Example: A Customer Support Bot

System prompt

320 tokens · input

$0.000800

User message

45 tokens · input

$0.000113

Conversation history (10 turns)

1200 tokens · input

$0.003000

Function schemas (3 tools)

240 tokens · input

$0.000600

Model response

380 tokens · output

$0.003800
Total per call (GPT-4o)$0.008313

× 10,000 calls/day = $83.13/day

Why Long Outputs Kill Your Budget

The 4× output multiplier means every token the model generates is 4 times more expensive than a token you send. If you ask GPT-4o to "write a detailed 2,000-word blog post", that's approximately 2,600 output tokens ($0.026) vs a 50-token prompt ($0.000125). The response costs 200× more than the question. Always specify output length constraints when brevity is acceptable: "Respond in 2–3 sentences", "Give a bullet-point summary", "Be concise."

Optimization

Prompt Optimization Tricks That Actually Work

Proven techniques to reduce OpenAI API costs by 30–60% without sacrificing quality

Compress System Prompts

Before

You are a helpful, friendly, and professional customer service assistant. Please always respond in a polite and courteous manner. Make sure to greet the user warmly.

After

Customer service assistant. Be polite, concise.
~30 tokens saved

Remove Redundant Instructions

Before

Please make sure to always respond in English. Do not use any other languages. All responses should be in English only.

After

Respond in English.
~25 tokens saved

Enable Prompt Caching

GPT-4o supports prompt caching for inputs >1,024 tokens. Place your static system prompt and context first — cached tokens cost 50% less ($1.25 vs $2.50/1M). For production apps with a fixed system prompt, this alone cuts input costs in half.

Use Embeddings for Long Context

Instead of stuffing a 50-page PDF into context (50K+ tokens), embed it into a vector database and retrieve only the 5–10 most relevant chunks (500–1,000 tokens). RAG (Retrieval-Augmented Generation) is the single highest-ROI optimization for document-heavy applications.

Limit max_tokens on Output

Always set a max_tokens limit appropriate to your use case. For classification tasks, set max_tokens=5. For summaries, max_tokens=200. This prevents runaway verbose responses and makes cost predictable. The model won't waste tokens if it knows it has a budget.

Debugging context_length_exceeded

1

Count your total tokens

Use our counter with your full prompt (system + history + user). Identify which component is largest.

2

Switch to a higher-context model

GPT-4o supports 128K tokens. If you're on GPT-3.5 (4K/16K context), upgrading often resolves it immediately.

3

Implement message sliding window

Keep only the last 10 messages. When the window fills, summarize the oldest messages into a single compact summary.

4

Trim function schemas

Remove verbose descriptions from unused tools. Every character in a function description is a token.

Expert FAQ

Frequently Asked Token Questions

Answers from developers who've optimized millions of OpenAI API calls

Editorial Standards

Our content is created by experts and reviewed for technical accuracy. We follow strict editorial guidelines to ensure quality.

Learn more about our standards

Contact Information

UntangleTools
support@untangletools.com

Last Updated

UntangleTools Logo
UntangleTools Logo
UntangleTools Logo