How Large Language Models Work: Beginner's Guide
Blog Post

How Large Language Models Work: Beginner's Guide

Jake McCluskey
Back to blog

You've been using ChatGPT or Claude for months, maybe longer. You know how to write prompts, you've seen it generate impressive responses, and you've probably also watched it confidently fabricate complete nonsense. But what's actually happening when you hit send? Large language models work by breaking your text into tokens, processing them through transformer neural networks that weigh relationships between words, and predicting what comes next based on patterns learned from billions of training examples. This explainer walks through the mechanics step by step, from how your prompt gets chopped up to why the model sometimes hallucinates, so you can write better prompts and understand what these tools can actually do.

What Are Tokens in Large Language Models

Before an LLM can process anything you write, it splits your text into tokens. A token isn't always a whole word. Usually a chunk of 3-4 characters, though common words like "the" or "cat" get their own single token.

The sentence "ChatGPT is helpful" becomes roughly four tokens: ["Chat", "GPT", " is", " helpful"]. Notice the space before "is" counts as part of that token. This matters because every model has a context window measured in tokens, not words or characters.

GPT-4 Turbo supports 128,000 tokens, which translates to roughly 96,000 words of English text. Claude 3.5 Sonnet offers 200,000 tokens. When you hit that limit, the model can't see anything beyond it. That's why pasting an entire 400-page manual sometimes produces responses that ignore sections near the end.

Tokenization also explains why LLMs struggle with tasks like reversing words or counting letters. The model never sees individual characters during processing, just these pre-chunked tokens, so asking it to spell "strawberry" backwards forces it to work from fragmented pieces rather than the actual letters.

How Transformers Work in AI Explained Simply

The transformer is the architecture that makes modern LLMs possible. Before transformers, AI models processed text sequentially, one word at a time, like reading a sentence from left to right without looking ahead. Transformers process all tokens simultaneously and calculate which tokens should pay attention to which other tokens.

This happens through something called self-attention. When the model processes the sentence "The cat sat on the mat because it was tired," self-attention helps the model figure out that "it" refers to "cat" rather than "mat" by weighing the relationships between every token and every other token in the sequence.

A typical large language model contains dozens of these attention layers stacked on top of each other. GPT-3 has 96 layers. Each layer refines the understanding of context, building from simple word associations in early layers to complex semantic relationships in deeper layers. Think of it like reading comprehension that gets more sophisticated with each pass.

The transformer architecture is also why LLMs can handle multiple languages, code, and structured data in the same conversation. The attention mechanism doesn't care about the type of content, just the statistical patterns between tokens. If you're curious about how these layers organize different types of knowledge internally, how neural networks organize knowledge internally covers that process in detail.

How Are LLMs Trained Step by Step

Training happens in two distinct phases: pre-training and fine-tuning. Pre-training is where the model learns language patterns from massive datasets. GPT-4 was trained on an estimated 13 trillion tokens of text from books, websites, code repositories, and other sources.

During pre-training, the model learns by predicting the next token. It sees "The capital of France is" and tries to predict "Paris." When it gets it wrong, the training process adjusts the internal weights (the numbers that determine how tokens relate to each other) to make the correct answer more likely next time. This happens billions of times across the entire dataset.

After pre-training, the model can complete sentences and generate coherent text, but it doesn't follow instructions well. Might produce unhelpful or harmful outputs. That's where the second phase comes in: Reinforcement Learning from Human Feedback, or RLHF.

RLHF involves human reviewers ranking different model outputs for the same prompt. If you ask "How do I make a cake?" and the model generates five different responses, humans rate which ones are most helpful, accurate, and safe. The model then learns to prefer outputs that match those human preferences. This is why ChatGPT and Claude sound conversational and try to be helpful rather than just spitting out raw text completion.

Why Training Cutoff Dates Matter

Pre-training is expensive. Training GPT-4 reportedly cost over $100 million in compute resources. Because of this cost, models aren't continuously retrained. They have a knowledge cutoff date, the point where their training data ends.

If you ask GPT-4 about events from last week, it won't know about them unless it has access to web search or another real-time tool. The model's knowledge is frozen at its training cutoff. This is one reason why understanding LLM mechanics helps you troubleshoot: if the model doesn't know something recent, it's not being stubborn or wrong, it literally never saw that information during training.

What Is RAG in AI and How Does It Work

Retrieval-Augmented Generation, or RAG, solves the knowledge cutoff problem by giving LLMs access to external information. Instead of relying only on what the model learned during training, RAG systems retrieve relevant documents or data and inject them into the prompt before the model generates a response.

Here's how it works in practice. You ask a question about your company's internal policies. A RAG system searches your policy database, finds the three most relevant documents, and adds them to your prompt as context. The LLM then generates an answer based on those retrieved documents rather than trying to guess from general knowledge.

RAG is how tools like ChatGPT's file upload feature work. When you upload a PDF, the system chunks the document into smaller sections, converts them into numerical representations called embeddings, and stores them in a vector database. When you ask a question, it retrieves the most relevant chunks and includes them in the context window.

Typical RAG implementations retrieve between 3 and 10 document chunks per query, each usually 500-1000 tokens long. This keeps the context focused without overwhelming the model's attention mechanism. If you're working with complex documents that include tables and charts, how to use multimodal RAG to analyze PDF documents covers advanced techniques for handling visual elements.

How Do LLMs Work Under the Hood During Generation

When you hit send on a prompt, several things happen in sequence. First, tokenization splits your input into tokens. Those tokens get converted into numerical vectors (lists of numbers) that represent their meaning in a high-dimensional space. Words with similar meanings end up with similar vector representations.

These vectors pass through the transformer layers, where self-attention calculates relationships and updates each token's representation based on context. After flowing through all layers, the model outputs a probability distribution over every possible next token in its vocabulary (usually 50,000 to 100,000 options).

The model doesn't just pick the highest probability token every time. That would make outputs repetitive and boring. Instead, it samples from the probability distribution using parameters like temperature. Temperature 0 means always pick the most likely token (deterministic). Temperature 1 means sample proportionally from the probabilities, more creative but less predictable.

This process repeats for every token generated. The model predicts one token, adds it to the sequence, and runs the whole process again to predict the next token. A 500-word response requires roughly 650 individual prediction cycles. That's why longer outputs take more time to generate.

Why Hallucinations Happen

LLMs hallucinate because they're always predicting the next plausible token, not retrieving facts from a database. If the model hasn't seen enough examples of a specific fact during training, it fills in gaps with statistically plausible but incorrect information.

Ask an LLM for a citation to a specific research paper, and it might generate a real-sounding title, author list, and journal name that doesn't actually exist. The model learned the pattern of how citations look, but it's generating a new example rather than recalling a specific one. Studies suggest that even the best models hallucinate factual claims roughly 3-5% of the time in open-ended generation tasks.

This is also why prompt engineering matters. If you ask "What are three benefits of X?" the model will generate three benefits even if only one exists, because you've structurally demanded three items. Asking "What benefits does X have, if any?" gives the model room to say "none" if that's accurate.

How Prompt Engineering Actually Works

Understanding how LLMs process tokens and context changes how you write prompts. Every token in your prompt influences the probability distribution for the next token, so structure matters as much as content.

Putting important instructions at the beginning and end of your prompt works better than burying them in the middle. The model's attention mechanism weighs recent tokens more heavily, and the final tokens before generation have the strongest influence on the output. This is why "Answer in bullet points" works better at the end of a prompt than at the beginning.

Providing examples (few-shot prompting) dramatically improves output quality because it shifts the probability distribution toward the pattern you demonstrate. Instead of asking "Summarize this email," you can show two example emails with your preferred summary style, then provide the new email. The model picks up on the pattern and matches it.

Chain-of-thought prompting, where you ask the model to "think step by step," works because it forces the model to generate intermediate reasoning tokens before the final answer. Those intermediate tokens change the context and often lead to more accurate conclusions. For complex business workflows, how to use agentic AI to automate repetitive processes shows how to structure multi-step prompts for consistent results.

How AI Agents Use LLMs to Take Actions

An AI agent is an LLM with the ability to call external tools and take actions based on its outputs. Instead of just generating text, the agent can search the web, run code, query databases, or trigger API calls.

This works through a reasoning loop. You give the agent a task like "Find the three cheapest flights to Tokyo next month and add them to a spreadsheet." The agent breaks this into steps: search for flights, compare prices, identify the top options, format the data, write to a spreadsheet. At each step, it decides which tool to use and what parameters to pass.

Modern agent frameworks let LLMs call functions by generating structured output. The model outputs something like search_flights(destination="Tokyo", departure_after="2025-02-01"), the system executes that function, and the results get added back into the context for the next reasoning step.

Agents can fail in interesting ways because they're still predicting plausible next steps, not executing a guaranteed algorithm. An agent might call the same API five times with slightly different parameters because it's not tracking state perfectly, or it might confidently skip a step that seems obvious to a human. And honestly, most teams skip this part: roughly 30-40% of complex multi-step agent tasks require human review or correction in current implementations.

Why Understanding This Matters for Your Work

Knowing how LLMs work changes how you use them. You'll recognize when a task is outside the model's capabilities (like precise arithmetic or recalling specific facts without RAG support) and when poor outputs are actually prompt design issues.

You'll understand why context window size matters when choosing between models. If you're analyzing long documents, Claude's 200,000-token window might be worth the cost difference over a smaller model. If you're generating short marketing copy, a smaller context window is fine and cheaper.

You'll also know when to combine multiple approaches. Use RAG for fact-heavy tasks where accuracy matters. Use direct prompting for creative or analytical tasks. Use agents for multi-step workflows that require tool access. Understanding the mechanics helps you pick the right architecture for each job instead of treating every LLM interaction the same way.

Look, the gap between casual users and informed practitioners isn't about knowing the math behind attention mechanisms. It's about understanding enough of the system to predict how it will behave, troubleshoot when it doesn't, and design workflows that work with the technology's actual capabilities rather than against them. That's the difference between getting frustrated when ChatGPT hallucinates a citation and knowing to use RAG with verified sources instead.

Ready to stop reading and start shipping?

Get a free AI-powered SEO audit of your site

We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.

Run my free audit
WANT THE SHORTCUT

Need help applying this to your business?

The post above is the framework. Spend 30 minutes with me and we'll map it to your specific stack, budget, and timeline. No pitch, just a real scoping conversation.

How Large Language Models Work: Beginner's Guide | Elite AI Advantage