The 5W1H of Context Engineering: A Method for Agentic AI

This article defines Context Engineering (CE), contrasts it with traditional Prompt Engineering, explains its components and benefits for Agentic AI, outlines practical steps and best‑practice techniques—including KV‑cache design, masking, file‑system context, and attention manipulation—while also discussing evaluation challenges and future outlook.

Hailey Says
Hailey Says
Hailey Says
The 5W1H of Context Engineering: A Method for Agentic AI

Background

On June 25, Andrej Karpathy retweeted a Shopify CEO post emphasizing the importance of "Context Engineering"; on July 19, Manus Peak published an experience report on building Agent Context Engineering, marking CE as a replacement for Prompt Engineering (PE) and a prerequisite for agents that handle complex tasks.

What is Context Engineering (CE)?

CE refers to the full set of information presented to a language model before it generates output. The context includes:

Instructions / System Prompt – initial directives, examples, and rules.

User Prompt – the immediate task or question.

State / History (short‑term memory) – prior user and model responses.

Long‑term Memory – accumulated knowledge such as user preferences or project summaries.

Retrieved Information – external knowledge from documents, databases, or APIs.

Available Tools – definitions of callable functions or built‑in tools.

Structured Output – format specifications like JSON objects.

CE vs. PE

CE represents a paradigm shift from the static prompt templates of PE to a systematic, pre‑execution output that supplies comprehensive background.

Prompt Engineering (PE)

Focuses on clever, task‑specific phrasing.

Limited to expressing the task request.

Analogous to a sticky note for someone else.

Context Engineering (CE)

Provides a complete system with background information.

Includes documents, examples, rules, patterns, and validation.

Like writing a full script with all details.

Why CE Matters

Reduces AI errors that stem from missing or incorrect context rather than model flaws.

Ensures consistency by allowing the AI to follow project patterns and conventions.

Enables complex, multi‑step functionality.

Supports self‑correction through verification loops.

How to Use CE

Building a complete CE involves checking the following aspects:

Design and manage prompt chains.

Adjust instructions or system prompts.

Handle dynamic elements such as user input, dates, or times.

Search and prepare relevant knowledge (e.g., Retrieval‑Augmented Generation).

Query augmentation.

Define and describe tools when constructing an agent system.

Prepare and optimize few‑shot demonstrations.

Construct input and output formats (e.g., separators, JSON schemas).

Manage short‑term and long‑term memory, including vector‑store retrieval.

Apply other techniques that help optimize LLM system prompts for the desired task.

An example built with n8n demonstrates CE components. The system prompt must explicitly list the required context and break the problem into core components, turning the prompt generation into an experimental process.

-- Instructions用于准确提示System要做什么
-- 有助于模型理解用户输入是什么以及希望system输出什么内容
-- <user_query> What's the latest dev news from OpenAI? </user_query>
You are an expert research planner. Your task is to break down a complex research query (delimited by <user_query>...</user_query>) into specific search subtasks, each focusing on a different aspect or source type.
--调用工具
The current date and time is: {{ $now.toISO() }}
--结构化输入
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 (e.g., "web", "news", "academic", "specialized")
4. Time period relevance (today, last week, recent, past_year, all_time)
5. Domain focus if applicable (technology, science, health)
6. Priority level (1‑5)

After obtaining the above subtasks information, you will add two extra fields. Those correspond to start_date and end_date. Infer this information given the current date and the time_period selected. start_date and end_date should use the format as in the example below:

"start_date": "2024-06-03T06:00:00.000Z",
"end_date": "2024-06-11T05:59:59.999Z",

Best Practices

Drawing from existing engineering practices and academic research, the article outlines major challenges and key techniques.

Challenges

Slow Feedback Loop : Model fine‑tuning takes weeks; finding optimal context strategies remains a stochastic trial‑and‑error process.

Context Length Limits & Cost : Large unstructured inputs (web pages, PDFs) can exceed model windows, degrade performance, and incur high token costs.

KV‑Cache Invalidation : Minor token changes invalidate cache, increasing latency and cost.

Action Space Management : Expanding tool sets cause cache invalidation and model confusion when undefined tools are referenced.

Model Attention Drift : In long, multi‑step tasks agents may lose focus on earlier goals.

Error Handling : Hiding failures removes evidence needed for the model to learn.

Few‑Shot Trap : Overly similar past action‑observation pairs cause the model to over‑generalize or hallucinate.

Key Techniques

KV‑Cache‑Aware Design : Keep prompt prefixes stable (avoid timestamps), append context only, ensure deterministic serialization, and use prefix/prompt caching with session IDs for self‑hosted models.

Masking, Not Removing : Use context‑aware state machines to mask logits of unavailable tools instead of deleting definitions; employ response prefilling to constrain the action space.

File System as Context : Treat the file system as an unlimited, persistent external memory; use reversible compression (e.g., keep URLs, drop full content) to reduce length without permanent loss.

Manipulating Attention via Rephrasing : Continuously rewrite a “todo.md” file with completed items to push the global plan into the model’s recent attention window.

Retaining Error Content : Preserve failed attempts and their observations in the context so the model can implicitly update its beliefs and avoid repeating mistakes.

Avoiding Few‑Shot Stagnation : Introduce controlled variability in serialization templates, phrasing, order, or format to break repetitive patterns.

Evaluation

To assess a CE‑built AI system, test information retention, positional bias, and reasoning coherence over long sequences using existing benchmarks or module‑level input‑output checks. One can also employ an LLM evaluator to score another LLM’s output, providing a complementary metric beyond numeric scores.

Final Thoughts

Context Engineering assumes broad acceptance of Agentic AI’s long‑term value and serves as a practical engineering patch for current multi‑agent system limitations. As models become more capable, the relative importance of CE may shift from a competitive moat to a safety and personalization layer.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LLMPrompt EngineeringRAGMulti‑agent SystemsAgentic AIKV cacheContext Engineering
Hailey Says
Written by

Hailey Says

🎨 Sharing AI experiences, insights, and taste 🗺️ Code & Design & KOL

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.