AI · LLMs · Machine Learning · Software Engineering · Technology · RAG · Context Engineering
Inside the Modern AI Stack: A Plain-English Guide to How LLMs Really Work
A plain-English walkthrough of how LLMs work — tokenization, transformers, RAG, agents, and evaluation — using software architecture analogies.
Large Language Models feel magical from the outside. You type a question, wait a few seconds, and receive a fluent, structured answer that appears to understand your intent. But under the surface, modern AI systems are not magic. They are layered software systems built from tokenizers, embeddings, transformer networks, context management, alignment techniques, retrieval pipelines, tool orchestration, and evaluation frameworks.
A useful way to understand modern AI is to stop thinking of it as "one model" and start thinking of it as a full-stack system. The model is the engine, but the real product experience comes from everything wrapped around it: memory, tools, documents, APIs, feedback loops, safety controls, and evaluation.
1. Tokenization and Embeddings: Turning Language into Machine-Readable Structure
Before an AI model can understand a sentence, it has to convert human language into something a neural network can process.
LLMs do not read words the way humans do. They break text into smaller units called tokens. A token might be a whole word, part of a word, punctuation, or even a small character sequence. For example, a rare technical word may be split into several subword fragments, while a common word may remain as a single token.
A good software analogy is a compiler. Just as a compiler transforms human-readable source code into bytecode or an intermediate representation, tokenization transforms raw text into a sequence of numerical IDs.
But token IDs alone do not carry meaning. That is where embeddings come in. Embeddings map each token into a high-dimensional vector space. In simple terms, they place related ideas close to each other in a mathematical map. "King" and "queen" may sit closer together than "king" and "voltage." "Database," "query," and "index" may cluster together because they often appear in related contexts.
This is why embeddings power semantic search, recommendation systems, vector databases, and many RAG applications. They allow systems to search by meaning rather than exact keyword matching.
The important product lesson is this: tokenization affects cost, speed, and accuracy. A poorly handled input can consume too many tokens, reduce available context, and increase API cost. For AI product builders, tokens are not just a technical detail. They are the unit of economics.
2. Transformers and Self-Attention: The Engine Behind Modern LLMs
The transformer is the architecture that made modern LLMs possible. Its core innovation is self-attention.
A simple way to describe self-attention is this: the model looks at every token in a sequence and decides how much each token should pay attention to every other token.
In the sentence:
The engineer fixed the bug because it caused the app to crash.
The model needs to understand what "it" refers to. Self-attention helps the model connect "it" back to "the bug," not "the engineer" or "the app."
From a software architecture perspective, self-attention is like a massively parallel event router. Instead of processing each word one by one in a strict queue, the transformer examines the whole sequence and dynamically routes contextual information between tokens.
Technically, transformers do this using Query, Key, and Value matrices. Each token produces a query, key, and value representation. The model compares queries against keys to calculate relevance, then uses those relevance scores to decide how much information from each value should be passed forward.
This mechanism allows transformers to capture long-range relationships, process training data efficiently on GPUs, and scale to enormous model sizes.
A common misconception is that LLMs "read" text from left to right like humans. During training and prompt processing, transformers process the sequence in parallel. Order is injected through positional encoding techniques, such as Rotary Positional Embeddings. During generation, however, they produce output one token at a time, which is why responses stream progressively.
3. Context Window and KV Cache: The Runtime Memory of an LLM
The context window is the model's working memory. It defines how many tokens the model can consider at once, including system instructions, chat history, documents, user input, and the answer it is generating.
A 128k-token context window sounds huge, but it is not unlimited. Every instruction, pasted document, retrieved chunk, and generated response competes for the same budget.
The KV cache is another critical part of inference. When a model generates text, it does not want to recompute the entire conversation every time it produces a new token. So it caches the Key and Value tensors from previous tokens. This makes generation faster, but it also creates a large memory-management problem.
A good analogy is session state plus virtual memory. The context window is the memory allocated to a session. The KV cache is runtime state that grows as the conversation grows. Serving frameworks such as vLLM improve efficiency by managing this memory more intelligently, using techniques like paged attention.
This matters because many AI product bottlenecks are not caused by raw compute alone. During generation, memory bandwidth often becomes the limiting factor. Long context, large batches, and deep models can create latency spikes or out-of-memory failures if the serving infrastructure is poorly designed.
There is also a quality issue. Bigger context does not automatically mean better answers. Models can suffer from the lost-in-the-middle problem, where information placed deep inside a long prompt is more likely to be ignored or underweighted.
For product design, this means context engineering is just as important as prompt engineering. The question is not "How much can I stuff into the prompt?" The better question is "What does the model need, where should I place it, and how do I make it easy to use?"
4. Alignment: Turning a Raw Predictor into a Useful Assistant
Pre-training gives an LLM broad language and reasoning capability. But a raw pre-trained model is not automatically a helpful assistant. It predicts likely text. It does not naturally know how to follow instructions, refuse unsafe requests, format answers, or behave conversationally.
That is where alignment comes in.
The first stage is often Supervised Fine-Tuning, or SFT. The model is trained on examples of high-quality instruction-following behavior. This teaches it the shape of helpful answers.
Then techniques such as Reinforcement Learning from Human Feedback help refine the model further. In RLHF, humans compare model outputs, and a reward model learns which answers people prefer. The model is then optimized to produce responses that score better under that reward model.
Newer methods such as Direct Preference Optimization, or DPO, simplify this process by directly training on preferred versus rejected responses without requiring a separate reward model.
A useful analogy is this: pre-training builds the raw operating system, while alignment adds the user interface, guardrails, defaults, and safety sandbox.
This is also where many people misunderstand fine-tuning. Fine-tuning is usually not the best way to teach a model constantly changing facts. It is better for teaching behavior, tone, structure, or domain-specific response patterns. For fresh or proprietary knowledge, Retrieval-Augmented Generation is usually a better pattern.
5. RAG: Giving the Model an Open-Book Exam
Retrieval-Augmented Generation, or RAG, is one of the most important patterns in practical AI applications.
Instead of relying only on the model's internal training data, a RAG system retrieves relevant documents at runtime and places them into the prompt. The model then answers using that supplied context.
The simplest analogy is an open-book exam. The model may already know a lot, but RAG gives it the specific material it should use for this particular question.
A more technical software analogy is a runtime database query. Rather than hardcoding all knowledge into the model weights, the application retrieves relevant information from an external knowledge store when needed.
A typical RAG pipeline includes:
- splitting documents into chunks
- embedding those chunks into vectors
- storing them in a vector database
- retrieving the most relevant chunks for a user query
- optionally reranking the chunks
- injecting them into the model's context
- generating an answer grounded in the retrieved material
RAG is powerful because it helps reduce hallucination, supports proprietary knowledge, and allows AI systems to work with changing information without retraining the model.
But RAG is not magic. Poor chunking, weak retrieval, irrelevant documents, bad ranking, or excessive context can still produce poor answers. A RAG system is only as good as its retrieval quality and context design.
The key misconception is that putting documents into a prompt "teaches" the model permanently. It does not. The model only has access to those documents during that specific request. Once the request ends, that temporary context disappears unless it is stored somewhere else.
6. Agents and Tool Use: From Chatbot to Digital Operator
The next major shift is from passive chatbots to agentic systems.
A normal chatbot answers questions. An agent can plan, call tools, inspect results, revise its plan, and continue working toward a goal.
Tool use gives the model external capabilities. It can call a calculator, search the web, query a database, read a file, execute code, create a calendar event, or interact with business systems.
Architecturally, this resembles microservices orchestration through an API gateway. The LLM interprets the user's request, decides which tool is needed, generates a structured payload, waits for the tool result, and then continues reasoning.
Importantly, the model itself does not literally browse the web or execute code internally. It emits a tool call request. An external orchestrator executes that request, then returns the result to the model.
This distinction matters because building reliable agents is not just about choosing a stronger model. It is about designing the orchestration layer: permissions, tool schemas, retries, memory, state management, observability, and failure handling.
Agentic systems are powerful because they turn LLMs from text generators into action-taking systems. But they also introduce new risks. If the model has access to real tools, then mistakes can have real-world consequences. That is why strong guardrails, approval flows, audit logs, and bounded permissions are essential.
7. Evaluation: The CI/CD Pipeline for AI Quality
Traditional software can be tested with deterministic unit tests. AI systems are harder because outputs are probabilistic, subjective, and context-dependent.
A good AI answer may depend on accuracy, usefulness, tone, safety, format, reasoning quality, latency, cost, and user preference. No single metric captures all of this.
That is why modern AI evaluation uses several layers.
Static benchmarks such as coding tests, knowledge exams, or reasoning datasets provide baseline signals. Human preference testing provides real-world feedback. Arena-style pairwise comparisons allow users to choose which response is better. LLM-as-a-judge systems use strong models to evaluate outputs at scale.
The software analogy is CI/CD for AI quality. Evaluation pipelines detect regressions, compare model versions, validate prompts, and measure whether changes improve or degrade the user experience.
However, evaluation has traps. Static benchmarks can be contaminated if test data appears in training data. LLM judges can have biases, such as preferring longer responses, more confident wording, or outputs similar to their own style. Human preference data can also be inconsistent.
The lesson is that AI evaluation must be multi-dimensional. A model with the highest benchmark score is not always the best model for your product. The best model is the one that performs reliably on your actual use case, with your users, your data, your latency requirements, and your risk profile.
The Bigger Picture: Modern AI Is a System, Not Just a Model
The most important mindset shift is this:
Modern AI products are not built by simply calling an LLM API. They are built by designing an intelligent system around the model.
The model matters, but so do the surrounding layers:
- tokenization affects cost and context efficiency
- embeddings enable semantic search and memory
- transformers provide the reasoning substrate
- KV caching affects latency and scalability
- alignment shapes usability and safety
- RAG grounds answers in trusted knowledge
- tools turn language into action
- agents orchestrate multi-step workflows
- evaluation keeps quality from drifting
This is why the AI landscape is moving from "prompt engineering" to AI systems engineering.
The winners will not simply be the teams using the largest model. They will be the teams that understand how to combine models, retrieval, tools, memory, orchestration, observability, and evaluation into reliable products.
In other words, the future of AI belongs not only to model builders, but to system designers.
The text box is only the surface. The real intelligence is in the architecture behind it.