AptoCompass Logo
Apto Compass

How to Calculate LLM Token Costs: Complete Math Guide for Frontier Models (2026)

Calculate exact LLM API costs per token across GPT-6, Claude Sonnet 5.5, Gemini 3.8, and DeepSeek V4. Verified pricing, prompt caching formulas, and production math.

By Abhinay Kumar•14 min read•Updated: 2026-10-27
Formula and calculations for estimating LLM token API costs across frontier models

Quick Summary & Key Takeaways

  • Frontier API pricing ranges from $0.08/$0.32 per 1M tokens on edge models (GPT-5.4 nano) to $10.00/$50.00 on autonomous reasoning engines (GPT-6 Astra).
  • Claude Sonnet 5.5 ($2.00 input, $10.00 output per 1M) and GPT-5.4 Mini ($0.25 input, $1.00 output) represent the primary high-throughput production benchmarks.
  • Output tokens carry a 3x to 5x pricing penalty across all frontier providers due to autoregressive decoding constraints and dedicated GPU VRAM retention.
  • Prompt caching reduces static prefix costs by up to 90% on Anthropic, Kimi, and DeepSeek, turning 100-page document RAG pipelines from financial liabilities into viable architectures.

Free Interactive Companion Tool

Test inputs and calculate figures directly with our in-browser tool

Launch Tool

How to Calculate LLM Token Costs: Complete Math Guide for Frontier Models (2026)

Calculating API token spend before deploying software to production prevents budget overruns. With modern frontier architectures like GPT-6, Claude Sonnet 5.5, Gemini 3.8, and DeepSeek V4, calculating your actual inference bill requires modeling input prefill, output generation, prompt cache hit ratios, and multi-turn context accumulation. This technical walkthrough provides the exact mathematical equations, code verification scripts, and real-world production benchmarks to calculate your monthly LLM API expenditures.

Quick Answer: Total request cost equals uncached input tokens plus cached tokens plus output tokens, each multiplied by its respective provider rate per 1,000,000 tokens. Output tokens cost 3x to 5x more than input tokens, while prompt caching cuts repetitive prefix costs by 50% to 90%.

Universal Formula

Universal LLM API Cost Equation

Total Cost = (Input Tokens ÷ 1,000,000 × Input Rate) + (Cached Tokens ÷ 1,000,000 × Cache Rate) + (Output Tokens ÷ 1,000,000 × Output Rate)
Input Tokens: Uncached prompt tokens evaluated during prefill.
Input Rate: Provider fee per 1,000,000 uncached input tokens.
Cached Tokens: Prompt tokens retrieved from Key-Value memory.
Cache Rate: Discounted fee per 1,000,000 cached tokens (50% to 90% off).
Output Tokens: Completion tokens generated autoregressively.
Output Rate: Provider fee per 1,000,000 output tokens (3x to 5x higher).

Current Token Pricing Matrix: Active 2026 Frontier Lineup

Model providers price API calls per 1,000,000 tokens. The table below presents verified rates across the active frontier models featured in our in-house AI Token Estimator and Cost Calculator as of October 2026. If you are architecting multi-turn agents or managing static prompt prefixes, model your complete context allocation using our system prompt and context budget calculator.

Model Name Provider Uncached Input (/ 1M) Cached Input (/ 1M) Output Tokens (/ 1M) Output / Input Multiplier Context Window
OpenAI GPT-6 Astra OpenAI $10.00 $5.00 (50% off) $50.00 5.0x 256k
OpenAI GPT-6 Sol OpenAI $6.00 $3.00 (50% off) $30.00 5.0x 256k
OpenAI GPT-5.6 Terra OpenAI $2.50 $1.25 (50% off) $12.50 5.0x 256k
OpenAI GPT-5.4 Mini OpenAI $0.25 $0.125 (50% off) $1.00 4.0x 128k
OpenAI GPT-5.4 nano OpenAI $0.08 $0.04 (50% off) $0.32 4.0x 128k
Anthropic Claude Sonnet 5.5 Anthropic $2.00 $0.20 (Read) / $2.50 (Write) $10.00 5.0x 1M
Anthropic Claude Opus 5.5 Anthropic $4.00 $0.40 (Read) / $5.00 (Write) $20.00 5.0x 1M
Moonshot AI Kimi K3.1 Pro Moonshot AI $2.00 $0.20 (Read) / $0.00 (Write) $10.00 5.0x 1M
DeepSeek DeepSeek V4 Pro DeepSeek $0.50 $0.05 (90% off) $2.00 4.0x 1M
DeepSeek DeepSeek V4.1 Flash DeepSeek $0.15 $0.015 (90% off) $0.60 4.0x 1M
Google Gemini 3.8 Flash Google $0.75 $0.1875 (75% off) $3.75 5.0x 1M
Google Gemini 3.5 Flash-Lite Google $0.10 $0.025 (75% off) $0.40 4.0x 1M
xAI Grok 4.7 xAI $2.00 $1.00 (50% off) $6.00 3.0x 256k

Prerequisites and Setup Requirements

To calculate token volumes programmatically and forecast production expenses, ensure you have:

  • Python Runtime: Python 3.10 or higher.
  • Tokenizer Libraries: Install the official token counting packages: pip install tiktoken anthropic
  • Environment Variables: Set your API credentials when running live count verifications: export OPENAI_API_KEY="your-api-key" export ANTHROPIC_API_KEY="your-api-key"

Step 1: Converting Words and Characters to Tokens

Large language models do not process words, characters, or syllables directly. They process chunks of characters known as tokens.

1 Token ≈ 0.75 Words
Standard Heuristic
1,000 Words ≈ 1,333 Tokens

However, this heuristic fails when processing code, structured JSON payloads, or non-English scripts. Modern tokenizers encode text with variable compression ratios:

  • OpenAI o200k_base (GPT-5 / GPT-6): Achieves high compression on programming syntax and multilingual alphabets, averaging 3.8 to 4.2 characters per token.
  • Anthropic Claude Tokenizer: Encodes Python whitespace and JSON indentation slightly differently, averaging 3.6 to 4.0 characters per token.
  • Structured Code and YAML: Indentation, brackets, and function signatures split into more tokens per word than standard prose. A 1,000-line Python script typically contains 1.4x to 1.8x more tokens than a 1,000-word essay.

Python Token Counter Script

Use this copy-paste script to measure exact token counts across text and code snippets:

import tiktoken

def inspect_token_density(text: str, encoding_name: str = "o200k_base") -> dict:
    enc = tiktoken.get_encoding(encoding_name)
    tokens = enc.encode(text)
    token_count = len(tokens)
    word_count = len(text.split())
    char_count = len(text)
    
    return {
        "token_count": token_count,
        "word_count": word_count,
        "char_count": char_count,
        "tokens_per_word": round(token_count / max(word_count, 1), 2),
        "chars_per_token": round(char_count / max(token_count, 1), 2)
    }

# Benchmark sample
sample_text = "Calculate exact API costs for GPT-6 and Claude Sonnet 5.5 across inference endpoints."
metrics = inspect_token_density(sample_text)
print(metrics)

Typical output:

{'token_count': 16, 'word_count': 12, 'char_count': 83, 'tokens_per_word': 1.33, 'chars_per_token': 5.19}

Step 2: Factoring the Output Token Multiplier

A common budgeting mistake is treating input tokens and output tokens equally. Across all frontier model providers, output tokens cost between 3x and 5x more than input tokens.

The Technical Reason for the Multiplier

Input tokens are processed in parallel during the prefill phase. The model loads the prompt into GPU memory and computes attention across the entire context window simultaneously.

Output tokens must be generated autoregressively: the model generates token N+1, appends it to the context, and re-evaluates the attention matrix to predict token N+2. This sequential process holds GPU Key-Value memory occupied for the duration of the stream, consuming memory bandwidth and preventing other requests from utilizing those tensor cores.

Concrete Example: Claude Sonnet 5.5 vs GPT-5.4 Mini

Suppose you execute an agentic query with 3,000 input tokens and 1,500 output tokens:

On Claude Sonnet 5.5 ($2.00 / 1M Input, $10.00 / 1M Output):

  • Input Cost: (3,000 ÷ 1,000,000) × $2.00 = $0.0060
  • Output Cost: (1,500 ÷ 1,000,000) × $10.00 = $0.0150
  • Total Request Cost: $0.0210
  • Observation: Output tokens represent 33% of the token volume but 71.4% of the financial cost.

On GPT-5.4 Mini ($0.25 / 1M Input, $1.00 / 1M Output):

  • Input Cost: (3,000 ÷ 1,000,000) × $0.25 = $0.00075
  • Output Cost: (1,500 ÷ 1,000,000) × $1.00 = $0.00150
  • Total Request Cost: $0.00225 (9.3x cheaper than frontier tier).

When designing prompts with our AI Prompt Generator, enforcing output brevity via system rules directly cuts inference bills.


Step 3: Calculating Prompt Caching Economics

Prompt caching is the most significant pricing development in modern LLM infrastructure. By caching static prefixes in Key-Value memory across sequential API requests, providers eliminate redundant matrix multiplication.

Provider Caching Mechanisms

1. Anthropic Prompt Caching (Claude Sonnet 5.5 & Opus 5.5)

  • Minimum Cache Threshold: 1,024 tokens.
  • Cache Write: 1.25x standard input rate (a 25% write penalty on the first call to store KV states for 5 minutes).
  • Cache Read: 0.10x standard input rate (a 90% discount on all subsequent hits).
  • TTL Window: 5 minutes, automatically refreshed with each hit.
Anthropic Cost = (Uncached Tokens ÷ 1M × Input Rate) + (Write Tokens ÷ 1M × 1.25 × Input Rate) + (Read Tokens ÷ 1M × 0.10 × Input Rate) + (Output Tokens ÷ 1M × Output Rate)

2. OpenAI Prompt Caching (GPT-6 & GPT-5.6)

  • Minimum Cache Threshold: 1,024 tokens (increments of 128 tokens).
  • Cache Write: Standard input rate (1.0x write cost; zero write penalty).
  • Cache Read: Flat 50% discount on cached tokens.
  • Activation: 100% automatic for matching prefixes.

3. DeepSeek Caching (DeepSeek V4 Pro & V4.1 Flash)

  • Cache Write: Standard input rate.
  • Cache Read: 90% discount (0.10x input price) applied automatically across all context lengths.

Step 4: Modeling Multi-Turn Context Accumulation

In multi-turn chat applications and autonomous coding agents like those compared in our AI code editor review, context does not remain static; it compounds quadratically with each turn:

Turn k Input Tokens = System Prompt + (Sum of all prior User & Assistant Turns) + Current User Query

Without context pruning or prompt caching, a 10-turn conversation where each user message is 200 tokens and each assistant response is 400 tokens consumes:

  • Turn 1: 500 (System) + 200 = 700 tokens
  • Turn 2: 700 + 400 (Assistant 1) + 200 = 1,300 tokens
  • Turn 5: 3,100 tokens
  • Turn 10: 6,100 tokens
  • Total Input Tokens Processed Across 10 Turns: 34,000 tokens!

By anchoring the static 500-token System Prompt and tools definition at the prefix, Anthropic and DeepSeek cache those tokens on Turn 1 and read them at a 90% discount through Turn 10. For empirical pricing comparisons and terminal benchmarks across aggressive alternatives, see our Kimi K3.1 Pro review and coding benchmark.


Worked Production Scenarios

Scenario A: Customer Support Agent Handling 10,000 Queries/Day

Architecture Specifications:

  • Static System Prompt & Company Policy: 2,500 tokens.
  • Dynamic User Query: 250 tokens.
  • Assistant Response: 350 tokens.
  • Daily Traffic: 10,000 API requests.
  • Target Model: Claude Sonnet 5.5 ($2.00 / 1M Input, $10.00 / 1M Output).

Monthly Cost Without Prompt Caching:

  • Daily Input Volume: 10,000 requests × 2,750 tokens = 27,500,000 tokens
  • Daily Input Cost: 27.5M × $2.00 / 1M = $55.00
  • Daily Output Volume: 10,000 requests × 350 tokens = 3,500,000 tokens
  • Daily Output Cost: 3.5M × $10.00 / 1M = $35.00
  • Daily Total: $90.00 per day
  • Monthly Cost (30 Days): $2,700.00

Monthly Cost With Claude Sonnet 5.5 Prompt Caching (99% Hit Ratio):

  • Cache Writes: First call incurs (2,500 ÷ 1,000,000) × $2.50 = $0.00625.
  • Cached Reads: 9,999 calls × 2,500 tokens = 24,997,500 tokens × $0.20 / 1M = $5.00.
  • Uncached Dynamic Input: 10,000 calls × 250 tokens = 2,500,000 tokens × $2.00 / 1M = $5.00.
  • Output Cost: Unchanged at $35.00 per day.
  • Daily Total: $45.006 per day
  • Monthly Cost (30 Days): $1,350.18
  • Net Monthly Savings: $1,349.82 (50.0% reduction in total API spend).

Architecture Challenge: Analyzing 100-page PDF contracts (~80,000 tokens) to answer 20 compliance questions per document.

Architectural Pattern Pipeline Mechanics Token Volume per Doc Monthly Cost (500 Docs) Accuracy & Coherence
Chunked Vector RAG (GPT-5.4 Mini) Vector search retrieves top 5 chunks (2,000 tokens) per question. 20 queries × 2,000 tokens = 40,000 tokens $12.50 / month Risk of missing cross-page clauses and definitions.
Full-Document Prompt Caching (Claude Sonnet 5.5) Entire 80,000-token contract cached once; 20 questions run against the active cache. 1 Write (80k @ $2.50) + 19 Reads (80k @ $0.20) + 10k Output $315.00 / month 100% global document context; zero chunk fragmentation.
Full-Document Prompt Caching (DeepSeek V4 Pro) Entire 80,000-token contract cached at $0.05/M read rate. 1 Write (80k @ $0.50) + 19 Reads (80k @ $0.05) + 10k Output $68.00 / month High-accuracy legal review at budget infrastructure cost.

Common Cost Leaks and Troubleshooting Matrix

Engineering Defect Financial Impact Diagnostic Symptom Remediation Protocol
Dynamic Timestamps in System Prompt Destroys prompt caching hit rate (drops to 0%). API bills 5x higher than modeled projections. Move Current Date: [timestamp] to the user message; keep the system prompt static.
Client Streaming Disconnections Billed for tokens generated after user closes browser. Output token volume remains high despite short sessions. Hook server-side AbortController to cancel LLM stream immediately upon disconnect.
Formatting Drift in JSON Schemas Model loops whitespace or re-generates malformed arrays. Maximum context window hit; runaway output bills. Enforce native structured outputs with our JSON to TypeScript and Pydantic generator.
Unbounded Multi-Turn Conversation Memory Context grows quadratically turn-over-turn. Late-turn messages cost 10x more than initial greetings. Implement a sliding window (retain last 6 turns) and summarize earlier history.

Frequently Asked Questions