Context Engineering: Why Curating Context Beats Crafting the Perfect Prompt
The bottleneck moved from prompt wording to context quality. How I decide what actually goes into the window — retrieval, compaction, and the noise that quietly wrecks agent decisions.
A code snippet from this post was tested
Node.js v22.23.0 · Verified July 1, 2026
A code snippet from this post was tested
Node.js v22.23.0 · Verified July 1, 2026
Logic from this post, adapted into a runnable form and executed by the publishing pipeline.
node verify.mjsSnippet
async function reRankContext(query, retrievedContexts) {
// Placeholder for a function that simulates calculating relevance score.
// In a real scenario, this would involve a model call.
function calculateRelevanceScore(q, context) {
// Simple heuristic: higher score if query words appear in context
let score = 0;
const queryWords = q.toLowerCase().split(/\s+/);
const contextLower = context.toLowerCase();
for (const word of queryWords) {
if (contextLower.includes(word)) {
score += 1;
}
}
// Add a small random component to avoid identical scores for identical contexts
// while maintaining determinism by not using Math.random() directly here.
// For demonstration, we'll just use context length for slight variation if scores are equal.
score += context.length * 0.001;
return score;
}
const scoredContexts = retrievedContexts.map(context => ({
context,
score: calculateRelevanceScore(query, context)
}));
scoredContexts.sort((a, b) => b.score - a.score); // Sort by highest score first
return scoredContexts.slice(0, 3).map(item => item.context); // Take top 3
}
// Test cases
(async () => {
const query1 = "best vegetarian restaurants";
const contexts1 = [
"This restaurant serves amazing Italian food, including several vegetarian options.",
"A guide to the best vegan and vegetarian restaurants in the city.",
"Top 10 pizza places, with a focus on meat lovers.",
"Delicious seafood dishes are the specialty here.",
"Healthy eating tips and recipes for a vegetarian diet."
];
console.log("Query:", query1);
console.log("Original Contexts:", contexts1);
const result1 = await reRankContext(query1, contexts1);
console.log("Re-ranked (top 3):", result1);
console.log("---");
const query2 = "AI advancements";
const contexts2 = [
"Recent breakthroughs in large language models and their applications.",
"The history of computing from mainframes to microprocessors.",
"New developments in artificial intelligence ethics and regulation.",
"Understanding deep learning models and neural networks.",
"A debate on climate change and renewable energy sources."
];
console.log("Query:", query2);
console.log("Original Contexts:", contexts2);
const result2 = await reRankContext(query2, contexts2);
console.log("Re-ranked (top 3):", result2);
console.log("---");
const query3 = "empty query";
const contexts3 = [
"First context.",
"Second context.",
"Third context."
];
console.log("Query:", query3);
console.log("Original Contexts:", contexts3);
const result3 = await reRankContext(query3, contexts3);
console.log("Re-ranked (top 3):", result3);
console.log("---");
const query4 = "specific data";
const contexts4 = [
"This data refers to financial reports for Q1 2023.",
"Detailed analytics of user engagement for mobile app version 2.0.",
"Customer feedback regarding product feature X.",
"General company news updates.",
"Market trends for the upcoming year."
];
console.log("Query:", query4);
console.log("Original Contexts:", contexts4);
const result4 = await reRankContext(query4, contexts4);
console.log("Re-ranked (top 3):", result4);
})();Captured output
Query: best vegetarian restaurants
Original Contexts: [
'This restaurant serves amazing Italian food, including several vegetarian options.',
'A guide to the best vegan and vegetarian restaurants in the city.',
'Top 10 pizza places, with a focus on meat lovers.',
'Delicious seafood dishes are the specialty here.',
'Healthy eating tips and recipes for a vegetarian diet.'
]
Re-ranked (top 3): [
'A guide to the best vegan and vegetarian restaurants in the city.',
'This restaurant serves amazing Italian food, including several vegetarian options.',
'Healthy eating tips and recipes for a vegetarian diet.'
]
---
Query: AI advancements
Original Contexts: [
'Recent breakthroughs in large language models and their applications.',
'The history of computing from mainframes to microprocessors.',
'New developments in artificial intelligence ethics and regulation.',
'Understanding deep learning models and neural networks.',
'A debate on climate change and renewable energy sources.'
]
Re-ranked (top 3): [
'The history of computing from mainframes to microprocessors.',
'Recent breakthroughs in large language models and their applications.',
'New developments in artificial intelligence ethics and regulation.'
]
---
Query: empty query
Original Contexts: [ 'First context.', 'Second context.', 'Third context.' ]
Re-ranked (top 3): [ 'Second context.', 'First context.', 'Third context.' ]
---
Query: specific data
Original Contexts: [
'This data refers to financial reports for Q1 2023.',
'Detailed analytics of user engagement for mobile app version 2.0.',
'Customer feedback regarding product feature X.',
'General company news updates.',
'Market trends for the upcoming year.'
]
Re-ranked (top 3): [
'This data refers to financial reports for Q1 2023.',
'Detailed analytics of user engagement for mobile app version 2.0.',
'Customer feedback regarding product feature X.'
]
The Great Prompt Shift: Why Context Engineering Now Dominates the AI Conversation
For years, the talk was all about the perfect prompt. We meticulously crafted incantations, tweaking every word, every punctuation mark, to coax just the right response from our burgeoning AI models. And for a time, it worked. The bottleneck was indeed our ability to communicate our intent clearly to a somewhat alien intelligence.
But something has shifted. As Large Language Models (LLMs) have grown more sophisticated, their ability to understand nuance, follow complex instructions, and generate creative text has skyrocketed. Now, the single biggest factor dictating the quality of an AI's output, especially in the realm of agents and complex reasoning tasks, isn't the prompt itself. It's the context you provide.
I've spent over a decade in software engineering, dabbling in everything from frontend experiences to Web3, and now deeply immersed in AI. What I'm seeing repeatedly is that the bottleneck has moved from prompt wording to context quality. This isn't just theory; this is born from countless hours of debugging agent behavior, observing baffling failures, and ultimately, realizing that the AI wasn't "dumb" – it was just operating on incomplete, irrelevant, or worse, contradictory information.
What is Context Engineering?
Context engineering, to me, is the deliberate and strategic process of acquiring, filtering, structuring, and presenting relevant information to an LLM to guide its reasoning and generation. It's about deciding what actually goes into that precious, limited token window an LLM has access to. Think of it as preparing a meticulously curated research brief for a highly intelligent but utterly uninitiated assistant.
It encompasses several key stages, each fraught with its own challenges and opportunities for optimization.
The Problem with Naive Context: More Isn't Always Better
Our first instinct, often, is to just dump all potentially relevant information into the context window. After all, LLMs are smart, right? They can figure it out.
Wrong. So very wrong.
While modern LLMs have incredible capabilities, they are still susceptible to "context noise." This noise manifests in several ways:
- Lost in the Middle: Research has shown that LLMs tend to pay less attention to information in the middle of a long context window. Crucial details can get buried and overlooked.
- Conflicting Information: If your retrieved context contains contradictory statements, the LLM might struggle to reconcile them, leading to hesitant, inaccurate, or even nonsensical outputs.
- Irrelevant Information (Distractions): Every extra token that doesn't directly contribute to the task at hand is a distraction. It dilutes the signal, consumes valuable token real estate, and can lead the LLM down irrelevant tangents.
- Token Limits: The most obvious constraint. Even with ever-increasing context windows, there's always a limit. We can't just throw entire databases at the LLM.
This is why "curating context beats crafting the perfect prompt" has become my mantra. You can have the most beautifully worded instruction, but if the foundation of knowledge it's built upon is shaky, the output will be too.
My Toolkit for Effective Context Engineering
So, how do I go about actually deciding what makes it into the window? It boils down to a few core techniques:
1. Retrieval Augmented Generation (RAG)
This is the bedrock for most of my context engineering efforts. Instead of relying solely on the LLM's pre-trained knowledge, RAG dynamically retrieves relevant external information and injects it into the prompt.
How I use it:
- Vector Databases: I store domain-specific knowledge, documentation, user manuals, and even past conversations in vector databases (like Pinecone, Weaviate, or even local FAISS indexes for smaller projects).
- Semantic Search: When a query or agent action is initiated, I perform a semantic search against this database to find the most relevant chunks of information.
- Chunking Strategy: This is crucial. Splitting documents into appropriate-sized chunks (e.g., paragraphs, sections, or even custom logical units) impacts retrieval quality. Too small and you lose context, too large and you introduce noise. My current preference leans towards chunks that are ~250-500 tokens, with some overlap for continuity.
// Simplified RAG conceptual code
async function getRelevantContext(query: string, knowledgeBase: VectorStore): Promise<string[]> {
const relevantChunks = await knowledgeBase.query(query, { topK: 5 }); // Get top 5 most relevant chunks
return relevantChunks.map(chunk => chunk.text);
}
async function generateResponseWithRAG(userQuery: string, llm: LLM, knowledgeBase: VectorStore): Promise<string> {
const context = await getRelevantContext(userQuery, knowledgeBase);
const fullPrompt = `You are a helpful assistant. Use the following context to answer the user's question:\n\nContext:\n${context.join('\n\n')}\n\nUser Question: ${userQuery}\n\nAnswer:`;
return llm.generate(fullPrompt);
}2. Context Compaction and Filtering
Retrieval isn't perfect. Even semantic search can bring back mildly relevant but ultimately unhelpful chunks. This is where compaction comes in.
- Re-ranking: After initial retrieval, I often use a smaller, faster model or a cross-encoder to re-rank the retrieved documents based on their actual relevance to the specific query in the context of other retrieved documents.
- Summarization: If a retrieved document is too long but contains critical information, I'll use a local LLM or a specialized summarization model to condense it down. This is particularly useful for meeting minutes, lengthy bug reports, or historical data.
- Question Answering (QA) Refinement: Sometimes, I'll use a smaller LLM to extract just the answer to a specific sub-question from a chunk, rather than passing the whole chunk. This is a form of proactive filtering.
// Conceptual context compaction using a re-ranker
async function reRankContext(query: string, retrievedContexts: string[]): Promise<string[]> {
// Imagine a function that scores each context based on relevance to query
const scoredContexts = retrievedContexts.map(context => ({
context,
score: calculateRelevanceScore(query, context) // Placeholder for actual re-ranking model call
}));
scoredContexts.sort((a, b) => b.score - a.score); // Sort by highest score first
return scoredContexts.slice(0, 3).map(item => item.context); // Take top 3
}3. Proactive Noise Reduction and Schema Enforcement
This is where the agentic approach truly shines. Instead of just reacting to the LLM's needs, I actively design the information flow to minimize noise from the start.
- Schema-driven Retrieval: For structured data, I often describe the schema of the data I expect to retrieve rather than just keywords. For instance, "retrieve user preferences for 'email_notifications' and 'theme_settings'." This helps the model understand precisely what data points it needs.
- Function Calling for Data Access: Instead of putting raw database dumps in the context, I teach the LLM to call functions that retrieve specific, filtered data. This keeps the prompt clean and the data fresh.
graph TD
A[User Query] --> B{Retrieve Initial Context};
B --> C[Vector Store Lookup];
C --> D[Raw Context Chunks];
D --> E{Re-rank & Filter};
E --> F[Summarize if needed];
F --> G[Curated Context];
G --> H[LLM Invocation];
H --> I[Response];Mermaid Diagram: A simplified flowchart illustrating the context engineering process.
The Bottom Line: Minimize Cognitive Load
My guiding principle for context engineering is simple: Minimize the cognitive load on the LLM. Every piece of information in the context window should be there for a reason, directly contributing to the task at hand. If it doesn't, it's noise.
This paradigm shift means we're spending less time perfecting the wording of "Act as a helpful assistant..." and more time perfecting the information environment within which that helpful assistant operates. It means investing in robust retrieval pipelines, intelligent compaction strategies, and a deep understanding of what information an agent actually needs versus what it might find interesting.
The performance differences I've observed are not subtle. Agents that struggle with basic reasoning suddenly become insightful. Models that hallucinate factual details suddenly become reliable knowledge repositories. It's not magic; it's just better data engineering.
If you're building with AI, especially complex agentic systems, take a hard look at your context. Is it clean? Is it relevant? Is it concise? Because therein lies the true path to unlocking the full potential of these incredible models.
I'm always keen to discuss these evolving challenges and solutions in the AI space. Feel free to connect with me on LinkedIn or X to share your own experiences and insights!