What Is Generative AI? A Beginner’s Guide with Interactive Demos
How language models predict text, and how to use them wisely
Generative AI can write text, explain code and create images in seconds, but how does it actually work, and why does it sometimes get things confidently wrong? In this lesson you will experiment with a tiny language model, see how text is cut into tokens, build a good prompt and learn when to trust the result.

Learning objectives
- Define generative AI and give examples of what it can create
- Explain that a language model repeatedly predicts the next token
- Describe what tokens and the context window are
- Explain the effect of temperature on generated text
- Write a specific prompt with role, task, context, format and constraints
- Recognize hallucinations and decide when to verify an answer
Generative AI can write text, explain code, create images and answer questions in seconds. But how does it actually work, and why does it sometimes get things confidently wrong? In about 25 minutes you will experiment with a tiny language model, see how text is cut into tokens, build a good prompt and learn when to trust the result. A self-graded quiz closes the lesson.
- Time: 25 minutes
- Level: beginner
- Prerequisites: none
- You need: a web browser
1. What is generative AI?
Generative AI is a family of artificial intelligence systems that create new content: text, code, images, audio or video. Instead of only sorting or recognizing existing data (is this email spam?), they produce something new that resembles what they learned from.
The assistants that chat with you are built on language models. A language model is a program trained on huge amounts of text and code to learn the patterns of language. Three ideas are enough to understand most of what follows:
- Training: the model learns patterns from a very large collection of examples.
- Prompt: you give it instructions or a question, written in everyday language.
- Generation: it produces an answer one small piece at a time.
2. How it works: predicting the next word
At its core, a language model answers one question again and again: given the text so far, what comes next? It assigns a probability to each possible continuation, picks one, adds it to the text, and repeats.
In mathematical notation, the probability of a whole text is built from these step-by-step predictions:
P(w1, …, wn) = P(w1) × P(w2 | w1) × … × P(wn | w1, …, wn-1)
Real models are enormous neural networks. To see the idea with your own eyes, the demo below uses a toy model: it only counts which word follows which in a few short sentences. It is far simpler than a real language model, but the loop is the same: look at the text, compute probabilities, pick the next word.
Start word: Temperature: 1.0
Text so far:
Experiment: set the temperature to 0.2 and generate several sentences: the output becomes very repetitive, because the most frequent word almost always wins. Now set it to 2.0: unlikely words get a real chance and the text becomes more varied, but also less coherent.
What is temperature?
Temperature controls how much randomness is used when picking the next word. The probabilities come from a formula called softmax, where T is the temperature:
pi = ezi/T / Σj ezj/T
Low temperature gives predictable, focused answers (good for code and facts). High temperature gives more creative, varied answers (good for brainstorming).
3. Tokens: how text is cut into pieces
Models do not read letters or whole words. They read tokens: small chunks of text such as a short word, part of a longer word, a number or a punctuation mark. The length of a prompt and of an answer is counted in tokens, and every model can only handle a limited number of them at once: its context window.
Type a sentence:
This splitter is a simplified imitation: real tokenizers learn their own pieces from data, so the exact cuts differ from one model to another. The lesson is still valid: long or rare words are cut into several tokens, and common short words are often a single token.
4. Prompts: how to ask well
A prompt is the instruction you give to the model. A vague prompt gets a vague answer. Compare:
- Vague: "Explain loops."
- Specific: "You are a patient Python tutor. Explain for-loops to a complete beginner who knows variables. Use two short examples and finish with one exercise. Keep it under 200 words."
A strong prompt usually contains five ingredients: a role, a task, some context, an output format and constraints. Build one below and watch the checklist.
Role:
Task:
Context:
Output format:
Constraints:
Your prompt:
5. Limits: when to trust and when to verify
Because a model generates plausible text rather than retrieving verified facts, it can make mistakes that sound completely convincing. Know the main risks:
- Hallucinations: confident statements that are false or unsupported, including invented numbers, quotes or sources.
- Bias: the model reflects patterns, and sometimes prejudices, present in its training data.
- Outdated knowledge: a model knows only what it saw during training, unless it is connected to up-to-date sources.
- Privacy: never paste passwords, API keys, or other people's personal data into a tool unless you are sure how it is handled.
Rule of thumb: the higher the cost of being wrong, the more you must verify. Try these cases:
6. Using generative AI from code
Most providers offer an API so that your own programs can send a prompt and receive a generated answer. Here is the shape of such a call in Python (you need an account and an API key, so this example is for reading):
import anthropic # pip install anthropic
client = anthropic.Anthropic() # reads your API key from an environment variable
reply = client.messages.create(
model="MODEL_NAME", # pick a model from the provider's documentation
max_tokens=300,
messages=[{"role": "user", "content": "Explain variables in Python in two sentences."}],
)
print(reply.content[0].text)
Notice the same ideas as before: a prompt goes in, a limit on the number of tokens is set, and generated text comes out. If you have not written Python yet, start with Write Your First Python Program.
Generated code deserves the same caution as generated text: read it, run it, and test it before you rely on it.
7. Practice exercises
Complete each sentence, then check your answer. The feedback explains the solution.
8. Final quiz: test what you learned
Ten questions, one correct answer each. You get instant grading and every answer is explained.
Key takeaways
- Generative AI creates new content (text, code, images, audio) from patterns learned in data.
- A language model repeatedly predicts the next token, then adds it to the text.
- Tokens are pieces of text, and the context window limits how many the model can handle at once.
- Temperature controls randomness: low for focused answers, high for variety.
- A good prompt has a role, a task, context, a format and constraints.
- Models can hallucinate: verify anything important, and test any generated code.
Frequently asked questions
Is generative AI the same thing as artificial intelligence?
No. Artificial intelligence is the broad field. Generative AI is one branch of it, focused on creating new content. Other branches classify data, make predictions or control robots.
Why does generative AI sometimes make things up?
Because it generates the most plausible continuation of the text, not a verified fact. When it lacks reliable information, it can still produce a fluent but false answer. This is called a hallucination.
Do I need to know programming or math to use it?
No. You can use chat assistants with everyday language. Programming becomes useful when you want to connect a model to your own applications, and basic math helps you understand how it works.
Is it safe to paste private information into an AI tool?
Avoid sharing passwords, API keys, financial details or other people's personal data unless you have checked how the tool stores and uses your data.
Can I trust code written by AI?
Treat it like code from a stranger: read it, run it and test it. It is often a great starting point, but it can contain bugs or security problems.
Try the prompt builder again with a real question of your own, and compare the answer you get with a vague version of the same question. To go further, explore the other lessons and learn the coding basics in Write Your First Python Program.
Definitions
Generative AI
A family of AI systems that create new content such as text, code, images or audio.
Language model
A program trained on large amounts of text and code that predicts which token is likely to come next.
Token
A small piece of text, such as a short word, part of a word, a number or a punctuation mark.
Prompt
The instruction or question given to a generative model.
Temperature
A setting that controls how random the choice of the next token is: low is focused, high is varied.
Hallucination
A fluent but false or unsupported statement produced by a generative model.
Context window
The maximum number of tokens a model can consider at once, prompt and answer included.
Common mistakes to avoid
- Believing the model looks up answers in a database instead of generating plausible text
- Writing vague prompts and expecting precise answers
- Trusting statistics, quotes and references without checking them
- Using AI-written code without reading and testing it
- Pasting passwords, API keys or personal data into a tool
- Writing an API key directly in public code
Key takeaways
- Generative AI creates new content from patterns learned in data
- Tokens are pieces of text and the context window limits how many fit
- Temperature controls randomness: low for focus, high for variety
- Models can hallucinate: verify important claims and test generated code