Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 13/ 160
01. LLM Fundamentals

Token Pricing

Input vs output pricing, cached tokens, and estimating unit economics for AI features.

BeginnerInput Tokens · Output Tokens3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

You pay for the words you send and the words you get back, and the words you get back cost more.

Think of it like…

A taxi meter with different rates for the waiting time and the distance driven: you pay for how much you tell the driver (input) and how much the driver talks back (output), and the longer the route, the more the meter runs.

Where you’ve seen it

A support copilot handles 200,000 chats a month. Each request sends 3,000 input tokens (system prompt, history, retrieved articles) and gets 300 output tokens. With illustrative prices of $3 per million input and $15 per million output, a request costs 0.009 + 0.0045 = $0.0135, or about $2,700 a month. Caching the 2,000-token stable prefix at a 90 percent discount, and trimming history, brings it near $1,500, without changing the user experience.

Words you’ll hear

Input vs output tokens
Prompt tokens are cheaper; generated tokens usually cost 3 to 5 times more.
Cached input
Repeated prefix tokens billed at a discounted read rate.
Batch API
Asynchronous processing at a discount (often around half price) for non-urgent work.
Unit economics
Cost per request, user, resolved ticket or other business unit versus the revenue or value it creates.

Visual diagram

Loading interactive diagram

Theory & deep dive

Estimating cost per request and per month

Cost per request = (input tokens × input rate) + (output tokens × output rate), minus savings from cached input. Multiply by request volume for a monthly figure, then add the costs that are easy to forget: retries and fallbacks, embedding and retrieval calls, evaluation runs, agent loops (each step is another call whose history grows), and reasoning tokens. Instrument real usage from API response fields rather than estimating from the prompt template.

Report cost against value, not alone: cost per resolved ticket, per generated report, per active user. A feature costing $0.02 per request is cheap if it saves a support agent two minutes, and expensive if users trigger it ten times per session for a free tier. Prices differ widely across model sizes, often by 10 to 50 times between a small and a frontier model, which is why model choice is the biggest lever.

A quick estimate

5,000 users × 20 requests a month × (2,500 input + 400 output tokens) with $2/$10 per million rates: input 250M tokens = $500, output 40M tokens = $400, total about $900 a month, or $0.18 per user.

Key takeaway: Tokens × rates × volume, plus retries, agents and reasoning; measure real usage.

Levers for reducing cost

In rough order of impact: use the smallest model that meets your quality bar (route easy requests to cheaper models); cut input by trimming history, retrieving fewer and shorter chunks, and compressing prompts; cache stable prefixes; cap output length and ask for concise formats; use batch APIs for offline work; avoid redundant calls with response caching; and reduce retries by using structured outputs. For self-hosted models, cost shifts to GPU hours and utilisation, where batching and quantisation are the main levers.

Set guardrails: per-user and per-tenant budgets, max-token limits, alerts on spend anomalies, and dashboards of cost per feature. Run cost into your evaluation loop, comparing quality-per-dollar when you change models or prompts, because a 2 percent quality gain at 4 times the cost is rarely a good trade.

Cheaper by design

Route 70% of requests (simple FAQs) to a small model at one tenth the price, keep 30% on the large model, and cache the system prompt: blended cost falls by more than half while a held-out evaluation shows no measurable drop in resolution rate.

Key takeaway: Model choice first, then input trimming, caching, output caps and batching; guard with budgets.

How it works, step by step

Step 1 of 4
Instrumentation

1. Measure tokens per request

Log input, cached, output and reasoning tokens from every API response, by feature.

usage.input_tokens · cached · output
Key numbers at this step
Source
API usage fields

When to use it & trade-offs

What it is

LLM APIs bill per token, with separate rates for input (prompt) and output (generated) tokens, usually quoted per million. Output tokens cost several times more than input tokens because each is generated one at a time. Variations include cheaper cached input, discounted batch processing, premium rates for long contexts, extra charges for images or audio, and billed reasoning tokens. Unit economics means turning those rates into cost per request, per user and per business outcome.

Why it matters: An AI feature that delights users can still lose money on every call. Product and engineering decisions, such as which model, how long a prompt, how many retries, whether to cache, depend on cost per task, and prices change often, so you need a method rather than memorised numbers. Interviews and design reviews ask you to estimate monthly cost and say how you'd cut it.

When to reach for it

  • Forecasting monthly cost before launch
  • Choosing models by quality per dollar
  • Setting per-tenant budgets and limits

When to avoid or be careful

  • Estimating from word counts or a single sample prompt
  • Ignoring retries, agent loops and reasoning tokens
  • Optimising cost without a quality evaluation

Cost-control approaches

Smaller model / routing
Best for: Mixed workloads with many easy requests

Strengths: Often the largest saving.

Watch out: Needs a router and quality evaluation.

Prompt caching and trimming
Best for: Long, repeated context

Strengths: Same quality, lower input cost, faster responses.

Watch out: Requires prompt restructuring.

Batch API / self-hosting
Best for: Offline or very high-volume workloads

Strengths: Discounted rates or fixed GPU cost.

Watch out: Latency, or the operational burden of GPUs.

Interview signals & numbers to know

When this topic comes up

  • “Estimate the monthly cost of this feature”
  • “Why are output tokens priced higher?”
  • “How would you cut LLM costs by 50%?”
  • Unit economics
  • Batch vs real-time

Benchmarks & rules of thumb

≈ 3 – 5× -
output price relative to input price
≈ 50% -
common batch API discount
10 – 50× -
price gap between small and frontier models

Common mistakes & follow-ups

Common interview mistakes

  • Pricing an agent as one request when each step re-sends a growing history.
  • Forgetting that reasoning tokens are billed as output and can dwarf the visible answer.
  • Switching to a cheaper model without an evaluation and later discovering it hurts resolution rate.

Likely interviewer follow-ups

  • How would you estimate the monthly cost of a support chatbot?
  • What's the single biggest lever to reduce LLM cost?
  • How do you stop one tenant from running up the bill?
Interview tip: Always do the arithmetic out loud with round numbers and name the assumptions. A clear back-of-envelope estimate is the answer interviewers want.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Token Pricing with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Prompt Caching
Next topic
Hallucinations