Skip to content

Repository files navigation

MathCrew 🧮

Python 3.10+CrewAIGeminiLicense: PolyForm Strict

AI-powered adaptive math tutor for kids (Grade 1–6)

A web-based math learning app for elementary students, powered by a CrewAI multi-agent pipeline with Google Gemini + Ollama hybrid architecture.

Motivation

Built as a personal project to help my daughter practice math at home, and as an experiment with CrewAI's multi-agent orchestration. What started as a simple worksheet generator evolved into a full adaptive tutoring system.

  • 4 AI agents collaborate: problem generation → feedback → error analysis → scaffolded practice
  • Problem Bank — caches generated problems to save LLM calls (reuses problems with matching conditions)
  • 3 curriculum styles: Common Core · RSM · Singapore Math
  • Gamification: XP / levels / streaks / 15 badges / confetti animations
  • Multi-student support with PIN login
  • Chart.js-powered dashboard

Screenshots

Main App — Dashboard with real learning data

LoginNew Student Setup
LoginSetup
ProblemAgent Pipeline
ProblemPipeline
Wrong Answer + ScaffoldCorrect Answer + Achievement
WrongCorrect
DashboardAchievements
DashboardAchievements

Features

FeatureDescription
AI 4-Agent PipelineManager → Creator → Helper → Analyst sequential execution
Real-time SSE UILive agent pipeline progress displayed in the browser
Adaptive ScaffoldingOn wrong answers: error analysis (computational/conceptual/procedural/careless) + auto-generated practice problems
Problem BankCaches problems by grade+style+topic, skips Creator LLM call on cache hit
Multi-CurriculumChoose from Common Core · RSM (Russian Math) · Singapore Math
GamificationXP · Levels (1–50) · Streak bonuses · 15 badges · Confetti animations
Multi-Student + PINPer-student profiles, PIN protection, independent learning history
DashboardAccuracy trends · Per-topic performance · Achievement overview (Chart.js)

Tech Stack

LayerTechnology
BackendPython 3.10+ / Starlette (async)
AI AgentsCrewAI
Primary LLMGoogle Gemini 2.5 Flash
Local LLMOllama (gemma3:4b)
DatabaseSQLite3
RealtimeServer-Sent Events (SSE)
FrontendVanilla JS / Chart.js / canvas-confetti

Prerequisites

RequirementNote
Python 3.10+Required
Gemini API KeyRequired — see setup guide below
OllamaOptional — needed for local LLM

Quick Start

# 1. Clone
git clone https://github.com/freesoft/MathCrew.git
cd MathCrew
# 2. Create & activate virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate# 3. Install dependencies
pip install "crewai[google-genai]" starlette sse-starlette uvicorn python-dotenv
# 4. Configure environment variables
cp .env.example .env # or create manually# Add GEMINI_API_KEY=your_key_here to .env# 5. Run
python web_tutor.py
# → http://localhost:8000

Gemini API Key Setup

  1. Go to Google AI Studio
  2. Click Get API KeyCreate API Key
  3. Add the key to your .env file:
    GEMINI_API_KEY=your_key_here
    
  4. Free tier limits: 15 requests/min, 1,500 requests/day (as of 2025, subject to change)

Ollama Setup (Optional)

To run the Helper agent locally for faster feedback:

  1. Install from https://ollama.com
  2. Pull the model:
    ollama pull gemma3:4b
  3. Configure in .env:
    USE_LOCAL_LLM=true # (default) Use Ollama for Helper
    USE_LOCAL_LLM=false # Use Gemini for all agents
    

If Ollama is not installed, set USE_LOCAL_LLM=false and all agents will use Gemini.

Choosing a Local Model

The default gemma3:4b is lightweight and good for simple feedback. For stronger math reasoning, consider upgrading:

ModelVRAMMath PerformanceBest For
gemma3:4b (default)~3 GBBasicSimple feedback, low-end hardware
qwen3:8b~6 GBGood8 GB GPU, significant upgrade over gemma3
qwen3:14b~10 GBStrong16 GB GPU, recommended for full local mode
deepseek-r1:14b~10 GBStrong (math-specialized)Math-heavy reasoning tasks
llama4-scout:17b~12 GBStrongNatural English, general purpose
qwen3:32b~20 GBClosest to Gemini Flash24 GB GPU (RTX 4090 etc.)

To switch models, just pull and update web_tutor.py:

ollama pull qwen3:14b
# In web_tutor.py, change the local_llm model:local_llm=LLM(model="ollama/qwen3:14b", base_url="http://localhost:11434")

Full local mode (all agents on Ollama, no data leaves your machine) is planned for a future release — ideal for schools and privacy-sensitive deployments.


AI Agent Architecture

[Manager] → [Creator] → [Helper] → [Analyst]
│ │ │ │
Gemini Gemini Ollama Gemini
▲
│
[Problem Bank] ── hit → skip Creator (saves LLM call)
AgentModelPurpose
Learning ManagerGemini 2.5 FlashAnalyzes learning history, decides next problem direction
Problem CreatorGemini 2.5 FlashGenerates grade- and topic-appropriate math problems (JSON output)
Solution HelperOllama gemma3:4bEncouraging feedback on correct/wrong answers, step-by-step explanations
Misconception AnalystGemini 2.5 FlashError analysis — computational / conceptual / procedural / careless

Why only Helper uses a local model: immediate feedback needs speed, while accurate problem generation and analysis require Gemini's accuracy.


Problem Bank

Generated problems are cached in the problem_bank table and reused when the same conditions (grade + curriculum_style + topic) are requested.

  • Regular problems: After Manager runs, extract topic → query bank → on hit, skip Creator (saves 1 LLM call)
  • Scaffold problems: Query bank → on hit, skip Creator (saves all LLM calls — no Manager for scaffolds)
  • Compares against each student's last 20 problems to avoid serving duplicates
  • Sorted by times_served ASC, RANDOM() — least-served problems are prioritized

Curriculum Styles

StyleApproach
Common CoreConceptual understanding + real-world problems, visual models (number lines, tape diagrams)
RSMLogical reasoning + algebraic thinking, 1–2 years ahead of standard curriculum
Singapore MathCPA (Concrete-Pictorial-Abstract) approach, bar models, number sense mastery

Students can select their curriculum style during profile creation or in settings. The chosen style determines per-grade scope and pedagogy injected into all agent prompts.


Environment Variables

VariableRequiredDefaultDescription
GEMINI_API_KEYYesGoogle Gemini API key
USE_LOCAL_LLMNotruetrue = use Ollama for Helper, false = use Gemini for all

Project Structure

MathCrew/
├── web_tutor.py # Web server + API routes + CrewAI agent pipeline + Problem Bank logic
├── db.py # SQLite schema + Problem Bank + gamification logic (XP/levels/achievements)
├── math_tutor.py # CLI version (standalone, runs in terminal without web)
├── templates/
│ └── index.html # Frontend SPA (Vanilla JS + Chart.js + confetti)
├── .env # API keys (gitignored)
├── .gitignore
└── README.md

Customization Guide

Changing Models

Edit LLM configuration in web_tutor.py:

# Change Gemini modelgemini_llm=LLM(model="gemini/gemini-2.5-pro", api_key=...)
# Change local model (see "Choosing a Local Model" section for recommendations)local_llm=LLM(model="ollama/qwen3:14b", base_url="http://localhost:11434")

Privacy Note

When using cloud LLMs (Gemini, OpenAI), student data — names, grade levels, answers, mistakes, and learning history — is sent to external servers. For apps used by children, this matters more than you might think.

What is COPPA? The Children's Online Privacy Protection Act is a US federal law that regulates online collection of personal information from children under 13. The 2024 amendments (fully effective 2025) strengthen requirements significantly: parental consent is opt-in by default, and operators must minimize data collection. If your child uses an AI tutor that sends their work to cloud servers, COPPA likely applies.

What is FERPA? The Family Educational Rights and Privacy Act protects student education records in K-12 schools. When a school adopts an EdTech tool, FERPA requires that student data is used only for educational purposes and not shared with third parties for unrelated use. Schools must ensure any cloud service they use has appropriate data handling agreements.

Why does local mode matter? In local mode (USE_LOCAL_LLM=true with all agents on Ollama), your child's name, grade, answers, mistakes, and learning patterns never leave your machine. There is no external data transmission, no third-party data processing, and no legal gray area. This is the simplest path to full compliance.

For school deployments:

  • Recommended: Full local mode with a capable model (e.g., qwen3:14b on 16 GB VRAM)
  • If cloud is necessary: Use only services that guarantee US data residency and do not use student data for model training. Gemini API paid tier meets both criteria (free tier data may be used for product improvement); Azure OpenAI US East/West is another option
  • See LICENSE.md for contact info regarding institutional use

Editing Curriculum

Modify the CURRICULUM_STYLES dict in web_tutor.py:

CURRICULUM_STYLES= {
"common_core": {
"display_name": "Common Core",
"pedagogy": "Focus on conceptual understanding...",
"grades": {
1: "Addition and subtraction within 20...",
# ... edit per-grade scope
},
},
# Add new styles here
}

Topic List

Update both KNOWN_TOPICS in web_tutor.py and the TOPICS array in templates/index.html.

Adding Achievements

Add a new entry to the ACHIEVEMENTS dict in db.py, then add the condition in check_achievements():

# Add to ACHIEVEMENTS"new_badge": {"name": "Badge Name", "icon": "🎖️", "desc": "Description"}
# Add condition in check_achievements()"new_badge": some_condition,

XP Formula

In db.pyget_gamification_stats():

  • Correct answer: 10 XP
  • Scaffold correct: 8 XP
  • Wrong answer: 2 XP
  • Streak bonuses: 3-streak +3, 5-streak +5, 10-streak +10, 20-streak +20

Level Formula

level=min(int(0.4*sqrt(xp)) +1, 50)

API Endpoints

MethodEndpointDescription
GET/Main page (index.html)
GET/api/studentsList all students
GET/api/studentCurrent logged-in student info
POST/api/loginPIN login
POST/api/setupCreate/update student profile
POST/api/logoutLogout
POST/api/new-problemGenerate new problem (runs agent pipeline)
POST/api/submit-answerSubmit answer + grading
POST/api/skipSkip current problem
POST/api/scaffold-problemGenerate scaffold practice problem after wrong answer
GET/api/gamificationXP · level · streak status
GET/api/achievementsAchievement/badge list + unlock status
GET/api/statsPer-topic accuracy stats
GET/api/score-over-timeScore trend data over time
GET/api/historyFull problem history
GET/api/eventsSSE stream (real-time agent status)

Support

If MathCrew is helpful for your family, consider supporting the project:

Sponsor

License

PolyForm Strict 1.0.0 — free for personal and non-commercial use.

For commercial use, educational institutions, or schools, please reach out via LinkedIn (linked on GitHub profile) to request approval.

About

AI-powered adaptive math tutor for kids (Grade 1-6) — CrewAI multi-agent pipeline with Gemini + Ollama

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages