How to Cut OpenAI API Costs by 40% (Token Optimization)
Every space, tab, and newline costs money with the OpenAI API. Learn to count tokens accurately and sanitize prompts to cut your bill by up to 40%.
If you are building a chatbot, a summarization tool, or any app powered by the OpenAI API, you are billed by the token. A token is roughly four characters, or about 0.75 words.
When you are prototyping, it seems cheap. But when you scale to thousands of users, or when you send a massive 2,000-token system prompt with every single request, the costs compound quickly.
The hidden cost of copy-pasting
Most developers write their prompts in a text editor or a Word document, then copy them into their code. Those documents are full of invisible formatting.
- Trailing spaces at the end of every line.
- Multiple consecutive newlines (paragraph gaps).
- Tabs instead of spaces.
LLMs count every single one of these characters as a token. If your prompt has 50 lines of text with trailing spaces, you are paying for 50 tokens that provide absolutely no value to the model.
Step 1: Count your tokens accurately
You cannot optimize what you cannot measure. Do not guess your token count. Different models use different tokenizers. GPT-4o uses a different encoding (`o200k_base`) than GPT-3.5 (`cl100k_base`).
Use a tool that uses the exact same Tiktoken library that OpenAI uses internally.
100% accurate Tiktoken counts for GPT-4o and Claude.
Count Your TokensStep 2: Sanitize the prompt
Once you know your baseline, you need to strip the invisible bloat. You can do this manually, or you can use a sanitizer tool that runs via regex.
A good sanitizer will replace tabs with spaces, remove trailing whitespace from every line, and compress three or more newlines down to just two (which is all the model needs to recognize a paragraph break).
Strip token-wasting whitespace instantly.
Sanitize System PromptStep 3: Optimize the logic
Whitespace is the easy win, but the real savings come from optimizing the instructions.
- Remove redundant instructions. If you say "Be polite" and "Always say please and thank you", delete one. The model understands both.
- Use concise language. Instead of "I want you to act as an expert copywriter who is very good at writing...", just say "Act as an expert copywriter."
- Move static data to RAG. Do not put a 5,000-word knowledge base in the system prompt. Use a vector database to only send the relevant chunks.
Monitor your usage
Make it a habit to run your prompts through the Token Counter before you push them to production. It takes 10 seconds and can literally save you hundreds of dollars as you scale.
Tools mentioned in this article
Frequently asked
Do spaces count as tokens in OpenAI?
Yes. OpenAI uses a tokenizer that chunks text. Often a space is included with the word that follows it, but trailing spaces at the end of a line definitely count as distinct tokens.
How much does a token cost?
It depends on the model. As of 2026, GPT-4o is generally around $2.50 per million input tokens, while smaller models like GPT-4o-mini are fractions of a cent. Even so, large prompts scale costs quickly.
Are Claude tokens counted the same way?
No. Anthropic uses a different tokenizer. Currently, there is no public browser-based tokenizer for Claude, so our Token Counter uses a highly accurate approximation to give you an estimate.
Does a smaller prompt affect the quality of the AI output?
Usually, no. In fact, LLMs often perform *better* with concise, sanitized prompts because there is less "noise" for the model to get distracted by.