Context Engineering Guide
Magic Translation Bureau is a translation team under 36Kr, focusing on fields such as technology, business, the workplace, and life, with a particular emphasis on introducing new technologies, new ideas, and new trends from abroad.
Editor's note: Prompt engineering is evolving into context engineering! This article uses a case study of an n8n agent to reveal that structured output, dynamic time injection, and RAG cache optimization have become the three core strategies for enhancing the efficiency of large language models (LLMs). This article is a compilation.
What is context engineering?
A few years ago, many top AI researchers even claimed that prompt engineering should have died out by now.
Obviously, this conclusion is completely wrong. In fact, prompt engineering is more important than ever.
It's so important that it is now being repackaged as "context engineering."
Yes, it's another fancy term used to describe the crucial process of adjusting the instructions and relevant background required for an LLM to perform tasks.
There has been much discussion about context engineering (by Ankur Goyal, Walden Yan, Tobi Lutke, Andrej Karpathy, etc.), but I'd like to share my own perspective and demonstrate the practical application of context engineering through the specific steps of developing an AI agent workflow.
Although it's unclear who first coined the term "context engineering," we'll briefly explain its meaning based on this schematic diagram by Dex Horthy.
Personally, I prefer the term "context engineering" because it more comprehensively covers the core work and related tasks of prompt engineering.
People often question the professionalism of prompt engineering because many people confuse it with "blind prompting" (such as entering a brief task description in a large model like ChatGPT).
Blind prompting is just asking a question to the system, while prompt engineering requires careful design of the context and structure of the prompt - perhaps it should have been called context engineering all along.
Context engineering is a more advanced stage: you need to design a complete context architecture, which often requires obtaining, enhancing, and optimizing system knowledge through rigorous methods, not just simple prompting.
From a developer's perspective, context engineering is the process of optimizing instructions and context through an iterative process to enable the LLM to achieve the expected goals.
This includes establishing an evaluation process to measure the effectiveness of the strategy.
Given the rapid development of the AI field, I suggest expanding the definition of context engineering: the process of designing and optimizing instructions and relevant context for LLMs and advanced AI models to enable them to perform tasks efficiently.
This definition not only covers text-based LLMs but also includes the context optimization of increasingly popular multimodal models.
Context engineering involves prompt engineering and the following related processes:
Design and manage prompt chains (if applicable)
Debug instructions/system prompts
Manage dynamic elements of prompts (such as user input, date and time, etc.)
Search and prepare relevant knowledge (i.e., RAG)
Query enhancement
Tool definition and description (in the context of an agent system)
Prepare and optimize few-shot demonstrations
Structured input and output (such as separators/JSON format)
Short-term memory (state/historical context management) and long-term memory (retrieving relevant knowledge from a vector database)
And other techniques for optimizing LLM system prompts
In short, context engineering aims to optimize the information provided to the LLM's context window.
This also means filtering out noisy information - which is an art in itself and requires systematic measurement of the LLM's performance.
Although everyone is talking about context engineering, the following will take you through a practical example of building an AI agent.
Practical application of context engineering
Let's take a look at a specific example of implementing context engineering for my self-built multi-agent in - depth research application recently.
This agent workflow was developed using n8n (but the choice of tool is not important). The complete architecture is as follows:
The "search planning agent" in the workflow is responsible for generating a search plan based on the user's query.
Here is the system prompt written for this sub - agent:
You are a professional research planner. Your task is to break down complex research queries (delimited by <user_query></user_query>) into specific search subtasks, with each subtask focusing on different aspects or source types.
Current date and time: {{ $now.toISO() }}
Each subtask should include:
1. A unique string ID for the subtask (e.g.,'subtask_1', 'news_update')
2. A specific search query focusing on one aspect of the main query
3. The type of search source (web/news/academic/specialized library)
4. The time range (today/last week/recent/past year/no time limit)
5. The applicable domain (technology/science/health, etc.)
6. Priority (1 highest - 5 lowest)
All fields (id/query/source_type/time_period/domain_focus/priority) are required for each subtask, except time_period and domain_focus, which can be left blank if not applicable.
Create 2 subtasks that comprehensively cover the topic, focusing on different information dimensions or sources.
After obtaining the subtask information, two fields, start_date and end_date, need to be added. Please derive the date range based on the current date and the selected time_period, with the format example as follows:
"start_date": "2024 - 06 - 03T06:00:00.000Z",
"end_date": "2024 - 06 - 11T05:59:59.999Z",
Many aspects of this prompt need careful consideration: what specific context should we provide to enable the planning agent to perform tasks efficiently? As you can see, this not only requires designing simple prompts or instructions but also an experimental spirit and providing key context to the model to optimize task execution.
Now let's break down the core elements of effective context engineering.
Instructions
Instructions are high - level operating guidelines provided to the system.
"You are a professional research planner. Your task is to break down complex research queries (delimited by <user_query></user_query>) into specific search subtasks, with each subtask focusing on different aspects or source types."
Many beginners and even senior AI developers may stop here. After reading the complete prompt, you'll understand how much additional context we need to provide for the system to work as expected - this is the core of context engineering: making the system clearly understand the problem scope and specific requirements.
User input
The user input is not shown in the system prompt, but here is an example:
<user_query>What are the latest development trends of OpenAI?</user_query>
Note that the use of delimiters makes the prompt structure clearer. This is very important: it can avoid confusion and clearly separate user input from system - generated content. Sometimes the type of input information is directly related to what we expect the model to output (for example, the query is the input, and the sub - queries are the output).
Structured input and output
In addition to high - level instructions and user input, you may notice that I spent a lot of effort defining the detailed requirements for the planning agent to generate subtasks:
For each subtask, provide:
1. A unique string ID for the subtask (e.g.,'subtask_1', 'news_update')
2. A specific search query that focuses on one aspect of the main query
3. The source type to search (web, news, academic, specialized)
4. Time period relevance (today, last week, recent, past_year, all_time)
5. Domain focus if applicable (technology, science, health, etc.)
6. Priority level (1 - highest to 5 - lowest)
All fields (id, query, source_type, time_period, domain_focus, priority) are required for each subtask, except time_period and domain_focus which can be null if not applicable.
Create 2 subtasks that together will provide comprehensive coverage of the topic. Focus on different aspects, perspectives, or sources of information.
Take a close look at these instructions, and you'll find that I specifically planned the field structure that the planning agent is required to generate and guided the data generation process through prompt examples. This additional context is crucial for clarifying expectations - if the priority scale of 1 - 5 is not declared, the system may default to a 1 - 10 scale. The details of the context determine success or failure!
Next, let's talk about structured output. To obtain stable output from the planning agent, we also provided context descriptions of the subtask format and field types. Here is an example of additional context passed to the agent to clarify the output expectations:
Each subtask should include the following information:
id: str
query: str
source_type: str # e.g., "web", "news", "academic", "specialized library"
time_period: Optional[str] = None # e.g., "today", "last week", "recent", "past year", "no time limit"
domain_focus: Optional[str] = None # e.g., "technology", "science", "health"
priority: int # 1 (highest) to 5 (lowest)
In addition, in n8n, a tool output parser can be used to structure the final output. The solution I adopted is to provide the following JSON example:
{
"subtasks": [
{
"id": "openai_latest_news",
"query": "latest OpenAI announcements and news",
"source_type": "news",
"time_period": "recent",
"domain_focus": "technology",
"priority": 1,
"start_date": "2025 - 06 - 03T06:00:00.000Z",
"end_date": "2025 - 06 - 11T05:59:59.999Z"
},
{
"id": "openai_official_blog",
"query": "OpenAI official blog recent posts",
"source_type": "web",
"time_period": "recent",
"domain_focus": "technology",
"priority": 2,
"start_date": "2025 - 06 - 03T06:00:00.000Z",
"end_date": "2025 - 06 - 11T05:59:59.999Z"
},
...
}
The tool will automatically generate a data schema, enabling the system to parse and generate standardized structured output, like this:
[
{
"action": "parse",
"response": {
"output": {
"subtasks": [
{
"id": "subtask_1",
"query": "OpenAI recent announcements OR news OR updates",
"source_type": "news",
"time_period": "recent",
"domain_focus": "technology",
"priority": 1,
"start_date": "2025 - 06 - 24T16:35:26.901Z",