AB
AiBoss
Wiki

What is Context Engineering? A Comprehensive Guide - AI Encyclopedia

Context engineering refers to building a dynamic system that provides the right information and tools in the correct format, enabling the Large Language Model (LLM) to perform its tasks appropriately. It is a systematic approach to ensure that the LLM obtains the necessary information and tools to perform its tasks...

上下文工程是什么?一文读懂 - AI百科知识

Context engineering is...artificialintelligent(AIIn the field of language models, especially large-scale language modelsLLMThis concept is gradually emerging and gaining increasing importance in applications. It can be understood as a systematic engineering approach. Its focus is on building dynamic systems in order to...AIModels, especiallyLLMIt provides accurate, relevant, and timely information and tools, enabling models to perform specific tasks more rationally and effectively. This contrasts with traditional prompting engineering (…).Prompt Context engineering, while primarily focused on instructions and examples within a single interaction, places greater emphasis on designing and implementing complete systems capable of capturing, storing, retrieving, and managing contextual information. This means context engineering focuses not only on "what to say," but also on "in what context," and how to build and utilize this "context" to enhance...AIThe overall performance. This involves the...AIA comprehensive consideration of the model's operating environment includes the scope of knowledge the model can access, the organizational structure of this knowledge, and how to effectively transfer this knowledge during interactions. Context engineering can be viewed as a more macroscopic and systematic approach.AIOptimize strategies by releasing [resources] through granular context management.AIThe full potential of the model.

What is context engineering?

Context engineering refers to building a dynamic system that provides the right information and tools in the right format, enabling large language models (LLMIt can complete tasks reasonably. It is a systematic method to ensure...LLMObtain all the necessary conditions to perform the task.

Understanding from multiple dimensions

From an operational perspective

It involves a series of technologies and methods used inAIBefore a model executes a task, relevant contextual information is purposefully built and provided for it. This information can come from various sources, such as pre-defined instructions from developers, real-time user input, historical interaction records, results from external tool calls, external knowledge bases, and real-time environmental information.

From a system design perspective

Context engineering emphasizes building capabilitiesautomaticA system for processing context information includes stages such as context acquisition, representation, storage, retrieval, updating, and evaluation. This requires developers to not only consider how to obtain valid context, but also how to...High efficiencyThis allows for the effective management and utilization of these contexts. For example, the LangGraph framework is designed to provide maximum control, allowing developers to precisely control the execution and input of each step.LLMThis allows for fine-grained management of the content and output storage methods of the context project.

From a goal-oriented perspective

The ultimate goal of context engineering is to improveAIThe model's performance in complex tasks demonstrates a better understanding of user intent, generates more accurate responses, makes more reasonable decisions, and provides more...intelligentAnd personalized services. As some researchers have pointed out, context is what distinguishes a mediocre service.AIContext engineering is the core element of an assistant and a super assistant, and it is the key to achieving this transformation.

Contextual engineering vs. hint engineeringPrompt Engineering)

Context Engineering and Hint EngineeringPrompt Engineering is a large-scale language model (LLMThese are two closely related but distinct core concepts in application development. Both aim to improve...LLMWhile both focus on output quality and task completion capabilities, their areas of focus, methodologies, and application scope differ significantly. Understanding the relationships between them is crucial for effectively designing and optimizing [the system/mechanism].AIApplications are crucial.

Tip Project (Prompt Engineering) The main focus is on how to design and optimize input.LLMThe "hint"PromptThis refers to instructions or questions directly provided by the user, and may include a few examples, task descriptions, output format requirements, etc. The core objective is to find an optimal textual expression to guide...LLMUnderstanding user intent and generating expected output is key. Cue engineering is more like the "art and science of communicating with the model," exploring how to "stimulate" specific capabilities of the model through carefully chosen words, sentence structures, and special symbols. For example, in text summarization tasks, cue engineering might try different instructions, such as "Give a short summary of the following article:", "Summarize the main points of this article:", or "TL;DR:" (short for Too Long; Didn't Read), evaluating which cue yields better summarization results. Cue engineering focuses on optimizing single interactions or individual tasks, with a methodology emphasizing experimentation, iteration, and skill development.

Context Engineering It operates at a more macro, more systemic level. It not only focuses on prompts for direct user input, but also on...LLMThe entire "information environment," or context, is constructed and managed. This context can include, but is not limited to: dialogue history, user profiles, domain knowledge bases, real-time data, API call results, and any other relevant information that helps the model better understand the current task and generate better output. The core goal of context engineering is to systematically design, acquire, organize, store, retrieve, update, and evaluate this contextual information to ensure...LLMIt can consistently obtain high-quality, highly relevant input. It's more like "building for the model".intelligent"Engineering disciplines of information ecosystems". For example, in aintelligentIn customer service systems, context engineering must consider the user's current question (prompt), as well as their historical inquiry records, purchase records, current session state, and relevant product knowledge bases and FAQ documents, effectively integrating and conveying this information.LLM.

Hint engineering and context engineering are not independent of each other, but rather complementary.High-quality prompts remain an indispensable part of context engineering, directly expressing user intent. Context engineering provides a solid foundation and rich background information for the effective execution of prompts. In many advanced...AIIn applications, the two are often used in combination: rich background information is acquired and organized through context engineering, and then this information is effectively delivered through well-designed prompts.LLMAnd guide them to complete complex tasks.

Core features of context engineering

Systematic and complete

The requirement is to move beyond fragmented, ad-hoc context provisioning and instead build a comprehensive system that fully covers the context lifecycle, including capture, storage, retrieval, updating, evaluation, and optimization. This contrasts with traditional prompting engineering, which primarily focuses on immediate instructions within a single interaction.

Dynamism and Adaptability

Context engineering emphasizes that contextual information should be dynamic, capable of being adjusted and updated in real time based on task progress, user feedback, and environmental changes. For example, in long conversations, the system needs to be able to summarize previous conversation content and use it as context for subsequent interactions.

Relevance and accuracy

Not all contextual information is useful. Context engineering identifies and provides the information most influential on the current task, avoiding information overload or the introduction of noise. It requires a deep understanding of task requirements and model characteristics.

Actively build and optimize

Context engineering is not about passively waiting for context to emerge, but rather about proactively designing and building mechanisms and processes that can generate and deliver high-quality context, optimizing its quality and effectiveness through continuous feedback and iteration. This proactive approach and optimization mindset are key aspects of an engineering methodology.

An important manifestation of context engineering

Helps to eliminate ambiguity

Natural language itself is highly ambiguous; the same word or sentence can have completely different meanings in different contexts. By providing sufficient contextual information,AIThe model can grasp the semantics more accurately and make the correct judgments.

Enhance the model's reasoning ability

Many complex tasks require models to perform multi-step reasoning based on existing information, and rich context can provide the necessary premises and constraints for such reasoning. For example, when planning a meeting schedule, the model needs to consider multiple contextual factors such as the participants' available time, the meeting location, and the agenda topics.

Achieve personalization andintelligentThe foundation of digital services

By learning and leveraging users' personalized context (such as historical behavior, preferences, habits, etc.).AIThe model can provide more customized services tailored to user needs. For example, a news article...recommendIf the system understands users' levels of interest in different topics, it can push more relevant news. As some researchers have pointed out,Context is the key factor in distinguishing a "dumb assistant" from a "super teammate". A tool that effectively utilizes contextAIThe system will perform better.intelligentNature andHigh efficiencyThis greatly improves user experience and task completion efficiency.

How Context Engineering Works

Dynamic Context Construction

Context engineering emphasizes dynamism, meaning it involves acquiring contextual information from multiple sources in real time based on task requirements, including user input, historical interactions, and external data. This dynamic construction process requires the system to be able to perceive and respond to environmental changes in real time, ensuring that the information received by the model is accurate and timely.up to dateOf or related to.

  • User InputUser input is the most direct source of context, including the user's current query, instructions, and feedback. Capturing and accurately understanding user input in real time is the first step in building effective context.
  • Conversation HistoryFor multi-turn dialogue applications, historical interaction records are crucial context. This includes previous question-and-answer sessions, user preferences, and the system's previous responses. Through techniques such as dialogue history summarization, vector storage, and retrieval, both short-term and long-term dialogue memory can be achieved, ensuring...AgentMaintain coherence and relevance throughout multiple rounds of interaction.
  • External Knowledge BasesThese sources include databases, APIs, document repositories (such as those retrieved via RAG technology), and knowledge graphs. These sources can provide the model with domain-specific knowledge or real-time information.
  • System Status & Environmental InformationFor example, the current time, user's geographical location, device type, and the current state of the application can all serve as important contextual clues.
  • Tool Outputs:whenLLMWhen external tools (such as calculators, code executors, and search engines) are invoked, the output of these tools also constitutes new contextual information.

Integration of information and tools

Context engineering focuses on the information itself, emphasizing its format and the provision of relevant tools. It requires integrating information from different sources (such as text, images, and audio) into a format suitable for model processing, and providing necessary tools (such as retrieval tools and external APIs) to help the model complete its tasks.

Precise context management

Context engineering requires granular management of context to avoid information overload or underload. Too much information can increase the model's inference cost, while insufficient information can prevent the model from accurately completing the task. Context management needs to consider the timeliness, relevance, and accuracy of the context.

Probabilistic and Deterministic Contexts

Context engineering categorizes context into deterministic context (such as instructions and rules sent directly to the model) and probabilistic context (such as information retrieved from external tools or the web). Deterministic context can be precisely controlled, while probabilistic context requires engineering design to optimize its impact on the model.

Context compression and optimization

To improve model efficiency, context engineering also involves context compression techniques, which compress large amounts of information into a format that the model can use.High efficiencyThe processing methods include summarizing historical information and extracting key information to ensure that the model obtains optimal information within a limited context window.

Search Enhancement Generation (RAG)

Retrieval-enhanced generation (RAG) is a key technique in context engineering. By retrieving relevant information from external data sources and integrating it into the model's context window, the model's knowledge boundary is expanded. This approach can significantly improve the model's performance when handling complex tasks.

MultimodalFusion

In some application scenarios, context engineering also involvesMultimodalInformation fusion, such as combining visual information (images, videos) with textual information, helps the model to understand the task context more comprehensively.

Context engineering vs. Retrieval Enhancement Generation (RAG)

Context engineering and Retrieval Augmented Generation (RAG) are current technologies used in large-scale language models (LLMIn application, RAG (Rich Artificial Intelligence) and context engineering are two closely related concepts that improve model performance and output quality by introducing external information. They exhibit significant hierarchical relationships and differences in scope, methodology, and objectives. Understanding these differences helps to more clearly position RAG within the context engineering framework and to grasp the broader implications of context engineering.

Search Enhancement Generation (RAG)

It is a specific technological paradigm, and the core process is...LLMBefore generating an answer, the system first retrieves the most relevant document fragments or information to the user query or current task from one or more external knowledge bases (such as vector databases or document collections). This retrieved information is then used as "context" or "reference" to complement the original user prompt.PromptEnter them together.LLM.LLMBased on this enhanced contextual information, more accurate, relevant, and factual answers are generated. The main goal of RAG is to solve...LLMThe RAG addresses the "knowledge cutoff" problem (where a model's knowledge is limited to a specific point in time in its training data) and the "illusion" problem (where a model may generate seemingly plausible but inaccurate or completely false information). By introducing external knowledge sources, RAG enables...LLMAble to access and utilize data beyond training dataup to dateOr information specific to a particular field. For example, in a RAG-based question-and-answer system, when a user inquires about the technical specifications of a specific product, the system first retrieves relevant information from the product manual or technical document library, and then provides this information along with the user's question.LLMGenerate the answer.

Context Engineering

It is a broader and more systematic concept. It is not limited to acquiring external knowledge through retrieval, but encompasses...LLMContext engineering encompasses the entire process of designing, building, managing, and optimizing the "context" required for its operation. This context can include a variety of information sources and types, far exceeding the document fragments commonly used in RAG. The goal of context engineering is to ensure...LLMIt provides access to the most relevant, accurate, and complete information at all times, supporting the completion of complex tasks. This includes:

  • Multi-source context integrationIn addition to the document knowledge base that RAG relies on, context engineering integrates data from structured databases, real-time APIs, user session history, system status, and others.AIThe model's output, as well as the results of calling external tools (such as calculators, code executors, APIs), etc.
  • Dynamic Context ManagementContext engineering emphasizes the dynamism of the context, meaning that the context needs to be updated and adjusted in real time based on the progress of the task, user feedback, and changes in the environment.
  • Provide the right toolsContext engineering focuses on information, specifically...LLMProvide the tools needed to complete the task and manage the use of these tools.
  • Information Format and RepresentationContext engineering focuses on how to organize and present information in the most effective way.LLMThis involves summarizing, structuring, and integrating information with other contexts.

RAG can be seen as an important technology or a key implementation module in the context engineering toolkit.A well-developed context engineering system might leverage RAG as a means of acquiring external knowledge, and would also include many other components and strategies to build and manage a broader context.

Application scenarios of context engineering

intelligentCustomer service and dialogue system

Context engineering inintelligentCustomer service and dialogue systems can significantly improve user experience and problem-solving efficiency. Traditional rule-based or...SimpleMachine LearningDialogue systems often lack the ability to understand context, resulting in stiff, disjointed conversations and difficulty in handling complex multi-turn interactions. By introducing context engineering, systems can better understand user intent, remember dialogue history, and utilize relevant information to provide more accurate and personalized services.

For example, when a customer inquires about the order status, a dialogue system with good context engineering capabilities can...automaticThe system identifies users, queries their recent order information, and provides targeted responses based on the order's specific status (e.g., paid, awaiting shipment, in transit, signed for), without requiring users to repeatedly provide order numbers or query details. This capability relies on the system's ability to effectively acquire and utilize various contextual information, including user identity, order database, and logistics information.

Suppose a user previously inquired about a product's return policy and contacts them again a few days later asking, "How do I return the item I bought?" A system without context memory might require the user to provide product information or order number again. However, a system employing context engineering can identify the user and, combined with previous conversation history,...automaticUnderstand that "that thing" refers to the product discussed earlier, and provide direct guidance on the corresponding return process.

This involvesEffective management and utilization of short-term memory (current conversation content) and long-term memory (user's historical interactions and preferences). Context engineering can help dialogue systems better handle complex linguistic phenomena such as topic shifts and referential resolution.

For example, if a user is discussing product A and then suddenly asks, "What about the other one?", the system needs to be able to infer from the previous context that "the other one" might refer to product B, which was compared earlier.

Through a well-designed context management strategyintelligentCustomer service and conversation systems can achieve a more natural, smoother, and more...High efficiencyEnhance user interaction and improve user satisfaction and loyalty.

Content generation and summary extraction in progress

Context engineering can significantly improve the quality, relevance, and personalization of generated content in the fields of content generation and summarization. In content generation, whether writing marketing copy, press releases, creative stories, or technical documents, it can enhance the performance of large language models (…).LLMProviding sufficient contextual information is crucial.

For example, when generating a product promotion copy, ifLLMGaining contextual information about the target audience (age, interests, consumption habits), product features, brand tone, and current marketing campaigns will result in more engaging copy and higher conversion rates. When writing technical documentation, providing project architecture diagrams, API documentation, and relevant code snippets as context can be helpful.LLMGenerate more accurate and easier-to-understand document content. Context engineering plays a role here in ensuring...LLMBeing able to "have a clear understanding" during the generation process can prevent the generation of vague or irrelevant content.

In terms of abstract extraction, traditional algorithms often focus only on the statistical features of the text itself, neglecting the specific context and user needs. By introducing context engineering, abstracting systems can generate more targeted and effective abstracts.practicalSummary of sexuality.

For example, for a long research report, different users may focus on different aspects: investors may be concerned with market trends and investment returns, while researchers may be more focused on methodology and innovative points. Context engineering can analyze user identity, query intent, or historical preferences to provide...LLMProvide relevant context to generate personalized summaries that meet specific user needs. When dealing with summarizing tasks involving multiple documents or requiring cross-document information integration, context engineering can help the system effectively organize and utilize information from different sources to generate comprehensive and coherent summaries.

For example, the retrieval function provided by frameworks such as LangChain allows the system to dynamically retrieve relevant information from external documents and insert it into the prompts, which is itself an application of context engineering in summary extraction.

Through refined context management, content generation and summary extraction systems can better serve specific scenarios and users, improving the efficiency and effectiveness of information delivery.

Code generation and software-aided development

Context engineering can significantly improve development efficiency, code quality, and developer experience in code generation and software-aided development. Large language models (LLMThe emergence of tools such as GitHub Copilot has proven that...AIIn terms of assistive programming capabilities. However, for these tools to truly understand the developer's intent and generate high-quality code, simply relying on the current code snippet or...SimpleThe annotations are often insufficient.

Context engineering by providingLLMProviding richer and more precise contextual information can significantly improve code generation. This contextual information can include the entire project's codebase structure, relevant API documentation, specific library usage guidelines, existing test cases, and the developer's coding style preferences. For example, when a developer inputs a function name and a brief description, a code generation tool with good context engineering capabilities can consult other related functions in the project, understand their call relationships and data flow, and generate code that is consistent in style, matches interfaces, and conforms to project specifications.

In software development, context engineering is used for code completion, bug fixing, code explanation, test case generation, and documentation.automaticEffective contextual information can significantly improve the generation process and other stages.AIThe performance of auxiliary tools.

For example, Alibaba's Tongyi Lingma can, based on the developer's task description,automaticIt can perceive the project framework, technology stack, required code files, error messages, etc., without manually adding project context. During inline code generation, it can utilize the current code file and cross-file context, as well as the codebase-level context, to generate line-level or function-level code suggestions in real time. Furthermore, some advanced systems are beginning to explore pre-generated context strategies.

For example, DeepWiki generates a structured wiki-like knowledge base and pre-indexes it by conducting a comprehensive analysis of code repositories (including code, README, and configuration files), providing...AIProvides structured documents, interactive diagrams, project feature/architecture descriptions, and other contextual information. Context7 parses project documents and uses...LLMExtract code snippets and metadata, generate vector embeddings, preprocess the entire project documentation, filter as needed, and provide code snippets, vector embeddings, and other data with accompanying metadata and descriptions.LLMOptimized llms.txt file and other context.

This refined context management and optimization enablesAIProgramming assistants can become developerspowerfulProductivity tools.

PersonalizationrecommendIn information retrieval

The goal is to provide the most relevant and valuable information or items based on the user's specific context and needs. TraditionalrecommendSystems often rely on users' historical behavioral data (such as ratings and purchase records) and item attribute information, while context-aware systems...recommendThe system (Context-Aware Recommender Systems, CARS) further incorporates "contextual information" into its considerations. This contextual information can be diverse, such as the user's time (e.g., weekday/weekend, daytime/nighttime), location (e.g., home/office/commuting), device (e.g., mobile phone/computer), current social environment, emotional state, or more granular contexts such as weather, type of activity, etc.

The application of context engineering in these systems involves context modeling, reasoning, and...recommendFusion in algorithms. Common context modeling methods include key-value pair models, markup language models, object-oriented models, logical models, and ontology models.

For example, a movierecommendThe system may give users [services/deliveries] on weekends.recommendLighthearted comedies, perhaps, are more suitable for weekday evenings.recommendDrama films, that's based on a time context.SimpleApplications. More complex context engineering might involve using Bayesian networks or rule-based reasoning for context inference, and using techniques such as tensor decomposition to jointly model user, item, and contextual information.

In the field of information retrieval, context engineering helps search engines better understand users' query intent, especially for fuzzy or conversational queries. By analyzing contextual information such as users' historical search records, geographical location, and current trending events, search engines can provide more accurate search results.

For example, when searching for "apple," the search results can be adjusted based on whether the user has previously searched for electronics or fruit, or whether there is an Apple retail store at the user's current location. Frameworks like LangChain also support dynamically retrieving information from a knowledge base that is relevant to the user's query and context through Retrieval Augmentation (RAG), thereby improving the accuracy of question answering and information provision.

Challenges of Context Engineering

Effective acquisition and integration of information

ComplexintelligentbodyIt may be necessary to dynamically obtain context from multiple heterogeneous sources (such as application developers, user input, historical interactions, tool call results, external databases, etc.). How can we ensure the accuracy and real-time nature of this information, and present it in a...LLMIntegrating data into easily understandable and digestible formats is a complex systems engineering problem. For example, data from different sources may have different structures and semantics, requiring effective cleaning, transformation, and alignment.

LLMLimitations and optimizations of context windows

Despite modernLLMWhile the context window is constantly increasing, the limit on the number of tokens remains for processing very long documents or complex tasks requiring long-term memory. How to select and provide the most relevant and informative context within a limited context window, avoiding information overload or truncation of key information, is a crucial aspect of context engineering that requires careful design. Research shows that...LLMAttention to information in different locations within a context window is not equal; information is easily diluted in the middle, and piling up similar content can lead to "attention interference."

Depth and Dynamic Adaptability of Contextual Understanding

Only toLLMProviding raw context data may not be sufficient; the system also needs a certain level of reasoning ability to understand implicit information, relationships, and dynamic changes within the context. For example, in long conversations, how can the system effectively maintain and update the dialogue history as context to ensure...LLMAccurately understanding the current dialogue state and user intent is a complex problem. When contextual information changes or contradictions arise, the system needs to be able to dynamically adjust its behavior and decisions.

Evaluate and measure the effectiveness of context engineering

How to quantify the impact of different contextual informationLLMWhat is the contribution to task performance? How do we evaluate the merits of context engineering strategies? Effective evaluation metrics and methods are needed for both.

Data privacy and security

How to provide personalized services while leveraging user context and protecting users' sensitive information is a problem that must be carefully considered.

The Development Prospects of Context Engineering

Context engineering will be moreintelligentHuaheautomaticchange

Future context management systems will be more than just...SimpleIt doesn't just transmit information; it proactively understands task requirements.intelligentThis involves filtering, integrating, summarizing, and even inferring the most relevant context, presenting it to the model in the optimal way. This involves more advanced...AIacting(AgentThis technology enables context management to possess a certain degree of autonomous decision-making and learning capabilities.

MultimodalIntegration with cross-domain contexts will become the norm.

along withMultimodalLLMWith the increasing prevalence of this technology, context engineering needs to process information from multiple sources, including text, images, audio, and video, effectively integrating them to provide models with a unified and comprehensive world view. Context engineering will also break down barriers between different domains of knowledge, enabling the sharing and utilization of cross-domain contexts and empowering broader general applications.artificialintelligentapplication.

The construction of long-term memory and personalized context will become more sophisticated.

To support more persistent interactions and deeper personalized services, context engineering needs to develop more effective long-term memory mechanisms that can accurately and securely store and retrieve key information such as users' long-term preferences and historical behaviors.AIThe system can truly "understand" users and provide a personalized experience for each individual.intelligentbodyVerification.

Greater emphasis on trustworthiness and security

along withAIWith its increasingly widespread application in social life, ensuring the authenticity of contextual information, avoiding the introduction of bias, protecting user privacy, and preventing malicious use will become important considerations in context engineering design and practice. Interpretable context management and verifiable context sources will be crucial.

Tools and platforms will become more complete and more widely used.

As the technology matures and best practices are accumulated, more standardized, modular, and easy-to-use context engineering frameworks, libraries, and platforms will emerge, greatly reducing the barrier to entry for developers and promoting the widespread application and innovation of context engineering technology across various industries.

HarmonyOSintelligentbodyWhat is the HMAF (Hardware Frame for Architects)? Understand it in one article - AIEncyclopedic knowledge

AIintelligentbody(AI AgentWhat is it? Understand in one article - AIEncyclopedic knowledge