# How AI Engineering from Scratch Teaches Algorithms From First Principles Before Frameworks

> Learn AI algorithms from first principles using Python's standard library before frameworks. Explore the build-first methodology in the rohitg00/ai-engineering-from-scratch repository.

- Repository: [Rohit Ghumare/ai-engineering-from-scratch](https://github.com/rohitg00/ai-engineering-from-scratch)
- Tags: deep-dive
- Published: 2026-09-02

---

**The rohitg00/ai-engineering-from-scratch repository employs a *build-first* methodology where every algorithm is implemented using only Python's standard library before any third-party framework is introduced.**

This educational approach ensures that learners understand the mathematical and algorithmic foundations of machine learning before relying on high-level abstractions. According to the repository curriculum, students must construct working implementations—from perceptrons to tokenizers—using pure Python before comparing their solutions against production frameworks like PyTorch, LangGraph, or tiktoken.

## The Build-First Pedagogical Philosophy

The curriculum is structured around a strict progression from mathematical theory to algorithmic implementation, deliberately delaying framework introduction until the core mechanics are fully understood. In [`phases/03-deep-learning-core/README.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/03-deep-learning-core/README.md), the repository explicitly states its goal: **"Neural networks from first principles. No frameworks until you build one yourself."**

This philosophy manifests across multiple phases, each enforcing a **stdlib-first** dependency policy that mandates original implementations using only Python's built-in capabilities.

## Phase 1: Deep Learning Core — The Perceptron

The journey begins with the perceptron, the simplest learning machine, implemented without NumPy, PyTorch, or any external dependencies. The lesson documentation in [`phases/03-deep-learning-core/01-the-perceptron/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/03-deep-learning-core/01-the-perceptron/docs/en.md) establishes the theoretical foundation—weights, bias, and step activation functions—before students write executable code.

### Pure Python Implementation

The repository's [`perceptron.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/perceptron.py) file contains a complete implementation using only Python lists and arithmetic operations:

```python
class Perceptron:
    def __init__(self, n_inputs, learning_rate=0.1):
        self.weights = [0.0] * n_inputs
        self.bias = 0.0
        self.lr = learning_rate

    def predict(self, inputs):
        total = sum(w * x for w, x in zip(self.weights, inputs)) + self.bias
        return 1 if total >= 0 else 0

    def train(self, training_data, epochs=100):
        for _ in range(epochs):
            errors = 0
            for inputs, target in training_data:
                pred = self.predict(inputs)
                error = target - pred
                if error:
                    errors += 1
                    for i in range(len(self.weights)):
                        self.weights[i] += self.lr * error * inputs[i]
                    self.bias += self.lr * error
            if errors == 0:
                break

```

Notice that the training loop, weight updates, and error calculation are hand-written using basic Python operations. This forces learners to internalize how gradient descent works at the arithmetic level before ever calling `model.fit()` in a framework.

## Phase 2: Layer-by-Layer Algorithmic Extensions

After mastering the single-layer perceptron, the curriculum addresses its limitations—specifically the inability to solve the XOR problem. Rather than importing a solution, students manually compose **OR**, **NAND**, and **AND** gates to create a multi-layer network. Only after hand-crafting this hidden layer logic do they graduate to a trainable two-layer network implementing full back-propagation.

This progression ensures that learners understand **why** multi-layer architectures are necessary before they abstract the complexity into PyTorch `nn.Module` classes.

## Phase 3: Language Models From Scratch — The Tokenizer

The "scratch-first" method extends to natural language processing. In [`phases/10-llms-from-scratch/01-tokenizers/code/main.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/10-llms-from-scratch/01-tokenizers/code/main.py), students build a Byte Pair Encoding (BPE) tokenizer that directly manipulates raw bytes without using OpenAI's `tiktoken` or Hugging Face tokenizers.

### BPE Without External Libraries

The implementation builds a merge table and compression ratios through direct byte manipulation:

```python
class BPETokenizer:
    def __init__(self):
        self.merges = {}
        self.vocab = {}

    def train(self, text, num_merges):
        tokens = list(text.encode("utf-8"))
        self.vocab = {i: bytes([i]) for i in range(256)}
        for i in range(num_merges):
            pairs = self._get_pairs(tokens)
            if not pairs: 
                break
            best = max(pairs, key=pairs.get)
            new = 256 + i
            tokens = self._merge_pair(tokens, best, new)
            self.merges[best] = new
            self.vocab[new] = self.vocab[best[0]] + self.vocab[best[1]]
        return self

```

External tokenizers like `tiktoken` appear only as optional benchmarks for comparison, not as dependencies for the learning exercise.

## Phase 4: Agent Engineering — Frameworks Last, Not First

When the curriculum reaches agent orchestration (Phase 14), the documentation in [`phases/11-llm-engineering/17-agent-framework-tradeoffs/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/11-llm-engineering/17-agent-framework-tradeoffs/docs/en.md) delivers a strict mandate: **"Refuse to pick a framework before you can draw the graph, the org chart, the chat, or the agent box."**

### Plain-Python Orchestration First

Students must first implement agent workflows using raw Python:

```python
def research_workflow(task):
    plan = llm.invoke(f"Plan the steps for: {task}")
    result = llm.invoke(f"Execute step 1: {plan[0]}")
    # …repeat without any framework abstraction

    return result

```

Only after this raw implementation works correctly do students map the logic onto **LangGraph**, **CrewAI**, **AutoGen**, or **Agno**, comparing metrics like token cost, code size, and state handling between their solution and the frameworks.

## Policy Enforcement: The AGENTS.md Mandate

The repository's [`AGENTS.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/AGENTS.md) policy file institutionalizes this approach through strict contribution guidelines:

- **One commit per lesson directory** enforcing atomic learning units
- **Original implementations only** prohibiting framework shortcuts in starter code
- **Stdlib-first dependency lists** guaranteeing that every lesson begins with zero third-party libraries

This policy ensures that the pedagogical pipeline remains intact: mathematical core → algorithmic implementation → verification → optional framework comparison.

## Summary

The rohitg00/ai-engineering-from-scratch repository teaches algorithms from first principles through a systematic four-phase approach:

- **Mathematical foundations precede code**: Each lesson establishes theory before implementation
- **Standard library only**: Core algorithms use pure Python without NumPy, PyTorch, or specialized libraries
- **Progressive complexity**: Students move from perceptrons to multi-layer networks to BPE tokenizers, building each layer manually
- **Frameworks as comparisons, not crutches**: External libraries appear only after the student implementation works, serving as benchmarks rather than starting points
- **Enforced by policy**: [`AGENTS.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/AGENTS.md) mandates original implementations, ensuring every learner experiences the full algorithmic construction

## Frequently Asked Questions

### Why learn algorithms from scratch when frameworks exist?

Understanding the underlying arithmetic and data structures allows engineers to debug framework behavior, optimize performance, and innovate beyond pre-built abstractions. When you hand-code backpropagation in [`perceptron.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/perceptron.py), you comprehend why gradient vanishing occurs—knowledge that remains obscure when merely calling `.backward()` in PyTorch.

### How long does the repository delay introducing frameworks?

Frameworks appear only after the primitive implementation is verified working. For deep learning, this means building a functioning multi-layer perceptron before seeing PyTorch. For LLMs, students build a working BPE tokenizer before comparing against `tiktoken`. The exact duration depends on the learner, but the policy in [`AGENTS.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/AGENTS.md) enforces that no lesson directory contains framework dependencies in its initial commit.

### Does this approach work for production engineering?

Yes. The repository explicitly bridges to production by mapping plain-Python implementations onto industry frameworks like LangGraph and CrewAI in Phase 14. By understanding the raw orchestration logic first, engineers can evaluate framework trade-offs based on token cost and state management rather than accepting black-box defaults.

### What Python knowledge is required to follow this curriculum?

Learners need only standard Python programming skills. The curriculum deliberately uses basic data structures (lists, dictionaries) and control flow rather than advanced libraries. The complexity comes from the algorithms themselves—matrix multiplication for neural networks or byte-pair statistics for tokenization—not from esoteric language features.