A multi-agent system for analogy-driven and visual learning in primary education.
MSc thesis project (Distinction) — University of Birmingham, School of Computer Science. Supervised by Dr Mohammed Bahja. Full thesis: docs/thesis.md.
StAnify turns a Key Stage 1–2 concept ("If you have 8 apples and eat 3, how many are left?") into a complete, classroom-ready package: an analogy-driven narrative, 4–6 curriculum-aligned illustrations, teacher guidance notes, and assessment items — in ~3 minutes, for ~$0.15 per concept.
StAnify was compared against GPT-5 and GPT-4 across mathematics, physics/chemistry, and biology concepts, blind-labeled A/B/C:
| StAnify | GPT-5 | GPT-4 | |
|---|---|---|---|
| Preferred by evaluators | 64.4% (58/90) | 33.3% | 2.2% |
| Engagement (mean /5) | 4.17 | 3.74 | 2.66 |
| Accuracy (mean /5) | 4.21 | 4.48 | 3.92 |
| Overall score (mean /5) | 3.93 | 4.00 | 2.79 |
- Preference for StAnify is statistically decisive (χ² = 52.27, p ≈ 1e-11); overall scores are statistically comparable to GPT-5 (paired t-test p = 0.685) while significantly above GPT-4 (p < 0.0001).
- The pattern: GPT-5 wins on polish, StAnify wins on pedagogy — evaluators chose the system that teaches over the system that explains.
- 17–25x faster than unaided teacher preparation (50–80 min → ~3 min); ~3–5x faster than a teacher drafting with GPT-4/5.
Eight CrewAI agents behave like a miniature school staffroom, with a Qdrant vector database holding curriculum data, an age-graded vocabulary bank, and visualization scaffolds:
| Agent | Role |
|---|---|
| Analogy Creator | Maps the concept to everyday child experiences |
| Narrative Builder | 300–500 word story embedding the analogy and scaffolded vocabulary |
| Content Evaluator | Scores lawfulness, relevance, analogy quality, vocabulary; rejects and loops below threshold |
| Content Chunker | Segments the narrative, marks highlights worth visualizing |
| Image Prompt Creator | Turns highlights into age-appropriate prompts using visualization tips |
| Image Creator | Stable Diffusion: txt2img, or img2img for continuity (SDXL in the study, SD 3.5 in this rebuild) |
| Image Critic | Vision-inspects every image for relevance, safety, appropriateness; can force regeneration |
| Content Combiner | Assembles narrative, images, teacher notes, and assessment items |
graph TD
A[Concept prompt] --> B[Analogy Creator]
B --> C[Narrative Builder]
C --> D{Content Evaluator<br/>RAG: curriculum + vocabulary}
D -- fail --> B
D -- pass --> E[Content Chunker]
E --> F[Image Prompt Creator]
F --> G[Image Creator<br/>Stable Diffusion]
G --> H{Image Critic<br/>vision inspection}
H -- regenerate --> F
H -- pass --> I[Content Combiner]
I --> J[Web app: slideshow, teacher pack, download]
The two evaluation loops (text and image) are the zero-hallucination mechanism: nothing reaches a child without passing a rubric-scored check against the curriculum database.
Exact counts. Diffusion models cannot count (asked for eight biscuits, SD 3.5 drew 5, 6, 10 and 12). Where a slide's lesson is a number, the Image Prompt Creator emits a counting spec; the scene is generated without those objects, and the Image Creator pastes exactly N object sprites onto a tray in rows of five, with taken-away items faded and set apart. Counts are correct by construction, and the Image Critic still inspects the composite.
Type the question a child might ask, choose the year group and subject, and StAnify makes the lesson while you watch each step. Every lesson has:
- Slides: 4-6 illustrated steps with the story text, a "look for" cue and a teacher note. Present full screen and move with the arrow keys.
- Story: the analogy, how it maps onto the concept and where it breaks down, plus the full story and new words.
- Teacher pack: the learning objective, teaching notes, likely misconceptions and quick questions with answers.
- Quality checks: the reviewer's scores for the story and the critic's scores for every picture, with anything a teacher should check before using it.
Requires uv (it installs Python 3.13 for you). Full instructions, keys and troubleshooting: START.md.
git clone https://github.com/Haadhi76/StAnify.git && cd StAnify
uv sync
cp .env.example .env # add OPENROUTER_API_KEY (free) and STABILITY_API_KEY
uv run stanify ingest # build the curriculum index (first run downloads a small embedding model)
uv run stanify serve # open http://localhost:8000The original Streamlit interface is kept as a backup demo (uv run streamlit run app.py), and lessons can be made without a browser:
uv run stanify run --concept "How are particles arranged in a solid compared to a gas?" --year "Year 4" --subject scienceEach run writes output/<run_id>/package.json plus the slide images. No Stability key? STANIFY_IMAGE_PROVIDER=placeholder runs the whole pipeline with labelled placeholder cards.
StAnify is model-agnostic. Every agent call goes through an ordered fallback chain. If a provider errors or rate-limits, the stage is retried on the next provider, and later stages start from the one that worked:
| Order | Default model | Key |
|---|---|---|
| 1 | openrouter/google/gemma-4-31b-it:free (free tier) |
OPENROUTER_API_KEY |
| 2 | gemini/gemma-4-31b-it (Google AI Studio) |
GEMINI_API_KEY |
| 3 | anthropic/claude-haiku-4-5-20251001 |
ANTHROPIC_API_KEY |
Models without a key are skipped. Override with STANIFY_LLM_CHAIN (any crewAI/LiteLLM model string: openai/..., ollama/...). The Image Critic uses the same chain, because Gemma 4 and Claude are both vision models. Images come from Stable Diffusion 3.5 through the Stability API (STABILITY_MODEL). Curriculum retrieval uses local FastEmbed embeddings in embedded Qdrant, so it needs no key.
Stack: CrewAI 1.15 · Qdrant (qdrant-client 1.19, FastEmbed) · Stability AI SD 3.5 · FastAPI · Streamlit 1.66 (backup demo) · Pydantic 2 · LiteLLM · Python 3.13.
Run live on the four thesis questions (October 2026): every story passed the curriculum reviewer, one of them only after the revise loop rewrote it. Each lesson took about 6-9 minutes, and about half the pictures passed the image critic. Pictures that miss are flagged in the Quality checks tab rather than hidden. Stable Diffusion still struggles with physics geometry (shadow length) and cutaway diagrams. On the day of testing the free OpenRouter and AI Studio tiers were rate-limited, so most calls fell through to Claude Haiku.
The thesis measured ~$0.14-0.16 per complete concept package on GPT-4o-mini + SDXL: image generation was ~80% of the cost, LLM calls ~14% (six agents, ~3.5k tokens), and vision evaluation and embeddings the remainder. The rebuilt system's default chain uses free Gemma 4 for the text and vision agents and local embeddings, so per-concept cost is now essentially the 4-6 SD 3.5 images.
src/stanify/
config/agents.yaml, tasks.yaml the staffroom: roles, goals, task prompts
schemas.py Pydantic models for every agent output and the final package
crew.py orchestration: CrewAI stages + evaluator and critic loops
knowledge.py YAML -> Qdrant ingest, filtered RAG retrieval
llm.py provider fallback chain
tools/ rag_search.py, sd_tools.py (Stability), vision_inspect.py
tools/compositor.py exact-count compositing for maths slides
web/server.py, web/static/ the web app: FastAPI + server-sent progress, HTML/CSS/JS UI
main.py CLI (`stanify serve`, `stanify ingest`, `stanify run`)
app.py Streamlit backup demo
data/ KS1-2 curriculum, age-graded vocabulary bank, visualization tips
tests/ pytest suite (no API keys needed)
docs/thesis.md the complete MSc thesis: methodology, evaluation design, full results
docs/agent_roles.md agent-by-agent inputs, outputs and schemas
The original experimental code lived in the University of Birmingham GitLab. This repository is a clean rebuild from the thesis (§4, Appendix A/B4) on current library versions, with the curriculum knowledge base re-authored from the National Curriculum in England programmes of study (Years 1-4).
Irfan, H. (2025). StAnify: A Multi-Agent System for Analogy-Driven and Visual Learning in Primary Education. MSc Thesis, University of Birmingham.
Haadhi Irfan — haadhi76.github.io/Portfolio · LinkedIn · haadhi.irfan@gmail.com
