Building an AI Learning Assistant
This lesson applies the seven-step method from "The Building Blocks of an AI Application" to a real product idea, the AI Learning Assistant, and builds its core: a tutor function that explains any topic at three levels, with an example and a quiz question.
Steps 1 to 4: Problem, Solution, Users, Features
| Step | The AI Learning Assistant |
|---|---|
| 1. Problem | Students struggle with complex concepts, and help isn't available at 10 p.m. before a test |
| 2. Solution | An AI tutor that explains a topic at the right level, gives an example and checks understanding with a quiz question; it doesn't do homework for them |
| 3. Users | School students, college students and working professionals |
| 4. Features | First version: ask about a topic, choose a difficulty, get an example and a quiz question, rate the answer. Later: summaries, progress tracking, longer quizzes |
Step 5: The Tutor Function
The tutor function is the part that talks to the model. It uses structured output (see "Structured Output and JSON") so the app always gets four fields it can display separately.
| Part | Description |
|---|---|
TutorReply | It is used to define the four fields: explanation, example, quiz question and quiz answer |
LEVELS | It is used to change the instructions for each difficulty |
explain(topic, level) | It is used to call the model and return the reply and the tokens used |
Save this as assistant.py; the next lesson's backend imports it.
from typing import Literal
from pydantic import BaseModel
from model_api import ModelClient
Level = Literal["beginner", "intermediate", "advanced"]
class TutorReply(BaseModel):
explanation: str
example: str
quiz_question: str
quiz_answer: str
LEVELS = {
"beginner": "Explain for a school student: everyday words, no jargon, one short analogy.",
"intermediate": "Explain for a college student: use the correct terms and define each one briefly.",
"advanced": "Explain for a working professional: precise and concise.",
}
SYSTEM = ("You are the tutor in an AI Learning Assistant. {level} Keep the explanation under 60 words. "
"Give one concrete example and one quiz question with its answer. If you're not sure, say so.")
client = ModelClient()
def explain(topic: str, level: Level):
"""Return a TutorReply (or None if no valid reply) and the tokens used."""
tutor_reply, reply = client.parse(
[{"role": "user", "content": f"Explain: {topic}"}],
TutorReply,
system=SYSTEM.format(level=LEVELS[level]),
max_tokens=1000,
)
usage = {"input_tokens": reply.input_tokens, "output_tokens": reply.output_tokens}
return tutor_reply, usage
if __name__ == "__main__":
for level in ["beginner", "advanced"]:
reply, usage = explain("photosynthesis", level)
print(f"[{level}] {reply.explanation}")
print(" Example:", reply.example)
print(" Quiz:", reply.quiz_question, "->", reply.quiz_answer)
print(" Tokens:", usage)Example output. The explanations, examples and quiz questions were produced by an AI model on November 16, 2026, to show how the levels differ, and the token counts are examples too. The program ran end to end against a local test server that returned them, not a live API.
[beginner] Plants make their own food using sunlight. Like a kitchen that runs on solar power, leaves take in water and carbon dioxide and use light to turn them into sugar, giving off oxygen.
Example: A sunflower turning toward the sun to catch more light for making food.
Quiz: What gas do plants give off during photosynthesis? -> Oxygen
Tokens: {'input_tokens': 182, 'output_tokens': 96}
[advanced] Photosynthesis converts light energy into chemical energy: chlorophyll drives the light reactions, producing ATP and NADPH, which power the Calvin cycle's fixation of CO2 into glucose; O2 is released from water splitting.
Example: Crop yield models use photosynthetic rate per leaf area to predict growth under different light levels.
Quiz: Which molecule is the source of the oxygen released in photosynthesis? -> Water
Tokens: {'input_tokens': 176, 'output_tokens': 104}Explanation: The same topic gets very different answers because only one sentence of the system prompt changes with level. client.parse() returns a TutorReply, so the app can show the explanation, the example and the quiz in separate places, and hide the quiz answer until the student tries. The function also returns the token counts, which the app stores to track cost.
The First Version, Running
Steps 6 and 7: Evaluate and Improve
| Measure | What it tells you |
|---|---|
| Answer accuracy | Are explanations correct? Check a sample against trusted sources |
| User satisfaction | Thumbs up and down on each answer |
| Response time | How long students wait |
| Learning improvement | Do quiz scores rise over time? |
| Cost per interaction | Tokens per answer × price |
The next two lessons build the app around the tutor function and then use these measures to improve it.
Common Mistakes
Building every feature at once. Start with ask, choose a level, and rate.
Doing the student's homework. Explain and quiz; don't hand over finished assignments.
Not storing token counts. Without them, you can't know what each answer costs.
Interview Corner
How would you scope the first version of an AI product?
Define the problem and users, then choose the smallest feature set that tests whether the AI actually helps, with a way to collect feedback. Everything else waits for evidence.
Why use structured output in a tutoring app?
So the app gets separate, predictable fields (explanation, example, quiz question, answer) to display in the right places, such as hiding the answer until the student tries.
Practice
Exercise: add a level. Add an "expert" level to Level and LEVELS for graduate students. Run explain() for "compound interest" at all four levels and compare the explanations' length and vocabulary.
Check your understanding:
- What problem does the AI Learning Assistant solve?
- Which features are in the first version?
- Why does
explain()return token counts?
Key Takeaways
Quick reference
| Step | The assistant |
|---|---|
| Problem | Hard concepts, no help at night |
| Users | School, college, professionals |
| First features | Ask, level, example, quiz, rating |
| Core code | explain(topic, level) |
Was this tutorial helpful?