Seven layers, one building. Stop mixing them up.
AI, ML, Deep Learning, GenAI, LLMs, RAG, and Agentic AI get used like synonyms — they’re not. Each one exists to fix a limit in the layer below it. Here’s how the stack actually fits together, with real code, real products, and a decision tree for what to build next.
Explain AI vs Machine Learning to five different people and you’ll get five different answers — a marketer means ChatGPT, a data scientist means the gradient-boosted model quietly predicting churn, a robotics engineer means a control system that’s been running since the 1980s. None of them are technically wrong. They’re each standing on one floor of a building and calling it the whole building.
That’s really the issue with how these terms get thrown around: seven distinct layers, used interchangeably, when they actually describe one cumulative stack — each built directly on top of the last. Picking the wrong layer for your problem doesn’t just cost elegance, it costs months. You don’t need an LLM to catch fraud. You don’t need an agent to summarize a PDF. You don’t need RAG if the model already knows the answer cold.
So here’s the stack, floor by floor — what each layer actually is, how it works once you open the hood, real code, real products, and when it’s worth reaching for.
The evolution, at a glance
| Era | Core idea | What broke |
|---|---|---|
| Rule-based AI | Hand-coded logic, expert systems | Couldn’t handle scenarios the designer didn’t anticipate |
| Machine learning | Learn statistical patterns from data | Needed hand-engineered features for unstructured data |
| Deep learning | Neural nets learn features automatically | Still one narrow model per task |
| Generative AI | Create content, not just classify it | Generation was narrow and inconsistent |
| LLMs | Foundation models specialized in language | Frozen knowledge, no private data, hallucination |
| RAG | Ground generation in retrieved real data | Still passive — answers only when asked |
| Agentic AI | Plan, use tools, act autonomously | Reliability and safety at scale remain open problems |
Every new layer exists to fix a bottleneck in the layer before it. Keep that in mind for everything below.

Artificial Intelligence
AI is the broadest category here, and the least useful one to hang your hat on: any system built to do things that normally take human intelligence — reasoning, perception, decision-making, planning ahead. It’s not one technique, it’s a goal. Every other layer in this guide is a different engineering path toward it.
AI as a field dates to the 1956 Dartmouth Workshop. Early systems were almost entirely symbolic — expert systems like MYCIN used hand-written logical rules, which worked for narrow domains and collapsed the moment real ambiguity showed up.
Characteristics
- Can be rule-based (symbolic) or statistical (learned) — a rule-based chess engine is AI even though it never learns
- Ranges from narrow AI (one task) to the still-hypothetical general AI (human-level flexibility)
Advantages
- Automates decisions at speed and consistency humans can’t match
- Scales without proportional labor cost
Limitations
- Narrow AI fails outside its trained domain
- “AI” as a label says nothing about how a system actually works
| Industry | Application |
|---|---|
| Manufacturing | Vision-guided robotic arms for defect detection |
| Gaming | NPC behavior trees, A* pathfinding |
| Finance | Rule-based fraud flagging |
| Logistics | Route optimization, warehouse robotics |

Machine Learning
ML is what people mean when they say a system “learned” something instead of being told what to do. Rather than writing “if income > X and age < Y, approve the loan,” you show it thousands of past decisions and let it work out the pattern itself.
The three core types
- Supervised — learns from labeled input → output pairs (predict house price from past sales)
- Unsupervised — finds structure with no labels (cluster customers by behavior)
- Reinforcement — learns via trial, error, and reward (an agent learning to play a game)
The three core types
Collect data → Clean & preprocess → Feature engineering →
Split (train/test) → Train model → Evaluate → Tune → Deploy → Monitor
Minimal example
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=200)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
If your data is tabular — clear rows and columns — reach for classic ML first. Deep learning isn’t automatically better, it’s a different tool for a different data shape.
| Algorithm | Type | Typical use |
|---|---|---|
| Logistic regression | Supervised | Binary classification |
| Random forest / XGBoost | Supervised | Fraud detection, churn |
| K-Means | Unsupervised | Customer segmentation |
| Q-learning | Reinforcement | Game-playing agents |

Deep Learning
Deep Learning takes ML a step further — it’s the subset that uses neural networks stacked into many layers to work out hierarchical features straight from raw data: pixels, audio, raw text. Nobody has to hand-engineer what the model should look for.
In classic ML, a human decides the features. In deep learning, early layers learn edges and colors, middle layers combine them into shapes, later layers combine those into full concepts — automatically, from data.
Core architectures
- CNNs — convolutional filters slide across images, detecting local patterns and stacking them into higher-level features
- RNNs — process sequences while maintaining memory of previous inputs; LSTMs/GRUs handle longer sequences
- Transformers — replaced RNNs for most sequence tasks since 2017, using self-attention to look at an entire sequence at once. This is the architecture behind every LLM below.
Training loop
- Initialize weights randomly
- Forward pass — data flows through layers to a prediction
- Calculate loss — how wrong the prediction was
- Backpropagation — attribute error to each weight
- Update weights with an optimizer (Adam, SGD)
- Repeat until loss stabilizes
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, padding=1)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(2)
self.fc = nn.Linear(16 * 14 * 14, 10)
def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = x.view(x.size(0), -1)
return self.fc(x)

Generative AI
GenAI is where things shift from recognizing to making. It’s the subset of Deep Learning built to create new content — text, images, audio, video, code — instead of just classifying or predicting from what already exists. A CNN looks at a photo and says “this is a cat.” A generative model produces a photo of a cat that’s never existed, on demand.
Foundation models
Modern GenAI runs on foundation models — very large networks pretrained on broad data, then adapted for many downstream tasks through prompting or fine-tuning. This is the shift from one-model-per-task to one model, many jobs.
Prompt engineering
Bad: "Write about dogs."
Better: "Write a 200-word blog intro about why golden retrievers
make good family pets, warm conversational tone,
targeting first-time dog owners."
| Modality | Tools |
|---|---|
| Text | ChatGPT, Claude, Gemini |
| Image | Midjourney, DALL·E, Stable Diffusion |
| Audio/Music | Suno, ElevenLabs |
| Video | Runway, Sora |
| Code | GitHub Copilot, Cursor, Claude Code |
Advantages
- One general model handles many tasks
- Cuts time-to-first-draft dramatically
Limitations
- Can hallucinate confidently
- No access to private or real-time data — this is exactly what RAG fixes

Large Language Models(LLMs)
An LLM is what you get when you point Generative AI specifically at language. Built on the transformer architecture and trained on enormous amounts of text, it picks up not just grammar but the patterns of how ideas get expressed.
The concepts that explain almost everything
- Tokens — the units of text a model processes; whole words, sub-words, or characters
- Embeddings — each token becomes a vector encoding meaning; similar words sit close together
- Attention — for each token, the model weighs how relevant every other token is — how it knows “it” refers to the trophy, not the suitcase
- Context window — the max tokens a model can consider at once, now spanning hundreds of thousands of tokens in frontier models
From raw model to usable assistant
- Pretraining — learn general language patterns by predicting the next token across massive text
- Fine-tuning — further train on curated instruction-following data
- RLHF — human rankings of outputs train the model to prefer helpful, honest, safe responses
| Family | Developer | Known for |
|---|---|---|
| GPT-4/5 | OpenAI | General-purpose, multimodal reasoning |
| Claude | Anthropic | Long context, coding, instruction-following |
| Gemini | Google DeepMind | Native multimodality |
| Llama | Meta | Open-weight, self-hostable |
| Mistral | Mistral AI | Efficient open-weight models |
| DeepSeek | DeepSeek | Cost-efficient reasoning |
| Qwen | Alibaba | Multilingual, coding |
An LLM only knows what was in its training data. It knows nothing about your private documents, or what happened an hour ago. That’s the exact gap RAG fills.

Retrieval-Augmented Generation (RAG)
RAG is what happens when you stop asking an LLM to answer from memory alone and hand it something to actually read first. It pairs the model’s generative ability with a retrieval step that pulls relevant information from an external source — your documents, a database, a knowledge base — and drops that context into the prompt before the model writes anything.
The pipeline
Documents → Chunking → Embedding model → Vector database
│
User query → Embed query → Similarity search ───────┘
│
Top-K relevant chunks
│
Prompt = query + retrieved chunks
│
LLM generates grounded answer
- Chunking — long documents split into passages (200–1,000 tokens) so nothing gets lost in one giant vector
- Embedding — each chunk becomes a vector capturing its meaning
- Vector database — Pinecone, Weaviate, Qdrant, or
pgvector, optimized for fast similarity search - Ranking — a re-ranking pass often refines results with a slower, more precise model
results = collection.query(query_texts=[query], n_results=3)
context = "\n".join(results["documents"][0])
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Answer only using the provided context."},
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
]
)
Use RAG when
- Answers must reflect private or changing data
- Hallucination is unacceptable
- You need source attribution
Skip RAG when
- The task needs no external knowledge
- The whole doc fits the context window anyway
- The system needs to act, not just answer

Agentic AI
Agentic AI is where the model stops just talking and starts doing. Built on top of LLMs, often paired with RAG, it can reason through a problem, plan a sequence of steps, call outside tools, and carry out multi-step actions toward a goal without someone approving each move along the way.
A chatbot, even a RAG-powered one, is reactive: you ask, it answers, done. An agent is goal-driven: give it an objective and it figures out the steps, calls the tools it needs, evaluates results, and adjusts if something fails.
Core components
- Planning — breaking a goal into executable steps
- Tool calling — invoking APIs, running code, querying a database
- Memory — short-term task context, sometimes long-term across sessions
- Reasoning — evaluating whether a step succeeded and what to do next (ReAct-style loops)
- Reflection — self-critiquing a plan or output before finalizing it
Goal received → Plan steps → Select tool → Execute call
↑ │
└────────── No ── Goal achieved? ←── Observe result
│
Yes → Return final output
Real examples
- Coding agents (Claude Code, Cursor’s agent mode) that read a codebase, plan a multi-file change, write it, run tests, and fix failures
- Support agents that look up an order, check policy via RAG, issue a refund, and send confirmation — end to end
- Research agents that split a broad question into sub-questions and synthesize a final report
AI vs Machine Learning vs Deep Learning: The Complete Comparison
| Layer | Goal | Input | Best use case |
|---|---|---|---|
| AI | Simulate intelligent behavior | Any | Broad problem framing |
| ML | Learn patterns from data | Structured/tabular | Fraud detection, forecasting |
| DL | Learn features automatically | Raw unstructured data | Vision, audio recognition |
| GenAI | Create new content | Prompt/instruction | Content and code generation |
| LLM | Understand & generate language | Text prompt | Language tasks, reasoning |
| RAG | Ground answers in real data | Query + knowledge base | Accuracy on private/changing data |
| Agentic AI | Achieve a goal autonomously | Objective | Multi-step autonomous workflows |
What’s powering the products you use
| Product | Core layers |
|---|---|
| ChatGPT | LLM, increasingly agentic with tools |
| Claude | LLM + Agentic capabilities (Computer Use, Claude Code) |
| Perplexity | LLM + RAG over the web |
| Cursor | LLM + Agentic AI |
| Notion AI / Microsoft Copilot | LLM + RAG over your workspace |

