Please enable JavaScript to use CodeHS

AI Explained

Reasoning and grounding

Language models are great at patterns but can make confident mistakes. Getting them to reason and stay grounded in facts is one of the hardest problems in AI.

AI ~12 min High school
The big idea

Chain-of-thought reasoning

When people solve hard problems, they usually write down intermediate steps. Language models do better, too, when they are prompted to “think step by step.” This is called chain-of-thought (CoT) reasoning.

Prompt: A bat and a ball cost $11 total. The bat costs $10 more than the ball. How much is the ball?

Model with CoT:
- Let the ball cost x dollars.
- Then the bat costs x + 10 dollars.
- Total: x + (x + 10) = 11
- 2x + 10 = 11
- 2x = 1
- x = 0.50
- The ball costs 50 cents.

By generating the reasoning steps, the model is more likely to reach the right answer. Newer reasoning models take this further: they can spend more compute “thinking” before answering, explore multiple paths, and refine their own reasoning.

Core idea: asking the model to show its work can dramatically improve accuracy on math, logic, and multi-step questions.


Limits

Hallucination

A language model is trained to predict the next token. Its objective is to produce plausible-sounding text, not necessarily true text. When it makes up facts, dates, quotes, or sources, that is called a hallucination.

Hallucinations are not bugs in the usual sense. They are a consequence of the model’s design: it generates language, not a database. Even when it is right most of the time, it can be confidently wrong about details.

Key warning: always verify important facts from an AI, especially for medical, legal, or technical decisions.


Fixes

Grounding and retrieval

One way to reduce hallucinations is to ground the model in real sources. Retrieval-Augmented Generation (RAG) works by searching a trusted database or document collection, then feeding the relevant snippets into the prompt. The model still generates text, but it is asked to use the provided context.

Another approach is to give the model tools: a calculator, a code interpreter, a search engine, or a database query. When the model needs a fact, it can call a tool instead of guessing.

rag.py
question = "What is our school's phone number?"
context = search_school_database(question)
answer = llm.generate(
    f"Use the following context to answer.\n
    Context: {context}\n
    Question: {question}"
)

Interactive

Grounding game

Read the source paragraph, then decide whether each claim is supported by it or a hallucination.

Source: The Ada High School Robotics Club won first place at the regional tournament on Saturday. The team, led by captain Mia Chen, built a robot named “Sparky” that sorted colored balls in under 30 seconds. The competition took place at Westbridge Community Center. Coach Ramirez said the team will travel to the state finals in March.
Score: 0 / 0

Check yourself
?

Why does chain-of-thought help?

Imagine asking a model to solve a long math problem in one sentence. What could go wrong, and how does showing intermediate steps fix it?

One big answer is easier to get wrong than many small steps. Breaking the problem into parts lets the model check each part and catch errors before the final answer.


Quick quiz

Three questions

1. What is a hallucination in an LLM?

2. What does RAG do?

3. Why is chain-of-thought useful?

Keep learning

Language models started with text, but they are quickly expanding to images, audio, video, and the world itself. The next tutorial explores multimodal AI.

Part of the AI Explained tutorial series.