Building an AI Learning Assistant

Updated · 7 min read
mediumPython 3.13aiapplicationsproductpythontutoring

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

StepThe AI Learning Assistant
1. ProblemStudents struggle with complex concepts, and help isn't available at 10 p.m. before a test
2. SolutionAn 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. UsersSchool students, college students and working professionals
4. FeaturesFirst 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.

PartDescription
TutorReplyIt is used to define the four fields: explanation, example, quiz question and quiz answer
LEVELSIt 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.

assistant.py
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.

Output
[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

The real first version, in a browser

Steps 6 and 7: Evaluate and Improve

MeasureWhat it tells you
Answer accuracyAre explanations correct? Check a sample against trusted sources
User satisfactionThumbs up and down on each answer
Response timeHow long students wait
Learning improvementDo quiz scores rise over time?
Cost per interactionTokens 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:

  1. What problem does the AI Learning Assistant solve?
  2. Which features are in the first version?
  3. Why does explain() return token counts?

Quick check: building the assistant

4 questions
1What changes between difficulty levels in the code?
2Which feature waits for a later version?
3Why return the quiz answer as its own field?
4What should the assistant not do?

Key Takeaways

Quick reference

StepThe assistant
ProblemHard concepts, no help at night
UsersSchool, college, professionals
First featuresAsk, level, example, quiz, rating
Core codeexplain(topic, level)

Was this tutorial helpful?

Lesson 184 of 186