Back to website
Mayank Aggarwal Learning Center

Phase 05 · GenAI & LLM ApplicationsLesson 3 of 3

Cutting AI API Costs

Keep LLM features affordable as usage grows.

  • 23:25 video
  • 2 min read
  • Intermediate
01

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.

02

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.
Text
cost per call ≈ (input tokens × input price) + (output tokens × output price)

Six ways to cut cost#

TechniqueHow it saves
Right-size the modelUse a small, cheap model for simple tasks (classification, extraction, routing) and keep the most capable model for genuinely hard reasoning
Trim the promptRemove repeated instructions, unused history and irrelevant context
Retrieve less, retrieve betterIn RAG, send the 3 best chunks instead of 15 mediocre ones
Prompt cachingMany providers discount repeated prompt prefixes (like a long system prompt)
Batch processingFor non-urgent jobs, batch APIs are typically much cheaper than real-time calls
Limit output lengthAsk for concise formats and set a maximum output token limit

Model routing in practice#

Python
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.

03

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 step
04

Recap & next

Key takeaways

  1. LLM APIs bill per token, for both input and output.
  2. Use smaller, cheaper models for simple tasks and reserve frontier models for hard ones.
  3. Trim prompts, cache repeated context and batch requests where the provider supports it.
  4. Compare providers and model versions regularly; prices and quality change often.

Quick check

2 questions · instant feedback

1LLM API usage is usually billed by…
2A good cost strategy is to…

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next