⚡0 XP🔥0

How Large Language Models Work

Lesson 2 of 19 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz

A simple explanation of LLMs - tokens, next-token prediction, training, context windows, temperature, knowledge cutoff - and why these facts change how you write prompts.

What is an LLM?#

A Large Language Model (LLM) is an AI system trained on a huge amount of text - books, websites, code, articles - to understand and generate human-like language. ChatGPT (OpenAI), Claude (Anthropic) and Gemini (Google) are all built on LLMs.

You don't need maths to use them well, but knowing how they work explains almost every prompt-writing rule in this course.

1. Tokens - how the model reads text#

Models don't read letters or whole words. They split text into tokens - common words, parts of words, punctuation.

"Prompt engineering is useful!"
→ ["Prompt", " engineering", " is", " useful", "!"]      (about 5 tokens)

"unbelievable"
→ ["un", "believ", "able"]                               (3 tokens)

Rules of thumb for English:

  • 1 token ≈ 4 characters ≈ ¾ of a word
  • 100 tokens ≈ 75 words
  • 1 page of text ≈ 500-700 tokens
Did you know?

Hindi and other Indian languages usually need more tokens per word than English. That matters because AI services charge by tokens and have token limits.

2. Next-token prediction - how the model writes#

An LLM generates its answer one token at a time. At each step it asks: "Given all the text so far, what is the most likely next token?"

Input so far:   "The capital of Madhya Pradesh is"
Most likely →   " Bhopal"
Then →          "."

It repeats this hundreds of times to produce a full answer.

हिंदी में समझें

LLM असल में एक बहुत ही उन्नत "अगला शब्द अनुमान लगाने वाली मशीन" है - जैसे आपके mobile keyboard में अगला शब्द suggest होता है, लेकिन उससे लाखों गुना ज़्यादा समझदार।

यह हर बार सोचता है: "अब तक जो लिखा गया है, उसके बाद सबसे सही अगला शब्द (token) क्या होगा?" - और ऐसे एक-एक token जोड़ते हुए पूरा जवाब बनाता है।

Prompt के लिए इसका मतलब: आप जितनी साफ़ और पूरी जानकारी शुरुआत में देंगे, उतना ही सही "अगला शब्द" वह चुनेगा। धुंधला prompt = धुंधला जवाब।

What this means for your prompts: the model continues from your text. Clear, specific text in → focused, specific text out.

3. Training - where the knowledge comes from#

  1. Pre-training - the model reads billions of pages and learns grammar, facts, reasoning patterns and coding.
  2. Fine-tuning - it is trained further on examples of helpful conversations, and with human feedback, to follow instructions and be safe.

Knowledge cutoff#

The model's built-in knowledge stops at the date its training data ends - its knowledge cutoff. It may not know recent news, new laws, the latest prices or new software versions - unless the tool can search the web, or you paste the information into the prompt.

Tip

For anything recent or specific to your organisation (current GST rates, your company policy, this year's syllabus), paste the source text into the prompt and tell the model to answer from it.

4. The context window - the model's working memory#

The context window is the maximum number of tokens the model can look at in one go - your prompt, any documents, the conversation history and its own reply. Modern models have large windows (hundreds of pages), but:

  • Anything outside the window is simply not seen.
  • In very long chats, early details can get less attention. If something is important, repeat it or start a fresh chat with a summary.
  • The model has no memory between separate chats unless the app has a memory feature.

5. Temperature - creativity vs consistency#

Many AI tools and APIs have a temperature setting:

Temperature Behaviour Good for
Low (0 - 0.3) focused, predictable, consistent facts, code, data extraction, classification
Medium (0.5 - 0.7) balanced general writing, explanations
High (0.8 - 1.0+) varied, creative, surprising brainstorming, stories, slogans

That's why the same prompt can give slightly different answers each time.

6. Why models make mistakes (hallucinations)#

Because the model generates plausible-sounding text, it can sometimes produce confident but wrong statements - fake statistics, non-existent book titles, incorrect dates, made-up legal sections. This is called a hallucination.

Hallucinations are more likely when:

  • the question is about obscure or very recent facts,
  • the prompt asks for exact numbers, quotes or references from memory,
  • the model is pushed to answer even when it doesn't know.
Common Mistake

Always verify important facts, numbers, citations, legal and medical information from a reliable source. You'll learn prompts that reduce hallucinations in later lessons (e.g. "answer only from the given text" and "say 'I don't know' if unsure").

What this means for prompting - summary table#

How LLMs work So in your prompt you should...
They predict from the text you give Be specific; give context and details
They have a knowledge cutoff Paste recent/internal information yourself
They see only the context window Keep important info in the current chat; summarise long chats
Output varies (temperature) Ask for a fixed format; use low temperature for factual tasks
They can hallucinate Ask for sources from given text, allow "I don't know", verify

Summary#

  • LLMs split text into tokens and generate answers by predicting the next token.
  • Their knowledge comes from training data and has a cutoff date.
  • The context window limits how much they can consider at once.
  • Temperature controls randomness.
  • They can hallucinate - good prompts reduce this, and you must verify important facts.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Estimate tokens

A rough rule: 1 token ≈ 4 characters of English text. Write estimate_tokens(text) that returns len(text) // 4 but at least 1 for non-empty text, and 0 for an empty string.

2

Does it fit the context window?

Write fits(prompt_tokens, max_output_tokens, context_window) that returns True if the prompt plus the expected output fit inside the context window, otherwise False.

Quick Quiz

Q1.

At its core, what does an LLM do when generating text?

Q2.

What is a token?

Q3.

What is the context window?

Q4.

What does a higher temperature setting do?

Q5.

Why can an LLM confidently state a wrong fact?