Lesson 8 of 12 intermediate

Token economics and model routing

কম token খরচ করাই লক্ষ্য না। ঠিক ফল পেতে মোট কত খরচ আর review লাগে, সেটা মাপো।

Guess first

Pick an answer. You will see if you were right at the end of the lesson.

Were tokens, retries and accepted results measured?

Open interactive version (quiz + challenge)

Real-world analogy

Cheap transport is not cheap if half the deliveries need to be repeated.

What is it?

Optimise cost per accepted task, including retries and human correction. Input, output and cached-token rates can differ by provider. Prices may rise or fall; never build a career plan on a guaranteed direction. Small models are candidates to evaluate, not automatically the right answer.

Real-world relevance

One shared support-ticket product, three optional entry points. Frontend: Compare a deterministic form validator with an unnecessary model call before adding AI. Backend: Compare routing candidates on the same labelled requests, accounting for retries. Mobile: Measure user-visible latency and server inference cost; do not assume on-device inference is free.

Key points

Quick check

Answer from memory — scrolling up first makes it stick less.

What artifact demonstrates this lesson?

Which statement is defensible?

Code example

def cost_usd(input_tokens, output_tokens, input_per_million, output_per_million):
    values = (input_tokens, output_tokens, input_per_million, output_per_million)
    if any(v < 0 for v in values):
        raise ValueError("negative usage or rate")
    return (input_tokens * input_per_million +
            output_tokens * output_per_million) / 1_000_000

Line-by-line walkthrough

  1. 1. Python: rates are explicit inputs rather than guessed current prices. This basic formula excludes cache-specific pricing and other fees; add those categories when your provider reports them.

Spot the bug

We saved 80% because the prompt has 80% fewer characters
Need a hint?
Were tokens, retries and accepted results measured?
Show answer
Report actual usage and acceptance rate on the same test set; do not convert character savings into cost claims.

Explain like I'm 5

কম token খরচ করাই লক্ষ্য না। ঠিক ফল পেতে মোট কত খরচ আর review লাগে, সেটা মাপো।

Fun fact

A useful artifact for this lesson: Use deterministic parsing where the rules are clear.

Hands-on challenge

Choose ONE stack; you do not need to build all three. Junior foundation: Calculate cost from supplied usage and rates; mark missing data unavailable. Senior extension: Compare accepted-task cost, retry rates, latency and review time before changing routing. Evidence: save the input, expected behaviour, actual result and one failure or limitation. Junior means basic coding and Git, not zero programming experience.

More resources

Open interactive version (quiz + challenge) Download the whole course as PDF / Word

Lock it in

← Back to course: AI-Assisted Software Engineering