How to Calculate LLM API Costs for Your App (Complete Guide)
Understanding token pricing doesn't have to be complicated. Here is the step-by-step guide to estimating input tokens, output tokens, and your total monthly API spend.
When building an application powered by Large Language Models (LLMs) like GPT-4o, Claude Sonnet, or Gemini, the biggest unknown is often the infrastructure cost. Unlike traditional cloud computing billed by time or bandwidth, LLMs are billed by "tokens."
In this guide, we'll break down exactly how tokens work, the formula for calculating costs, and how to estimate your monthly budget before you write a single line of code.
1. What is a Token?
A token is the fundamental unit of data an LLM reads and generates. It's not quite a word, and it's not quite a character. Think of it as a syllable or a chunk of a word.
The Golden Rule of Tokens:
- 1 Token ≈ 4 characters in English
- 1 Token ≈ 0.75 words
- 100 Tokens ≈ 75 words
Keep in mind that this rule applies to standard English text. Code, structured data (like JSON or XML), and non-English languages often consume tokens less efficiently.
2. Input vs. Output Tokens
All major API providers (OpenAI, Anthropic, Google) split their pricing into two categories:
- Input Tokens (Prompt Tokens): Everything you send to the model. This includes your system instructions, the user's message, and any context or documents you append.
- Output Tokens (Completion Tokens): Everything the model generates and sends back to you.
Why does this matter? Generating output is computationally much heavier than reading input. As a result, Output Tokens are almost always 3 to 5 times more expensive than Input Tokens.
3. The Cost Calculation Formula
API providers quote prices "per 1 Million tokens" (denoted as /1M). The universal formula to calculate the cost of a single API request is:
((Input_Tokens / 1,000,000) * Input_Price)
+ ((Output_Tokens / 1,000,000) * Output_Price)
4. A Real-World Example
Let's say you are building an app that summarizes customer service emails using GPT-4o mini.
- GPT-4o mini pricing: $0.15 / 1M Input, $0.60 / 1M Output
- Average email (input): 800 words ≈ 1,066 tokens
- System prompt (input): 100 words ≈ 133 tokens
- Average summary (output): 100 words ≈ 133 tokens
Calculations for one email:
- Total Input Tokens: 1,066 + 133 = 1,199 tokens
- Input Cost: (1,199 / 1,000,000) * $0.15 = $0.00017985
- Output Cost: (133 / 1,000,000) * $0.60 = $0.0000798
- Total Cost per Email: ~$0.00026
If you process 100,000 emails a month, your total monthly API bill for this feature would be just $26.00.
Don't want to do the math manually? Use our free calculator →
Open Calculator