Jay Mehta

LLM Anatomy & Concepts for Agentic System Builders

When you build AI agents or applications on top of LLMs, you’re making decisions at every step: which model to use, how much context you can fit, how to retrieve relevant knowledge, and how to deploy affordably. These concepts are the ones that drive those decisions. Understanding them turns you from someone who uses LLMs into someone who architects with them.

What goes on behind the scenes when you interact with an LLM:

    Raw Text
        │
        ▼
    ╔═════════════════════════════════════╗
    ║         Tokenizer/Preprocessor      ║
    ║                                     ║
    ║   ▼                                 ║
    ║   TOKENIZATION (BPE)                ║
    ║   ▼                                 ║
    ║   Token IDs [5765, 2065, 374, ...]  ║
    ║                                     ║
    ╚═════════════════════════════════════╝
        │
        ▼
    ╔═════════════════════════════════════╗
    ║           LLM Model                 ║
    ║                                     ║
    ║   ▼                                 ║
    ║   EMBEDDING → Dense Vectors         ║
    ║   ▼                                 ║
    ║   TRANSFORMER LAYERS (attention,    ║
    ║   feed-forward, billions of params) ║
    ║   ▼                                 ║
    ║   Output Token Probabilities        ║
    ║                                     ║
    ╚═════════════════════════════════════╝
        │
        ▼
    Generated Text

Anatomy of an LLM

An LLM is not a single monolithic thing — it’s a pipeline of distinct components, each with a specific job:

Component What it does Inside the model?
Tokenizer Splits raw text into subword tokens and maps them to integer IDs No — deterministic algorithm, shipped alongside the model
Embedding Layer Converts token IDs into dense vectors (high-dimensional representations) Yes — first layer of the neural network
Transformer Layers Process vectors through self-attention and feed-forward networks to build contextual understanding Yes — the bulk of the model’s parameters live here
Output Head Projects final vectors into vocabulary-sized probabilities for next-token prediction Yes — last layer of the neural network

Key distinctions:

What “size” refers to:

When someone says “a 70B model,” they mean 70 billion parameters spread across the embedding layer, transformer layers, and output head. The tokenizer adds negligible overhead.


LLM Concepts in Modelling, Customizing, Optimizing, and at Inference

When you’re building an AI agent or application, here’s how these concepts show up at each stage:

Modelling (creating the base model):

  1. Pre-trained Data Size — breadth of knowledge baked into the model, and its knowledge cutoff date
  2. Tokenization — how text is split into units the model learns from; determines vocabulary and context limits
  3. Embeddings — how tokens become vectors the model can process; learned during training
  4. Parameters — what’s actually inside that model making it smart
  5. Model Size — how many parameters, which determines capability vs. cost vs. hardware requirements
  6. Features — what the model learned to detect (you don’t control this, but understanding it explains model behavior)

Customizing & optimizing for deployment:

  1. Fine-tuning & LoRA — how you specialize a general model for your agent’s specific domain or behavior
  2. Quantization — how you deploy affordably without sacrificing too much quality

At inference (every request):

Tokenization and embeddings also run at inference time — the same flow (text → tokens → vectors → transformer layers → output) executes on every request your agent handles. The difference is that during training the model learns from this flow, while during inference it applies what it learned.

┌─────────────────────────────────────────────────────────────┐
│                    MODEL CREATION                           │
│                                                             │
│  Pre-trained Data (trillions of tokens)                     │
│       │                                                     │
│       ▼                                                     │
│  ┌─────────────────────────────────────────────────────┐    │
│  │  PRE-TRAINING                                       │    │
│  │                                                     │    │
│  │  Text ──→ TOKENIZATION ──→ Token IDs                │    │
│  │                │                                    │    │
│  │                ▼                                    │    │
│  │           EMBEDDING ──→ Vectors                     │    │
│  │                │                                    │    │
│  │                ▼                                    │    │
│  │           TRANSFORMER LAYERS                        │    │
│  │           (learn parameters by predicting           │    │
│  │            next token, billions of iterations)      │    │
│  │                                                     │    │
│  └─────────────────────────────────────────────────────┘    │
│       │                                                     │
│       ▼                                                     │
│  Base Model (parameters/weights, e.g. 7B, 70B, 405B)        │
│       │                                                     │
│       ▼                                                     │
│  (Optional) FINE-TUNING / LoRA ──→ Specialized Model        │
│       │                                                     │
│       ▼                                                     │
│  (Optional) QUANTIZATION ──→ Compressed for deployment      │
│                                                             │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│                    INFERENCE (using the model)              │
│                                                             │
│  Raw Text (user prompt, system prompt, RAG context)         │
│       │                                                     │
│       ▼                                                     │
│  TOKENIZATION ──→ Token IDs [5765, 2065, 374, ...]          │
│       │                                                     │
│       ▼                                                     │
│  EMBEDDING ──→ Dense vectors (features learned in training) │
│       │                                                     │
│       ▼                                                     │
│  TRANSFORMER LAYERS (parameters detect features,            │
│       │              build understanding layer by layer)    │
│       │                                                     │
│       ├── + LoRA adapters (if kept separate, applied here   │
│       │      alongside transformer weights per layer)       │
│       │                                                     │
│       │   OR merged into weights (if pre-merged after       │
│       │      fine-tuning — no extra step visible here)      │
│       │                                                     │
│       ▼                                                     │
│  Output Token Probabilities ──→ Generated Text              │
│                                                             │
└─────────────────────────────────────────────────────────────┘

The top box happens once (or occasionally, when you fine-tune). The bottom box happens every time your agent processes a request. Understanding both helps you make informed decisions about which model to pick, whether to fine-tune, and how to deploy.


Pre-trained Data Size

Pre-trained data size is the volume of text the model was trained on during pre-training. It’s measured in tokens and determines the breadth of knowledge.

Note: This is the total count of tokens processed during training, not unique tokens. The same document can appear multiple times across training epochs, so “15 trillion tokens” means the model read 15 trillion tokens in sequence as training examples — including repetitions. The actual unique token vocabulary is tiny by comparison (32K–128K entries). Think of it as “total tokens read” rather than “distinct tokens in the dataset.”

Scale reference:

Model Training Tokens Approximate Raw Text
LLaMA-2 2 trillion ~1.5 TB of text
LLaMA-3 15+ trillion ~11 TB of text
GPT-4 (estimated) 13+ trillion ~10 TB of text

GPT-4’s training data size is not officially disclosed by OpenAI. The figure above is a widely cited estimate.

What’s in the data:

Scaling laws (Chinchilla) — how model size and training data relate:

The optimal balance between model size and training data follows a power law. For a fixed compute budget, there’s an ideal ratio — you get the best performance by scaling parameters and training tokens roughly together, not by making one huge and the other small:

Why data size matters:

Practical takeaways for agentic architectures:


Tokenization

Tokenization is the process of converting raw text into a sequence of integers (token IDs) that the model can process. The model doesn’t see characters or words — it sees tokens.

Raw text is whatever input string gets sent to the model before any processing. It can originate from:

It’s simply the human-readable string that hasn’t yet been split into tokens.

When does the model “see” tokens? At inference time, before any neural network computation begins:

  1. The tokenizer (a deterministic preprocessor sitting outside the neural network) splits the raw text into subword pieces using BPE
  2. Each piece maps to an integer ID via a fixed vocabulary lookup table (e.g., "token"5765)
  3. Those IDs hit the embedding layer — each ID indexes into a learned matrix to produce a dense vector (e.g., 4096 floats)
  4. The transformer layers process those vectors — this is where the “thinking” happens (attention, feed-forward networks, billions of parameters)

From step 3 onward, the model only operates on high-dimensional vectors — it never processes raw characters or words directly.

Input:  "Tokenization is interesting!"
Tokens: ["Token", "ization", " is", " interesting", "!"]
IDs:    [5765, 2065, 374, 7185, 0]   (illustrative — actual IDs vary by tokenizer)

How it works:

Practical takeaways for agentic architectures:


Embeddings

An embedding is a dense vector representation of a token (or sequence) in continuous high-dimensional space. It’s how meaning gets encoded as numbers.

"king"  → [0.23, -0.45, 0.89, ..., 0.12]  (dimension: 4096)
"queen" → [0.25, -0.43, 0.87, ..., 0.14]  (similar vector!)
"car"   → [-0.67, 0.31, -0.22, ..., 0.88] (very different)

Key properties:

Vector dimensions:

Each embedding is a fixed-length array of floating-point numbers, each entry in this vector is a dimension. The length of that array is the dimensionality of the embedding.

Common dimension sizes:

Model / Context Dimensions Notes
BERT (base) 768 Older, smaller embedding models
OpenAI text-embedding-3-small 1536 Good balance of quality and cost
OpenAI text-embedding-3-large 3072 Higher quality, more expensive to store/search
LLaMA-3 8B (internal) 4096 Hidden dimension inside the LLM
LLaMA-3 70B (internal) 8192 Larger model = wider vectors

Practical impact:

Practical takeaways for agentic architectures:


Parameters / Weights

Parameters (or weights) are the learned numerical values inside the model. They are what the model “knows” — the compressed knowledge from training.

Neuron output = activation(weight₁·input₁ + weight₂·input₂ + ... + bias)

Scale reference:

Model Parameters Approximate Size (FP16)
LLaMA-3 8B 8 billion ~16 GB
LLaMA-3 70B 70 billion ~140 GB

More parameters = more capacity to store knowledge and patterns, but also more resources needed — both during training and when using the model:

Practical takeaways for agentic architectures:


LLM Model Size

Model size refers to the parameter count and directly determines memory requirements and capability:

What determines model size:

Size vs. capability tradeoff:

Parameters    RAM (FP16)    Capability Level
─────────────────────────────────────────────────
  1-3B         2-6 GB      Simple tasks, classification
  7-8B        14-16 GB     General chat, code, reasoning
 13-14B       26-28 GB     Strong all-around performance
 30-34B       60-68 GB     Near frontier quality
 65-70B      130-140 GB    Frontier-class reasoning
 180B+       360+ GB       Largest open models

Mixture of Experts (MoE):

Some models (Mixtral, GPT-4) use MoE — they have many parameters but only activate a subset per token. A router network decides which “expert” sub-networks handle each token:

Practical takeaways for agentic architectures:


Features

A feature is a pattern the model detects in the input to help it decide what to output next. Features are not defined by anyone — the model discovers them on its own during training.

How the model uses features:

Features aren’t a separate data structure the model looks up — they are the computation happening inside the transformer layers. The model builds features and uses them simultaneously, layer by layer:

  1. Input: Token embeddings arrive as vectors (raw features — just position in space based on token identity)
  2. Each transformer layer transforms the vectors:
    • Attention looks at all tokens and asks “which other tokens are relevant to this one?” — this detects relational features (e.g., “the word ‘it’ refers to ‘the dog’ earlier”)
    • Feed-forward network transforms the vector further — this detects local features (e.g., “this pattern looks like a negation”)
    • The output is a new vector that now encodes richer features than what came in
  3. After all layers: The final vector encodes everything the model “understands” about the full input — all features combined. This gets projected into vocabulary probabilities to predict the next token.

Each layer reads features from the previous layer’s output, builds higher-level features on top, and passes them forward. The model doesn’t “detect a question and then use that fact” as separate steps. Instead, by some middle layer, the vector has been shaped in a way that implicitly encodes “this is a question” — and subsequent layers build on that shape to produce an appropriate answer.

Features in training vs. inference:

Features play a role in both phases, but differently:

Think of it like learning to drive vs. driving:

The features exist in the parameters. Training shapes the parameters so they detect useful patterns. Inference runs input through those shaped parameters to produce predictions.

Features emerge in layers:

Each transformer layer builds increasingly abstract features on top of the previous layer’s output.

How features connect to embeddings and dimensions:

Features and dimensions are related but not the same thing:

Why they’re not one-to-one:

This is called a distributed representation. It’s what makes embeddings powerful (and hard to interpret) — meaning is spread out, not neatly boxed into individual slots.

Analogy: Think of a painting. The dimensions are individual pixels. A feature is “there’s a face in this image.” No single pixel is the face — the face emerges from the combination of many pixels. And each pixel contributes to multiple things (face, background, lighting) at once.

How is this different from traditional ML?

In older ML (like spam filters), a human would manually decide what features to look for:

You’d hand-engineer a list of signals and feed them to the model. In LLMs, nobody defines the features. The model discovers what patterns matter by itself during training. You can’t easily inspect or name them — they emerge from the data.

Practical takeaways for agentic architectures:


Fine-Tuning & LoRA

Fine-tuning is the process of taking a pre-trained model and training it further on a smaller, domain-specific dataset to specialize its behavior.

Why fine-tune?

A pre-trained model is a generalist — it knows a lot about everything but isn’t optimized for your specific task. Fine-tuning narrows its focus:

Full fine-tuning vs. LoRA:

Full fine-tuning updates all parameters in the model:

LoRA (Low-Rank Adaptation) is a fine-tuning technique that only trains a small set of adapter parameters, making it much cheaper in GPU memory and compute. The training dataset can be any size — LoRA’s benefit is about compute efficiency, not data efficiency.

How it works:

In a transformer, the attention layers have large weight matrices (e.g., 4096 × 4096 = ~16 million numbers in one matrix). During full fine-tuning, you’d update all 16 million numbers. LoRA takes a different approach:

  1. The original 4096 × 4096 matrix stays completely frozen — not a single number changes
  2. Two new small matrices are added alongside it (e.g., A: 4096 × 16, B: 16 × 4096)
  3. Only A and B get trained during the LoRA fine-tuning process
  4. At inference time: output = original_matrix × input + (A × B) × input

Why does this work? When you fine-tune for a specific task, you don’t need to change all 16 million numbers. The adjustment needed is much simpler — it can be captured with far fewer numbers. Think of it like: the original matrix is a detailed painting, but the edit you need is just “shift everything slightly warmer in tone” — that’s a simple adjustment, not a full repaint. The two small matrices capture that simple adjustment.

The “16” in those dimensions is the rank — a knob you control:

Deployment options after training:

Once A and B are trained, you have two choices for how to use them:

Which parameters get updated?

You (the practitioner) choose which layers to attach LoRA adapters to. Typically:

The original model weights are completely frozen. Only the small adapter matrices receive gradient updates during training.

How does training know when to stop?

Same as any neural network training — you monitor a validation loss:

There’s no magic “done” signal — it’s the practitioner’s job to monitor metrics and decide when the adapters have learned enough.

Analogy: Imagine you have a textbook (the pre-trained model). Full fine-tuning takes a red pen to every page — the original content is the starting point, but every part is allowed to change. LoRA adds a small set of sticky notes to specific pages — the original pages stay untouched, and the sticky notes are cheap to create.

QLoRA:

QLoRA = LoRA applied on top of a quantized model:

Practical takeaways for agentic architectures:

When to fine-tune vs. use RAG:

Approach Best for Tradeoff
RAG Factual recall, current information, large knowledge bases No training needed, but adds latency and retrieval complexity
Fine-tuning Behavior change, output format, tone, domain-specific reasoning Requires training data and compute, but faster at inference
Both Production agents that need specialized behavior AND current knowledge Most robust but most complex to maintain

Quantization

Quantization reduces the numerical precision of model weights to make models smaller and faster, with minimal quality loss.

Remember: every parameter in a model is a number (a weight). By default, each weight is stored as a 16-bit floating-point number (FP16). A 70B model has 70 billion of these numbers, each taking 16 bits — that’s ~140 GB just to load the model into memory. For many teams, that’s too expensive or simply doesn’t fit on available hardware.

Quantization solves this by asking: “Do we really need 16 bits of precision for each weight?” In most cases, the answer is no. You can round each weight to a less precise representation (8-bit, 4-bit, even 2-bit) and the model still produces nearly the same output. It’s like reducing an image from 24-bit color to 8-bit color — you lose some subtle gradients, but the picture is still recognizable.

When is quantization applied and by whom?

Quantization is typically applied after training, as a separate step before deployment:

Who does it:

Original (FP16):   1.234375 → stored in 16 bits
Quantized (INT4):  1.25     → stored in 4 bits (approximation)

Why quantize:

Quantization methods:

Method Approach
GPTQ Post-training quantization using calibration data
AWQ Activation-aware quantization (protects important weights)
GGUF Format used by llama.cpp for CPU inference
bitsandbytes Dynamic quantization during loading (NF4, INT8)

Practical takeaways for agentic architectures:


Q-bits (Quantization Bits)

Q-bits refers to the number of bits used to represent each weight after quantization. Lower bits = smaller model = faster but potentially less accurate.

FP32 (32-bit): Full precision, baseline quality
FP16 (16-bit): Half precision, standard for training/inference
INT8 (8-bit):  Good quality, 2x smaller than FP16
INT4 (4-bit):  Acceptable quality, 4x smaller than FP16
INT2 (2-bit):  Experimental, significant quality loss

Practical guide:

Q-bits Size Reduction Quality Impact Use Case
FP16 1x (baseline) None Cloud inference, training
INT8 2x smaller Minimal (~1%) Production serving
Q6_K 2.7x smaller Very small Local with quality priority
Q4_K_M 4x smaller Small (~2-3%) Local inference on laptop
Q2_K 8x smaller Noticeable Experimentation only

Practical takeaways for agentic architectures:


← Back to home