What Is Context Engineering? Foundations and Core Concepts
What Is Context Engineering? Foundations and Core Concepts
Every developer who has worked extensively with large language models has experienced a puzzling phenomenon: the same model, given the same prompt, produces wildly different responses depending on what else has been included in the conversation. Ask an AI to write a professional email without specifying the recipient's relationship, and you might get a generic template. Include details about the recipient's personality, the history of your relationship, and the specific outcome you're seeking, and suddenly the model produces something that sounds exactly like you. This is not magic—it is context engineering.
The discipline of context engineering encompasses all the decisions developers and users make about what information to provide to an AI model, how to structure that information, and when to update or refresh it. When done well, context engineering transforms an unpredictable AI tool into a reliable system. When neglected, it leaves you at the mercy of whatever assumptions the model makes from incomplete information.
This chapter establishes the foundational understanding you need for everything that follows in this book. We will define context engineering precisely, explore why the quality of your context directly determines the quality of your outputs, examine the context window as the fundamental technical constraint that shapes all context engineering decisions, and introduce the five pillars framework that organizes the rest of this book. By the end, you will understand not only what context engineering is but why it is essential for building AI applications that actually work in production.
The Nature of Context in AI Systems
To understand context engineering, you must first understand what "context" means in the context of AI systems. When we speak of context, we are referring to all the information that surrounds and informs a specific interaction with an AI model. This includes everything the model can "see" during a given request—the conversation history, any documents or data provided as part of the input, system-level instructions, and even the way information is formatted and ordered.
The critical insight is that AI models do not have persistent memory. Each conversation with a language model is essentially a fresh start from the model's perspective. What appears to be continuity and memory is actually the result of you, the user, re-providing relevant information with each new message. The model has no way to "remember" your previous conversations except through what you explicitly include in the context window. This has profound implications for how we must design AI interactions.
Consider a practical scenario: you are building a customer service chatbot for an e-commerce platform. The chatbot needs to handle order status inquiries, return requests, and product recommendations. Without careful context engineering, you might simply prompt the model with "You are a helpful customer service representative." The model would then rely entirely on its training data—general knowledge about customer service, typical e-commerce interactions, and whatever patterns it learned during training.
Now consider the difference when you provide rich context: "You are a customer service representative for ShopRight, an online retailer specializing in electronics and home appliances. Your tone should be friendly but professional. You have access to customer order history, which you can retrieve using the get_order_status function. When a customer asks about a return, always first confirm their order number and purchase date. Never provide refund timelines without checking the return_policy document, which specifies 30 days for electronics and 60 days for appliances." With this context, the model operates with specific knowledge, clear guidelines, and appropriate tools—producing responses that align with your business requirements rather than generic assumptions.
This contrast illustrates a fundamental principle: the information environment you create for an AI model is as important as the model itself. A powerful model with poor context will underperform a modest model with excellent context. This is why context engineering has emerged as a distinct discipline—it focuses specifically on optimizing that information environment.
Context engineering differs from traditional software development in an important way. In conventional programming, you write explicit instructions that the computer follows deterministically. In context engineering, you design the information space within which an AI model operates, allowing the model flexibility to interpret and respond while guiding it toward desired outcomes through the information you provide. It is a form of indirect control—shaping behavior not through rigid commands but through thoughtful information architecture.
The Context Window: Understanding the Fundamental Constraint
Every context engineering decision is bounded by a technical constraint called the context window. The context window is the maximum amount of text that a language model can process in a single request. This includes your prompt, any system instructions, conversation history, and all supporting documents or data you include. Once you exceed this limit, you must make decisions about what to keep and what to discard.
Modern large language models vary in their context window sizes. As of this writing, some models support context windows of 128,000 tokens (approximately 100,000 words or about 300 pages of text), while others offer 32,000 tokens or 8,000 tokens. The specific number matters less than understanding the implications: context windows are finite, and this finiteness forces every context engineer to make strategic decisions about prioritization.
A token is the basic unit of text that models process. Roughly speaking, one token equals four characters of English text, or about three-quarters of a word on average. This means a 32,000-token context window can hold approximately 24,000 words—about a novella's worth of text. This sounds generous until you begin building real applications. A comprehensive legal document might consume 15,000 tokens. Six months of customer support conversation history could easily exceed 50,000 tokens. The context window that seemed large quickly becomes a scarce resource that requires careful management.
Understanding tokenization is essential for practical context engineering. Words are not the unit of measurement—tokens are. Consider how the phrase "context engineering" breaks down: "context" might be one token, " engineering" another. But compound words, numbers, and punctuation all affect tokenization differently. The word "running" might be one token, while "running," (with a comma) might be two. This is why you should always measure actual token counts rather than relying on word counts when planning your context.
The practical implications of context window limits shape every aspect of context engineering. When your context approaches its limit, you face a set of difficult choices. You might truncate conversation history, losing valuable information about the ongoing interaction. You might compress documents, potentially losing important details. You might omit certain documents entirely, forcing the model to operate without potentially crucial information. Each choice involves trade-offs, and context engineering provides the frameworks for making these trade-offs strategically.
There is also a performance consideration beyond simple limits. Early research and practical experience suggest that models sometimes pay more attention to information at the beginning and end of the context window than to information in the middle. This "lost in the middle" phenomenon means that placing critical information at the boundaries of your context may be more effective than burying it in the middle of a large document. Cont