From Prompt Engineering to Context Engineering
A few years ago, many predicted that prompt engineering would quickly become obsolete. Instead, it evolved and grew more essential, giving rise to context engineeringâa broader, richer discipline dedicated to optimizing not just prompts, but the full context provided to Large Language Models (LLMs) and multimodal AI systems.
Prompt engineering isnât merely asking questionsâitâs carefully designing, structuring, and refining instructions to shape how AI responds. Context engineering expands on this by systematically architecting all relevant context, including instructions, inputs, tools, memory, and historical data, to ensure the model achieves optimal results.
What Exactly Is Context Engineering?
Context engineering involves the iterative process of crafting, optimizing, and testing the inputs provided to AI systems, ensuring they have precisely the information they need, structured in the right way, to perform their tasks effectively. This includes:
-
Structuring inputs and outputs (e.g., JSON schemas)
-
Managing dynamic elements (user inputs, dates, etc.)
-
Utilizing Retrieval-Augmented Generation (RAG) and memory stores
-
Tool integration and structured output parsing
-
Handling historical context and application state
Essentially, youâre not just writing prompts; youâre designing a thoughtful and comprehensive context environment for your AI models.
Context Engineering in Action
Letâs see what this looks like through a practical example of an AI-powered research workflow I developed recently.
A Concrete Example: Building a Research Planner Agent
I built an AI agent that takes complex user queries and breaks them down into targeted subtasks to optimize web searches. The workflow requires precise context to guide the agent in generating structured subtasks effectively.
Example of a System Prompt:
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 (e.g., 'subtask_1')
2. A specific search query
3. Source type (web, news, academic, specialized)
4. Time period relevance (today, recent, past_year, etc.)
5. Domain focus (technology, science, health, etc.)
6. Priority level (1-highest to 5-lowest)
Create 2 subtasks covering different aspects of the topic.
After creating subtasks, infer and include `start_date` and `end_date` based on the current date and time_period provided.
Structured Inputs and Outputs
To ensure the model returns consistent and usable outputs, I explicitly defined the expected JSON structure:
{
"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"
},
...
]
}
Providing examples like this guides the AI clearly and reduces the risk of unpredictable or inconsistent outputs. But this is not the ultimate solution to extracting structured outputs from LLMs. A better Alternative is discussed in the next section.
Structured Inputs and Outputs (feat. Instructor)
One of the biggest pain points in working with LLMs is getting consistent, structured outputsâespecially when youâre chaining tools or feeding data downstream. This is where I found the Instructor
library to be a game-changer.
Instead of writing verbose prompts to âguideâ the model into outputting a specific schema, Instructor
lets you define a Pydantic or dataclass model, and it takes care of enforcing structure from almost any generative model. Itâs clean, type-safe, and surprisingly reliable.
Hereâs an example of how I used it to define the subtask schema for my planning agent:
from pydantic import BaseModel
from typing import Optional
class Subtask(BaseModel):
id: str
query: str
source_type: str # "news", "web", etc.
time_period: Optional[str] = None # "today", "recent", etc.
domain_focus: Optional[str] = None
priority: int
start_date: str
end_date: str
Then, you can call the model like this:
from instructor import Instructor
from openai import OpenAI
client = Instructor(OpenAI())
subtasks = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Break down the following query into two subtasks: What's the latest dev news from OpenAI?"}],
response_model=List[Subtask]
)
The result? Clean, valid JSON outputsâtyped, validated, and ready to plug into your workflows.
Iâve used Instructor
where I needed predictability across components. It really shines when you need:
- Reliable field formatting
- Fail-fast error handling
- Seamless integration with Python-based workflows
- It retries request to the LLM automatically in case of failed validation (in rare cases)
Even if youâre not deep into Python, understanding this approach helps clarify the goal of context engineering: giving the model not only guidance but also constraints.
Tool Integration: Date and Time Context
Including dynamic elements, like date and time, dramatically improves AI responses. Rather than letting the model guess, providing accurate date/time context ensures the subtasks generated are relevant and timely.
Example integration in n8n:
The current date and time is: {{ $now.toISO() }}
This small piece of context significantly improves the quality and accuracy of results, especially for time-sensitive tasks.
Memory and Retrieval-Augmented Generation (RAG)
Context engineering extends to intelligently managing memory. For example, caching previously generated subtasks using a vector store helps quickly retrieve past results, making your AI workflow faster and less costly.
Why Context Engineering Matters
Good context engineering provides clear, structured, and optimized inputs that enable LLMs to generate consistently better outputs. It reduces the ambiguity of prompts, significantly improves accuracy, optimizes resource use, and facilitates more reliable workflows.
Itâs not about writing longer promptsâitâs about providing smarter context.
Advanced Topics and Future Directions
Context engineering continues to evolve rapidly, encompassing topics like context compression, context safety, and automated context optimization techniques. Expect it to become an increasingly critical skill set for AI developers as AI applications grow more complex and nuanced.
Further Reading and Resources:
- Prompting Guide
- Context Engineering by R. Lance Martin
- Andrej Karpathy on Context Engineering
- Context Engineering by Phil Schmid
- The Skill Thatâs Replacing Prompt Engineering (Simple.ai)
- 12-Factor Agents (GitHub)
- The Rise of Context Engineering (LangChain Blog)
Context engineering is not a trendâitâs an evolution, shaping how developers interact with AI. Start exploring and integrating context engineering into your workflows today!