Cost & Tokens

You pay per token, split into input (prompt) and output (completion).

LLM pricing is based on tokens, and almost always split into two rates:

  • Input tokens β€” everything you send (system, history, documents).
  • Output tokens β€” everything the model generates. Usually priced higher.

Estimating a call

Cost = (input tokens x input rate) + (output tokens x output rate). The API response reports exact usage so you can track spend.

How to spend less

  • Trim history and documents to what is relevant.
  • Set a tight max_tokens.
  • Use a smaller model for easy tasks.
  • Cache repeated prompts (covered later).

Example

Example Β· json
{
  "usage": {
    "prompt_tokens": 850,
    "completion_tokens": 120,
    "total_tokens": 970
  }
}

When to use it

  • A startup tracks monthly token usage per customer and charges overages above the free tier based on input + output token counts.
  • A developer reduces prompt size from 2000 to 400 tokens by moving static context to a cached system prompt, cutting costs by 80%.
  • A product team chooses gpt-4o-mini over gpt-4o for a classification task because accuracy is equivalent and the smaller model costs 15x less.

More examples

Calculate request cost in Python

Computes the dollar cost of a request by multiplying token counts by per-token prices.

Example Β· python
from openai import OpenAI

# gpt-4o-mini pricing (USD per 1M tokens, as of mid-2025)
INPUT_PRICE  = 0.15 / 1_000_000
OUTPUT_PRICE = 0.60 / 1_000_000

client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': 'Summarise the water cycle.'}]
)
u = r.usage
cost = u.prompt_tokens * INPUT_PRICE + u.completion_tokens * OUTPUT_PRICE
print(f'Cost: ${cost:.6f}  (in={u.prompt_tokens}, out={u.completion_tokens})')

Track cumulative token spend

Accumulates token counts across multiple requests to measure the cost of a whole batch.

Example Β· python
from openai import OpenAI

client = OpenAI()
total_in = total_out = 0

questions = [
    'What is photosynthesis?',
    'What is osmosis?',
    'What is mitosis?'
]
for q in questions:
    r = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role': 'user', 'content': q}],
        max_tokens=60
    )
    total_in  += r.usage.prompt_tokens
    total_out += r.usage.completion_tokens

print(f'Batch totals β€” input: {total_in}, output: {total_out}')

Reduce cost by shrinking the prompt

Truncates the context to a token budget before sending, reducing prompt cost while keeping key content.

Example Β· python
import tiktoken
from openai import OpenAI

enc = tiktoken.encoding_for_model('gpt-4o-mini')

def trim_to_tokens(text, max_tokens=200):
    tokens = enc.encode(text)
    return enc.decode(tokens[:max_tokens])

long_context = open('/tmp/article.txt').read()
short_context = trim_to_tokens(long_context, max_tokens=200)

client = OpenAI()
r = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {'role': 'system', 'content': 'Answer based on the context below.'},
        {'role': 'user',   'content': short_context + '\n\nWhat is the main topic?'}
    ]
)
print(r.choices[0].message.content)

Discussion

  • Be the first to comment on this lesson.
Cost & Tokens β€” AI | SoundsCode