Phase 05 · GenAI & LLM ApplicationsLesson 3 of 3
Cutting AI API Costs
Keep LLM features affordable as usage grows.
The problem
An AI feature that costs a few rupees in testing can cost lakhs once thousands of users rely on it. Understanding how LLM APIs are priced, and the handful of techniques that cut costs dramatically, is a core skill for anyone shipping AI products.
The concept
How LLM APIs charge: tokens#
Models read and write text in tokens: chunks of characters, often a word or part of a word. As a rough guide, 1,000 tokens is about 750 English words.
You pay for:
- Input tokens: everything you send, including system instructions, conversation history, retrieved documents and the question.
- Output tokens: everything the model writes back. These are usually priced several times higher than input tokens.
cost per call ≈ (input tokens × input price) + (output tokens × output price)
Six ways to cut cost#
| Technique | How it saves |
|---|---|
| Right-size the model | Use a small, cheap model for simple tasks (classification, extraction, routing) and keep the most capable model for genuinely hard reasoning |
| Trim the prompt | Remove repeated instructions, unused history and irrelevant context |
| Retrieve less, retrieve better | In RAG, send the 3 best chunks instead of 15 mediocre ones |
| Prompt caching | Many providers discount repeated prompt prefixes (like a long system prompt) |
| Batch processing | For non-urgent jobs, batch APIs are typically much cheaper than real-time calls |
| Limit output length | Ask for concise formats and set a maximum output token limit |
Model routing in practice#
def answer(question):
if is_simple(question): # e.g. FAQ lookup, yes/no, classification
return call_model("small-fast-model", question)
return call_model("large-capable-model", question)
Even a simple rule-based router can cut costs significantly, because most real traffic is easy.
Measure before optimising#
Log tokens and cost per feature and per request. You'll usually find that one or two prompts account for most of the bill, and those are where optimisation pays off.
Try it
Your task · about 10 minutes
Take one prompt you use often, count its tokens with a tokenizer tool, then rewrite it to be 40% shorter without losing quality. Compare the cost per 1,000 calls.
Use the interactive visual above to explore it step by stepRecap & next
Key takeaways
- LLM APIs bill per token, for both input and output.
- Use smaller, cheaper models for simple tasks and reserve frontier models for hard ones.
- Trim prompts, cache repeated context and batch requests where the provider supports it.
- Compare providers and model versions regularly; prices and quality change often.
Quick check
2 questions · instant feedback
Go deeper
The full course on GenAI & LLMs
Udemy course
Claude Ecosystem - AI, Code, Cowork, M365 & Certification
View course (opens Udemy in a new tab)
Finished this lesson?
Mark it complete to track your progress and keep your streak going.