DEV Community

Egor
Egor

Posted on AI-assisted

AI Python Mentor

Building an AI-Powered Python Learning Platform with FastAPI, Next.js, and RAG

Building an AI-powered application is relatively easy when all you need is a chat interface connected to an LLM API.

Building a complete learning platform around that AI is a different challenge.

I wanted to explore how to combine a structured curriculum, practical programming exercises, personalized learning, and an AI tutor into a single full-stack system.

That's how AI Python Mentor started.

In this article, I'll walk through the architecture, technology choices, AI integration, and some of the engineering challenges behind the project.

GitHub: https://github.com/coffee1337/ai-python-mentor

The Idea

AI Python Mentor is a full-stack platform designed to help users learn Python and backend development.

Instead of simply providing AI-generated explanations, the application combines several learning mechanisms:

  • A structured curriculum covering Python and backend concepts
  • Programming exercises and knowledge checks
  • An AI tutor with access to lesson context
  • Personalized learning roadmaps
  • Progress tracking and spaced repetition
  • Career-oriented skill gap analysis
  • Practical portfolio projects

The goal is to build an environment where AI supports structured learning rather than replacing it.

Technology Stack

The application uses:

Backend

  • Python 3.12
  • FastAPI
  • PostgreSQL
  • SQLAlchemy and Alembic
  • Pydantic
  • AsyncIO

Frontend

  • Next.js 15
  • React 19
  • TypeScript

AI

  • LLM Gateway
  • Retrieval-Augmented Generation (RAG)
  • Lesson-based knowledge indexing
  • Token usage and cost accounting

Infrastructure

  • Docker and Docker Compose
  • GitHub Actions
  • Background workers
  • Database migrations

I chose FastAPI because it provides a solid foundation for asynchronous API development, explicit validation, and modular backend design.

PostgreSQL handles relational data, while SQLAlchemy and Alembic manage database access and schema evolution.

System Architecture

The application follows a modular monolith approach.

The core API is responsible for authentication, learning progress, lessons, exercises, and integrations.

Specialized tasks can be handled by separate processes.

                    Browser
                       |
                       v
                 Next.js Frontend
                       |
                       v
                  FastAPI API
                       |
        +--------------+--------------+
        |              |              |
        v              v              v
    PostgreSQL     AI Gateway     Background
                       + RAG        Workers
        |
        +---- Learning Progress
        +---- User Sessions
        +---- Exercise Attempts
        +---- AI Usage Records

Separate execution architecture:
    Execution Dispatcher
              |
              v
       Isolated Python Runner
Enter fullscreen mode Exit fullscreen mode

One of my architectural goals was to keep core learning features independent of the availability of external AI services.

For example, if the AI Gateway is unavailable, users should still be able to access lessons and other non-AI functionality.

This separation makes the application easier to maintain and evolve.

Building an AI Tutor with RAG

A general-purpose LLM can explain Python concepts, but it doesn't automatically know which lesson a student is studying or which version of the material is currently published.

That's where Retrieval-Augmented Generation becomes useful.

AI Python Mentor uses a local knowledge index built from immutable snapshots of published lesson content.

The basic flow is:

Student asks a question
          |
          v
Identify current lesson
          |
          v
Retrieve relevant lesson context
          |
          v
Build LLM request
          |
          v
Generate contextual explanation
          |
          v
Track usage and cost
Enter fullscreen mode Exit fullscreen mode

By retrieving context from the learning materials, the AI tutor can ground its explanations in the content the student is actually studying.

This is particularly important for educational applications, where a technically correct answer may still be inappropriate for the learner's current level.

The platform also tracks AI usage and costs.

This is an important consideration when building products that rely on paid LLM APIs.

Handling Python Code Execution

Another challenge is supporting practical programming exercises.

Users submit Python solutions following a simple contract:

def solve(payload):
    # User implementation
    ...
Enter fullscreen mode Exit fullscreen mode

Exercises can define public examples and hidden tests, while the platform stores submissions and attempt history.

However, executing untrusted Python code directly inside the main API would introduce significant security risks.

For that reason, the system separates execution dispatching from the primary application and defines a dedicated Python Runner architecture.

The production execution model is designed around isolation and resource restrictions, including container or sandbox boundaries.

Important: Untrusted code execution is disabled by default. The production Runner still requires the appropriate isolated deployment and security configuration.

The key design decision is simple: the main web API should never become the execution environment for arbitrary user code.

Authentication and Security

The backend includes several account security features:

  • Cookie-based sessions
  • Argon2 password hashing
  • CSRF protection
  • Email confirmation and password recovery
  • Session revocation
  • Account export and deletion
  • Administrative audit logging

These features are important because real applications require much more than a working login endpoint.

For production deployments, secure cookies and HTTPS are also required.

Background Processing

Not every operation should run directly inside an HTTP request.

The architecture includes background processing for workloads such as notifications and execution dispatching.

Separating those workloads helps keep request handling responsive and makes longer-running operations easier to manage.

Testing and CI

The repository includes automated checks for backend functionality, database migrations, frontend builds, worker logic, and browser flows.

GitHub Actions helps validate changes across these areas.

I also added support for native Windows development without requiring Docker for every local workflow.

That was useful because it made the development environment more accessible while keeping Docker-based deployment options available.

Current Status

AI Python Mentor is still under active development.

The core learning platform, backend, AI integration, database layer, and CI infrastructure have been implemented.

Current work focuses on product polish, learning content quality, AI evaluation, and production deployment.

I prefer being transparent about this: having a production-oriented architecture does not mean every planned production component has already been deployed.

What I Learned

This project reinforced several engineering lessons:

1. AI should be a component, not the entire architecture.

A useful AI product still needs reliable authentication, data models, application logic, and operational safeguards.

2. Structured context matters.

RAG is particularly valuable when answers should reflect specific, versioned learning materials.

3. Security boundaries should be designed early.

This is especially important when an application accepts executable code from users.

4. Operational concerns are product features.

Usage quotas, cost tracking, background jobs, and testing all contribute to the long-term viability of an AI application.

What's Next?

My next priorities include improving AI evaluation, polishing the user experience, refining educational content, and preparing the infrastructure for production deployment.

You can explore the code and architecture here:

GitHub: https://github.com/coffee1337/ai-python-mentor

Portfolio: https://coffee1337.github.io

I'm also open to freelance and contract projects involving Python backends, FastAPI, AI/LLM integrations, RAG systems, and SaaS MVP development.

If you're building something similar, feel free to reach out!


Tags: #python #ai #fastapi #webdev

Top comments (0)