Skip to content
SamplePreview build: teacher profiles, ratings, reviews and lesson counts are sample data.
All tutors

Token Counting and Cost Control

Count tokens, predict what a feature will cost and cut spending without hurting answer quality.

By Farid Haddad Beginner Building with LLM APIs 4.3(3) 57 lessons taught Sample

Your first 3 replies from the tutor are free. Then you can continue the lesson for $4 from your credit.

A taste of a lesson

One question and the answer, as this tutor gives it.

Token Counting and Cost Control AI tutor following Farid Haddad's plan
Student:

My chatbot's cost per message keeps rising during long conversations. Why?

Tutor:

Because the API is stateless, your app resends the whole conversation with each new message. Turn 1 sends the system prompt plus one message; turn 20 sends the system prompt plus nineteen earlier exchanges plus the new one. Input tokens grow every turn, so cost per message grows too. Fixes: keep only the most recent turns within a token budget, summarise older turns, and cap reply length. Check your logs: how many input tokens did turn 1 and turn 20 use?

Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.

What you will be able to do

  • Explain what tokens are and why counts vary by language and content
  • Count tokens before a call and read real usage after it
  • Estimate cost per request, per user and per month
  • Apply the main cost levers in order of impact while protecting quality
  • Set up usage tracking, alerts and spending caps

Lesson plan

6 lessons. Pick one to start there.

  1. 1 What a token is Understand tokens and why the same text has different counts in different settings. Start
  2. 2 Counting before and after Measure tokens with a tokenizer before calling and from usage after. Start
  3. 3 From tokens to money Calculate cost per request, per user and per month with example prices. Start
  4. 4 Where the tokens go Break a real request down to find the biggest token consumers. Start
  5. 5 Cutting cost without cutting quality Apply cost levers and confirm quality holds with a small test set. Start
  6. 6 Budgets, alerts and caps Put guardrails in place so costs stay inside your plan. Start

Try asking

Tap a question to start a lesson with it.

About this tutor

For beginners building with model APIs who want to know what their app will cost before the bill arrives. You learn what tokens are, why counts differ across languages and content types, how to count them before a call and read actual usage after it, and how to turn counts into cost per request, per user and per month. Then you work through the main levers in order of impact: shorter prompts, trimmed history, output limits, smaller models for easy tasks, caching and batch processing. Every lesson uses made up example prices so the method stays valid when real prices change.

Reviews

4.3

3 ratingsSample

  • Anika R.Sample

    The breakdown of where tokens go showed our examples block was bigger than everything else combined. Trimmed it and quality held on our test set.

  • Tobias E.Sample

    Clear explanation of why our German and Polish users cost more. Practical and not preachy.

  • Gustavo L.Sample

    Built a cost table for my feature in the second lesson. Using made up prices and then plugging in real ones made the method stick.

About the teacher

Farid Haddad

Keeps LLM features fast, affordable and available as traffic grows: cost, caching, retries and observability

9 tutors 4.5(19) 347 lessons taught Sample

Most of my working life has been on platform and reliability teams, and these days I spend it on LLM features: the bills that surprise people, the 429 errors on launch day, the logs nobody can read. I teach the operational side of building with model APIs. We estimate costs before writing code, add retries that do not make outages...

See Farid's profile and tutors