New cohort: Agentic AI & LLM Engineering remote internship. Limited seats.Apply now
Lesson 01 of 21 20 minFree

Introduction to Generative AI and Large Language Models

What generative AI is, how large language models turn text into tokens and predict the next one, and what that means for the apps you will build.

By the end of this lesson you will be able to:

  • Explain what generative AI is and how it differs from older AI systems.
  • Describe how a large language model (LLM) reads text as tokens and predicts the next one.
  • Use temperature to control how creative or predictable a model is.
  • Name real tasks where LLMs help, and where they can go wrong.

Our story: building an AI study buddy

Throughout this course we will build features for an imaginary education startup. Its mission: give every student in India a patient tutor that can explain any topic, at any hour, in simple language. Teachers are overloaded, coaching is expensive, and students often hesitate to ask "basic" questions in class. A well-built AI assistant can help with all three.

To build that responsibly, we first need to understand what these models actually do. That is what this lesson is about.

From rules to generation: a very short history

AI is not new. The first chatbots in the 1960s, like ELIZA, matched keywords and replied from a fixed list of templates. They felt clever for a minute, then fell apart. In the 1990s and 2000s, machine learning took over: instead of writing rules, we trained statistical models on examples. That gave us spam filters, product recommendations and much better voice assistants.

The big leap came in 2017 with the Transformer architecture. Transformers can look at a long piece of text at once and learn which words matter to each other. Train a very large Transformer on a huge amount of text and you get a large language model, a model that can generate new text, code and more. That is why we call this wave generative AI.

Old AI vs generative AI. A classic spam filter answers one fixed question: "spam or not spam?". A generative model can write the email, summarise it, translate it to Telugu, or explain why it looks like spam, all from plain-language instructions.

Step 1: text becomes tokens

An LLM never sees letters or words the way we do. Its first step is a tokenizer, which cuts text into small pieces called tokens and gives each piece a number. Common words are usually one token. Rare words are split into several. Text in Hindi, Telugu or Tamil often needs more tokens than the same sentence in English, because the tokenizer saw less of those languages while it was built.

Type in the box below and watch your text get split.

Why tokens matter to you as a builder: API pricing, speed and the model's context window (how much it can read at once) are all measured in tokens, not words. A rough rule for English: 1 token ≈ 4 characters ≈ ¾ of a word.

Step 2: predict the next token

Here is the surprising core idea: an LLM is trained to do one thing, predict the next token. Given "The capital of India is", it calculates a probability for every token it knows. " New" gets a very high score, " Mumbai" a low one. It picks a token, adds it to the text, and repeats. Answers, essays and code are all produced this way, one token at a time.

Because the model learned from enormous amounts of text, predicting the next token well forces it to pick up grammar, facts, reasoning patterns and even coding style. Nobody programmed those skills in directly. They emerge from the training.

Step 3: choosing the token, and temperature

The model does not always pick the most likely token. It samples from the probabilities, which is why you can get different answers to the same question. A setting called temperature controls this:

  • Low temperature (0–0.3): the top choice wins almost every time. Good for facts, data extraction and code.
  • Medium (0.7–1.0): balanced, natural-sounding text.
  • High (above 1.2): surprising and creative, but more likely to go off track.

Try it: generate a sentence, then move the slider and watch the probability bars flatten or sharpen.

What can LLMs do?

For our study-buddy startup, a single model can already help with many jobs:

  • Explain and summarise: "Explain Newton's third law to a Class 8 student with a cricket example."
  • Generate practice material: five MCQs on the French Revolution, with answers.
  • Give feedback: point out grammar mistakes in a student's essay, kindly.
  • Translate and simplify: turn a dense English paragraph into simple Hindi.
  • Write and explain code: help a student understand why their Python loop never ends.
  1. Explain photosynthesis like I'm 12, in 3 lines.
  2. Plants are like tiny solar-powered kitchens 🌱. They take in sunlight, water from the soil and carbon dioxide from the air. Using sunlight's energy, they turn these into sugar (their food) and release oxygen for us to breathe.
  3. Now give me one quick quiz question on it.
  4. Which gas do plants release during photosynthesis? A) Carbon dioxide B) Oxygen C) Nitrogen

Limitations you must know

Next-token prediction is powerful, but it is not the same as knowing the truth. Keep these in mind from day one:

  • Hallucinations: a model can state wrong facts confidently, because a fluent-sounding answer can still be a wrong one. Always verify facts, figures and citations.
  • Knowledge cut-off: a model only knows what was in its training data, up to a certain date. Later lessons show how to give it fresh information (RAG).
  • Not deterministic: the same prompt can give different answers. Lower the temperature when you need consistency.
  • Bias: models learn from human text, including its biases. Lesson 3 covers how to build responsibly.

Rule of thumb: treat an LLM like a brilliant, fast intern. Great for drafts and ideas, but check its work before it reaches a student or customer.

Hands-on: build a tiny next-word predictor

Real LLMs use billions of parameters, but the core idea fits in a few lines of Python. The code below counts which word follows which in a small piece of text, then predicts the most likely next word. Press Run. Python runs right here in your browser, with nothing to install.

Your task: run the code, then complete predict_next() so it returns the word that most often follows the given word. The expected output line is Most likely word after 'students' is: learn.

from collections import Counter, defaultdict

text = """students learn faster with practice . students learn best by building projects .
teachers help students learn . students ask questions . good teachers answer questions"""

words = text.split()

# Count which word comes after each word
following = defaultdict(Counter)
for current, nxt in zip(words, words[1:]):
    following[current][nxt] += 1

print("Words seen after 'students':", dict(following["students"]))

def predict_next(word):
    # TODO: return the most common word that follows `word`
    # Hint: following[word].most_common(1) gives [(word, count)]
    return "?"

print("Most likely word after 'students' is:", predict_next("students"))
Show solution
def predict_next(word):
    return following[word].most_common(1)[0][0]

Your task: this is the actual formula models use to apply temperature to their scores (called a softmax). Run it and compare the probabilities at 0.2, 1.0 and 2.0. Then change the scores and see what happens.

import math

scores = {" biryani": 2.0, " dosa": 1.5, " paneer": 1.1, " pizza": 0.2}

def probabilities(scores, temperature):
    scaled = {w: s / temperature for w, s in scores.items()}
    total = sum(math.exp(v) for v in scaled.values())
    return {w: math.exp(v) / total for w, v in scaled.items()}

for t in (0.2, 1.0, 2.0):
    probs = probabilities(scores, t)
    pretty = ", ".join(f"{w.strip()} {p:.0%}" for w, p in probs.items())
    print(f"T={t}: {pretty}")

Check your understanding

What is the one task an LLM is trained to do?

  1. Search the internet for answers
  2. Predict the next token
  3. Look up facts in a database
  4. Translate between languages

LLMs are trained to predict the next token. Skills like answering, translating and coding emerge from doing that very well on huge amounts of text.

You are building a feature that extracts dates from exam notices and must give the same answer every time. Which temperature fits best?

  1. 1.8
  2. 1.0
  3. 0.1
  4. Temperature doesn't matter here

Low temperature makes the model choose the most likely token almost every time, which is what you want for extraction and consistency.

Why does the same sentence often use more tokens in Hindi than in English?

  1. The tokenizer saw less Hindi text, so it splits Hindi into smaller pieces
  2. Hindi sentences are always longer
  3. Models don't support Hindi
  4. Tokens are counted per letter in Hindi only

Tokenizers learn their pieces from training data. Languages with less data get split into smaller pieces, so they use more tokens and cost more.

A model confidently gives a wrong date for a historical event. What is this called?

  1. Overfitting
  2. Tokenization
  3. Temperature drift
  4. Hallucination

A hallucination is fluent, confident output that is factually wrong. Always verify important facts.

Key takeaways

  • Generative AI creates new content. LLMs do it by predicting the next token, over and over.
  • Text is split into tokens, and cost, speed and context limits are all measured in tokens.
  • Temperature trades predictability for creativity.
  • LLMs can hallucinate and have a knowledge cut-off, so verify before you trust.

Next, we will compare different models (big and small, open and closed) so you can pick the right one for a job.

Your progress is saved in this browser.

Adapted from Lesson 1 of Microsoft's "Generative AI for Beginners" (MIT License), rewritten and extended by Aiprojects4U. View source