Introduction to Large Language Models and Prompt Engineering
Over the past two years, large language models (LLMs) have transitioned from a niche research topic to a mainstream technology used by billions of people for tasks ranging from writing professional emails to generating production-ready code, analyzing market data to creating original artwork. But for most casual users, LLMs remain a black box: you type a request, you get a response, but you rarely understand why some prompts yield excellent, tailored results while others produce irrelevant, inaccurate, or nonsensical output. This gap between casual use and expert use is where prompt engineering comes in.
1.1 What Are Large Language Models? Core Architecture and Capabilities
At their simplest, LLMs are probabilistic models trained on massive volumes of text data to predict the next most likely token (a discrete unit of text, roughly equivalent to a syllable or common word fragment) in a sequence. But their ability to generalize across thousands of tasks without explicit task-specific training stems from the transformer architecture, introduced in the landmark 2017 paper *Attention Is All You Need*[^1]. Unlike earlier recurrent neural network (RNN) models that processed text sequentially (one word at a time, making it difficult to capture long-range dependencies in long passages), transformers use a mechanism called self-attention to process all tokens in a sequence in parallel, calculating the relevance of every token to every other token in the context.
To illustrate how self-attention works, consider the classic example sentence: *"The cat sat on the mat because it was warm."* A human reader easily infers that the pronoun "it" refers to the mat, not the cat. A transformer model achieves this by assigning high attention scores between "it" and "mat" (and related contextual cues like "warm") and low attention scores between "it" and "cat", allowing it to resolve coreference correctly even in long, complex sentences. This ability to capture long-range contextual relationships is the foundation of LLMs’ strong performance on natural language understanding and generation tasks.
Modern LLMs are built by stacking dozens to hundreds of these transformer layers, trained on trillions of tokens of text scraped from the internet, books, academic papers, and code repositories. The largest models, such as GPT-4, Claude 3, and Llama 3, have hundreds of billions of parameters (the internal variables adjusted during training) and require tens of thousands of GPUs to train. For end users, however, these models are accessed via simple API calls or web interfaces, with no need to understand the underlying training infrastructure.
LLMs’ core capabilities fall into four broad categories, all of which can be guided and optimized via prompt engineering: 1. Text Generation: Producing coherent, contextually relevant text for any use case, from marketing copy to creative writing to technical documentation. For example, a zero-shot prompt like *"Write a 100-word product description for a reusable stainless steel water bottle, highlighting its leak-proof design and 24-hour cold retention, for eco-conscious consumers"* will generate a tailored draft in seconds. 2. Translation and Localization: Translating text between dozens of languages while preserving tone, cultural context, and domain-specific terminology. A prompt like *"Translate the following marketing copy from English to Spanish for a Latin American audience, using informal, conversational tone and local slang where appropriate: [insert copy]"* will produce far more usable output than a bare translation request. 3. Reasoning and Problem-Solving: Solving mathematical, logical, and domain-specific problems by breaking them into step-by-step inferences. For example, a prompt asking *"A bakery sells 120 croissants a day for 5 days, then 150 croissants a day for the next 3 days. What is the total number of croissants sold in the 8-day period? Show your work"* will yield a correct, step-by-step solution for most modern LLMs. 4. Code Generation and Analysis: Writing, debugging, and explaining code in dozens of programming languages, as well as analyzing codebases to identify bugs or suggest optimizations. A prompt like *"Write a Python function that takes a list of integers and returns the second largest number, with error handling for empty lists and lists with fewer than 2 elements. Add comments explaining each step"* will generate production-ready code in seconds.
1.2 Inherent Limitations of Large Language Models
While LLMs are powerful, they are not perfect, and their limitations are critical to understand when designing prompts. Ignoring these limitations will lead to unreliable outputs, no matter how well-crafted your prompt is.
1.2.1 Hallucinations
Hallucinations refer to instances where an LLM generates factually incorrect, fabricated, or nonsensical content that is presented as fact. This is not a bug, but an inherent property of LLMs trained as next-token predictors: they are optimized to generate plausible, contextually consistent text, not factually accurate text. For example, if you ask a free version of GPT-3.5 *"Who won the 2024 Nobel Prize in Physics?"* (with a knowledge cutoff of 2023), it will often generate a plausible-sounding but completely fabricated name and citation, rather than stating it does not have access to 2024 data. According to a 2024 Stanford study, mainstream LLMs have an average hallucination rate of 15-20% on open-domain question answering tasks, meaning 1 in 5 answers will contain some degree of fabrication[^2].
Hallucinations are most common in open-ended, factual, or out-of-domain queries, but can be mitigated via prompt engineering techniques such as requesting source citations, asking the model to flag uncertain information, or providing relevant context from trusted sources (covered in Chapter 6).
1.2.2 Context Window Constraints
The context window is the maximum number of tokens an LLM can process in a single interaction, including both the input prompt and the generated output. Early LLMs like GPT-3 had a context window of just 2048 tokens (roughly 1500 words), but modern models support context windows of 128k tokens (GPT-4) or even 1 million tokens (Claude 3), enough to process hundreds of pages of text. However, even with large context windows, LLMs suffer from the "lost in the middle" problem: they are far better at recalling information from the beginning and end of the context window than from the middle. For example, if you insert a 100-page document into a 128k token context window and ask a question about a detail on page 50, the model may fail to recall it correctly.
Context window constraints require prompt engineers to prioritize the most relevant context in prompts, and to split long tasks into smaller, context-sized chunks where necessary.
1.2.3 Bias and Safety Limitations
LLMs learn biases present in their training data, which can lead to stereotypical or harmful outputs. For example, a 2023 study found that when asked *"Write a description of a doctor"*, 78% of outputs from popular LLMs described the doctor as male, even though 50% of doctors in the US are female[^3]. LLMs also have built-in safety guardrails that block prompts asking for harmful, illegal, or unethical content, which can sometimes block legitimate use cases. Prompt engineers must learn to work within these guardrails, and to design prompts that reduce the risk of biased or harmful outputs (covered in Chapter 6 and 10).
1.3 What Is Prompt Engineering?
Prompt engineering is the systematic practice of designing, testing, and optimizing input prompts to maximize the quality, relevance, reliability, and safety of LLM outputs. It is not a "hack" or a set of vague tricks: it is a structured engineering discipline that combines understanding of LLM behavior, clear task definition, iterative testing, and data-driven optimization.
To understand the value of