Generative AI · Intermediate level

Intermediate level · Module 3: Tokens, Embeddings and Semantic Search · Lesson 1 of 3

Tokens, Limits and Budgets

Learners count tokens with tokenisers, compare languages and estimate how prompts and documents fit into context windows and budgets.

In this lesson you will

  • Count tokens programmatically
  • Plan prompts within context and budget limits

Key concepts

tokenizertoken countcontext windowlanguage differencesbudget

The lesson

Each model family has its own tokenizer; AutoTokenizer.from_pretrained(name) loads it, and len(tokenizer(text).input_ids) gives a count. Hosted APIs report token usage in their responses.

Text in languages rarely seen during training splits into more tokens, which costs more and uses more of the context window.

Plan budgets: instructions + retrieved passages + question + answer must fit in the context window, and total tokens × price must fit the budget.

Step by step

  1. Count tokens for an instruction, a document and an answer.
  2. Compare the same sentence in two languages.
  3. Calculate how many passages fit a 4,000-token budget.
  4. Write a budget rule for the project.

Practice activity

Write a token budget for the project assistant.

Expected result: A realistic budget showing instructions, passages and answer within limits.

Check your understanding

  1. How do you count tokens for an open model?

    Show answer

    Load its tokenizer and count the input IDs.

  2. Why might a local-language sentence cost more?

    Show answer

    It splits into more tokens.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.