Skip to main content
Subscribe
AI & Agentic

Context Engineering Architecture: 8 Stages From Demo to Production

The 8-Stage Pipeline That Replaced Naive Prompting

Most “agentic AI” demos fall apart the moment you put real data behind them. The agent works in a notebook with three documents and crumbles in production with three thousand. The model didn’t get worse. The context did.

A 2025 MIT NANDA study found that 95% of generative AI pilots fail to deliver measurable ROI (MIT NANDA, 2025). The reflexive answer is to blame the model: too small, too cheap, too dumb. But teams that ship working agents will tell you the same thing. The bottleneck isn’t the model. It’s the architecture that decides what the model gets to see.

That architecture has a name now. Context engineering: the discipline of dynamically assembling the right tokens, in the right order, within the right budget, every single turn. Anthropic calls it “the natural progression of prompt engineering” (Anthropic Engineering, 2025). 82% of IT leaders agree prompt engineering alone is no longer enough to power AI at scale (DataHub, 2026).

I’ve spent the last year building agents at Growth Engine, and the same eight stages show up every time the system survives contact with users. This guide is the reference I wish I’d had when I started.

foundational guide to agentic AI

Key Takeaways

  • 95% of GenAI pilots fail in production. The root cause is context architecture, not model selection (MIT NANDA, 2025).
  • Production agents need 8 discrete stages: intent classification → parallel retrieval → reranking → compression → context assembly → token-budget audit → guardrails → observability.
  • 95% of data teams plan to invest in context engineering training in 2026 (DataHub, 2026).
  • Token budgets aren’t free, and the curve differs by vendor: Gemini 3.1 Pro doubles its rate above 200k tokens and GPT-5.5 charges 2× input above 272k, while current Claude models price the full 1M window flat. Architecture decides cost.

What Is Context Engineering Architecture?

Context engineering architecture is the system-level design pattern for assembling the exact tokens an LLM needs to complete a task: dynamically, every turn, within a measured token budget. It treats context as a finite resource with diminishing marginal returns, not as a magic-prompt input (Anthropic Engineering, 2025).

Prompt engineering optimizes a static template. Context engineering optimizes a runtime pipeline. The difference shows up the moment your agent has to deal with conversation history, retrieved documents, tool outputs, and user-specific memory all at once.

Here’s the practical distinction. A prompt engineer asks: “What’s the best instruction to give the model?” A context engineer asks: “Of the 1.4M tokens we could give the model, which 24,000 actually matter for this turn, and how do we pick them in 80 milliseconds?”

Both questions have answers. Only the second one survives production load.

why AI-native apps need new architectural patterns

The frame shift: prompt engineering treats the LLM as a black box you negotiate with. Context engineering treats the LLM as a deterministic function whose output is a function of its input. The input is a system you design, not a string you write.


Why Do Production Agents Fail Without Context Engineering?

Production agents fail because LLMs degrade non-linearly with context size. Stanford research shows retrieval accuracy drops more than 30% when relevant information sits in the middle of a long context window rather than at the start or end (Liu et al., 2023). This is the now-famous “lost in the middle” problem, and it gets worse as context grows.

The Datadog State of AI Engineering report found something even more revealing. In production traces, 69% of all input tokens were system-prompt tokens: instructions, policies, and tool definitions that the same agent re-sends on every single call (Datadog, 2026). That’s not context. That’s expensive boilerplate, repeated forever, drowning the actual question.

Retrieval accuracy by document position: 72% at position 1, dropping to 40% in the middle of the context window, recovering to 70% at the last position

According to a 2026 Anthropic engineering analysis, “context must be treated as a finite resource with diminishing marginal returns” (Anthropic Engineering, 2025). Stuff the window indiscriminately and your accuracy goes down even though your token bill goes up. That’s the economics of bad context architecture in one sentence.

semantic caching to reduce repeated context costs


What Are the 8 Stages of a Production Context Pipeline?

A production context pipeline runs eight discrete stages on every agent turn: intent classification, parallel retrieval, reranking, compression, context assembly, token-budget audit, guardrails, and observability. Each stage has a specific failure mode, a measurable output, and a place where you can swap implementations without rewriting the rest. This is the architectural difference between a demo and a system.

Most articles on context engineering stop at “use RAG and summarize older messages.” That’s not architecture. That’s two patterns. The pipeline below is what you actually run when an agent has to handle a thousand concurrent users with auditable behavior.

Long glass corridor inside a data center, suggesting linear stage-by-stage pipeline flow

Let’s walk through each stage. For each one I’ll give you the job, the failure mode, the metric, and a code sketch.


Stage 1: How Does Intent Classification Route the Request?

Intent classification is the router that decides what kind of context the request needs before any retrieval happens. A small, fast classifier (often a 7B model or even a logistic regression on embeddings) tags the user’s input with an intent (lookup, multi_hop_reasoning, code_generation, tool_call, chitchat) and routes the turn to a different downstream pipeline for each.

Why does this matter? Because you don’t need vector retrieval for “thanks.” You don’t need conversation history for a fresh code-review request. And you absolutely don’t need a 24-document RAG dump for a math question. Skipping the router is how teams end up paying for retrieval on every turn whether it’s needed or not.

# Stage 1: Intent classification, fast, cheap, deterministic
INTENTS = ["lookup", "reasoning", "tool_call", "code_gen", "chitchat"]

def classify_intent(query: str) -> str:
    embedding = embed_fast(query)  # ~5ms with bge-small
    return classifier.predict(embedding)  # logreg, ~1ms

intent = classify_intent(user_input)
pipeline = PIPELINES[intent]  # different stages run for each intent

The failure mode here is simple. Classify wrong, and the rest of the pipeline does work that doesn’t help. The metric is intent-accuracy on a held-out eval set, plus the percentage of turns that bypass retrieval entirely (target: 20-40% of agent turns don’t need RAG at all).


Stage 2: Why Run Retrieval in Parallel Instead of Sequentially?

Parallel retrieval fans the query out across multiple data sources at once (vector store, full-text search, knowledge graph, recent conversation memory, user profile) and waits for all of them to respond. The vector database market is projected to grow from USD $2.65B in 2025 to $8.95B by 2030, a 27.5% CAGR (MarketsandMarkets, 2025), but a vector store alone is rarely enough.

Real production retrieval is hybrid. You hit BM25 for exact-match terms, dense embeddings for semantic similarity, a graph for entity relationships, and your CRM or feature store for user-specific facts, all at the same time. Then you union the results.

Engineer's monitor displaying terminal code, used for the parallel retrieval and reranking stage

# Stage 2: Parallel retrieval, fan out, wait for all
import asyncio

async def parallel_retrieve(query: str, intent: str) -> list[Doc]:
    tasks = [
        vector_search(query, k=20),       # semantic
        bm25_search(query, k=20),         # lexical
        graph_lookup(extract_entities(query)),  # entities
        memory_recall(query, user_id),    # user memory
    ]
    results = await asyncio.gather(*tasks)
    return dedupe_by_id(flatten(results))

The failure mode is retrieval starvation: a slow source becomes the bottleneck. Set per-source timeouts (200-300ms), drop late responses, and proceed with what you have. The metric is recall@50 across the union, not any single source.

What I learned shipping this: every team I’ve worked with under-sizes their k. They ask for top-5 and wonder why the answer’s missing. Retrieve 20-50 candidates from each source. The reranker in stage 3 is what gets you down to 5, and it’s better at picking than the embedding similarity score.

agent infrastructure patterns for small teams


Stage 3: How Much Does Reranking Actually Improve Accuracy?

Reranking takes the 50-200 candidate documents from parallel retrieval and re-scores them with a more expensive cross-encoder, returning the top 3-10 that actually answer the question. The gains are well documented. Databricks testing found reranking improves retrieval quality by up to 48% over baseline retrieval and cuts hallucinations by 35% compared to raw embedding similarity, while ZeroEntropy’s own zerank-1 reports a 28% NDCG@10 lift over baseline retrievers (ZeroEntropy, 2025). The latency cost depends entirely on which model you pick. Dedicated rerankers land around 100ms for Cohere’s rerank-3.5 and roughly 60ms for zerank-1, while general-purpose cross-encoders run anywhere from 200ms to 2 seconds per query. At the top of the current leaderboard, Cohere’s Rerank 4 Pro scores 1627 ELO, about 170 points above its v3.5 predecessor and more than 400 points ahead on business and finance tasks (Cohere, 2026).

That accuracy delta is what kills the demo-to-production gap for most RAG pipelines.

Measured gains from a reranking stage: up to 48% retrieval quality uplift and 35% fewer hallucinations (Databricks), and a 28% NDCG@10 lift for zerank-1 (ZeroEntropy)

# Stage 3: Reranking - bring the top-50 down to top-5
def rerank(query: str, candidates: list[Doc], k: int = 5) -> list[Doc]:
    pairs = [(query, doc.text) for doc in candidates]
    scores = cross_encoder.predict(pairs)  # budget 100-200ms for 50 pairs
    ranked = sorted(zip(candidates, scores), key=lambda x: -x[1])
    return [doc for doc, _ in ranked[:k]]

A cross-encoder reranker is one of the highest-leverage additions you can make to an existing RAG pipeline. The failure mode is mostly cost (each rerank pass costs roughly 10x the embedding pass), and the metric is precision@5 on labeled query-document pairs.


Stage 4: What Does Context Compression Look Like in Production?

Context compression turns the top reranked documents into the smallest faithful representation that still answers the question. This isn’t summarization in the GPT-3 sense. It’s structured extraction, span selection, and key-value distillation aimed at removing tokens that don’t pull weight.

Datadog’s 2026 traces make the real target clear: system-prompt tokens, not retrieved documents, dominated the window at 69% of all input tokens (Datadog, 2026). That means compression has to happen on more than just RAG output. Tool definitions get pruned to the ones the intent classifier thinks are relevant. Conversation history gets summarized once it exceeds a length threshold. Older tool outputs get written to disk and replaced with a pointer.

Donut chart showing 69% of production input tokens are system-prompt tokens and 31% is everything else, per Datadog 2026 traces

A few patterns that work in practice:

The metric is the compression ratio (output tokens / input tokens) at preserved task accuracy. A good rule of thumb: you should be able to compress 4-6x without measurable accuracy loss on your eval set.

multi-agent orchestration patterns


Stage 5: How Does Context Assembly Order Tokens for the Model?

Context assembly is the deterministic step that takes everything from the previous stages (system prompt, compressed retrieval, tool definitions, conversation summary, current message) and lays them out in a specific order designed to fight context rot. Anthropic’s research is explicit that ordering matters as much as content: the start and end of the window are where attention is strongest (Anthropic Engineering, 2025).

A reasonable default ordering, top to bottom:

  1. System prompt: short, declarative, the policy and persona
  2. Tool definitions: pruned to the relevant subset
  3. Long-term memory: user-specific facts, structured
  4. Retrieved context: the reranked, compressed top-K
  5. Conversation summary: older history, compressed
  6. Recent turns: last 4-8 messages verbatim
  7. Current user message: the actual question, last

The current user message goes last because that’s where the model’s attention is strongest. The system prompt goes first because that’s the second-strongest position, and you want the policy bracketing the request.

# Stage 5: Context assembly - deterministic order
def assemble_context(parts: ContextParts) -> list[Message]:
    messages = []
    messages.append({"role": "system", "content": parts.system_prompt})
    if parts.tools:
        messages.append({"role": "system", "content": render_tools(parts.tools)})
    if parts.long_term_memory:
        messages.append({"role": "system", "content": render_memory(parts.long_term_memory)})
    if parts.retrieved_docs:
        messages.append({"role": "user", "content": render_docs(parts.retrieved_docs)})
    if parts.summary:
        messages.append({"role": "user", "content": parts.summary})
    messages.extend(parts.recent_turns)
    messages.append({"role": "user", "content": parts.current_message})
    return messages

Don’t get clever here. The assembler should be a pure function: same inputs, same outputs, no LLM calls. That’s how you replay traces in observability and trust the result.


Stage 6: How Do You Audit Token Budgets Before Sending the Prompt?

Token-budget audit is the mandatory checkpoint between assembly and sending. It counts the assembled tokens, compares them to the model’s effective window, and either fits or rejects the prompt before you spend money on it. This is the stage every shallow tutorial skips.

Token budgets aren’t free, and the pricing structure varies more than most teams expect. Some vendors charge a flat rate across the whole window, others tier it at a threshold:

The lesson isn’t that long context is uniformly expensive. It’s that the cost curve differs by vendor, so a budget audit tuned to one model’s threshold will silently mis-price another.

Slope chart of input price per 1M tokens below vs above each vendor long-context threshold: Claude Opus 5 flat at $5 and Sonnet 5 flat at $3, GPT-5.5 rising $5 to $10, Gemini 3.1 Pro rising $2 to $4

The audit step is short and worth its weight in incidents:

# Stage 6: Token-budget audit
def audit_budget(messages: list[Message], model: ModelConfig) -> AuditResult:
    tokens = count_tokens(messages, model.tokenizer)
    if tokens > model.hard_limit:
        raise BudgetExceeded(f"{tokens} > {model.hard_limit}")
    if tokens > model.soft_limit:
        log.warn(f"In premium tier: {tokens} tokens, +2x pricing applies")
    return AuditResult(tokens=tokens, tier="premium" if tokens > model.soft_limit else "standard")

Why does this matter beyond cost? Because budget overruns aren’t graceful. They truncate, often silently, and your model loses exactly the part of the prompt you needed most. The audit gives you a place to fail loudly and trigger a re-compress if you’ve exceeded your target.

Monitor with dense code on a dark background, representing token-budget audit instrumentation


Stage 7: What Guardrails Belong in the Context Pipeline?

Guardrails are the validation, redaction, and policy-enforcement layer that runs before the prompt leaves your service and after the model’s response comes back. Most teams bolt these on at the application layer; production teams bake them into the context pipeline because that’s where the data lives at the right granularity.

Guardrails that belong in the pipeline:

Cognizant deployed 1,000 context engineers across its ContextFabric platform in late 2025 and reported up to 70% fewer hallucinations versus baseline agents, largely attributed to disciplined guardrails plus retrieval (Cognizant, 2025).

# Stage 7: Guardrails - bidirectional
def pre_send_guardrails(messages: list[Message]) -> list[Message]:
    return [redact_pii(m) for m in messages]

def post_response_guardrails(response: str, retrieved: list[Doc]) -> ValidatedResponse:
    if not validate_schema(response):
        return retry_with_parser_feedback(response)
    if not citations_grounded(response, retrieved):
        return flag_hallucination(response)
    return ValidatedResponse(response)

AI agent guardrails for code review


Stage 8: How Do You Observe a Context Pipeline in Production?

Observability for context engineering means instrumenting every stage with OpenTelemetry GenAI semantic conventions so you can answer questions like “which stage caused this bad answer?” without re-running the whole pipeline. According to LangChain’s State of Agent Engineering 2025, 89% of organizations have implemented some form of agent observability. Among teams running agents in production, that number jumps to 94% (LangChain, 2025).

The OpenTelemetry semantic conventions for GenAI standardize the spans, attributes, and events you should emit (OpenTelemetry, 2025). At minimum, every agent turn should produce a trace with one span per pipeline stage, plus the full prompt and response captured as events.

Developer in a dark workstation typing on multiple keyboards, representing observability and on-call

What to capture per stage:

The Datadog 2026 report found that 5% of all LLM call spans returned an error in February 2026, and 60% of those were caused by exceeded rate limits. By March the error rate had dropped to 2%, with rate limits accounting for almost a third of them, which still worked out to nearly 8.4 million rate-limit errors in a single month across observed production traffic (Datadog, 2026). Without per-stage instrumentation, you can’t tell whether your agent is failing because retrieval was empty, the assembly was malformed, or the model itself rate-limited. With it, you can.

# Stage 8: Observability - per-stage spans
from opentelemetry import trace
tracer = trace.get_tracer("context-pipeline")

def run_pipeline(query: str):
    with tracer.start_as_current_span("agent.turn") as turn:
        with tracer.start_as_current_span("intent") as s:
            intent = classify_intent(query)
            s.set_attribute("gen_ai.intent.predicted", intent)
        with tracer.start_as_current_span("retrieve") as s:
            docs = parallel_retrieve(query, intent)
            s.set_attribute("gen_ai.retrieval.docs_count", len(docs))
        # ... and so on for each stage

What I learned shipping this: the single highest-leverage piece of instrumentation is logging the assembled prompt, every token, every turn, to a queryable store. When a user complains “the agent gave me a wrong answer,” you need to see the exact bytes the model saw. Aggregated metrics won’t tell you that.


How Do Memory and Context Engineering Relate?

Memory is the long-lived state that persists across agent turns: user preferences, prior conversations, distilled facts. It feeds into stages 2 (retrieval) and 5 (assembly). Memory is part of a context pipeline, not a separate system. The 2025 mem0 Series A funding round of $24M, plus its selection as the exclusive memory provider for AWS Strands Agent SDK, signals just how foundational this layer has become (Sacra, 2025).

There are three memory types that show up in every production architecture:

Memory products like mem0, Letta, and LlamaIndex differ mostly on how aggressively they distill and index these, but they all plug into the same stages of your pipeline. Don’t treat memory as a feature you bolt on later. It’s stage 2 input.

how persistent memory changes interface design


How Do You Build a Context Pipeline Step by Step?

Build the pipeline in order of leverage, not in order of stages. Most teams ship the model integration first and discover the architecture problems later. Here’s the order I’d recommend if you’re starting from a working prompt-and-RAG demo and trying to harden it for production.

  1. Add observability first (Stage 8). Before you change anything, instrument what’s already running. You can’t optimize a pipeline you can’t see. OpenTelemetry GenAI conventions are stable enough to adopt in 2026.
  2. Add the token-budget audit (Stage 6). Once you can see token counts per turn, surprising things become visible, like the 69% system-prompt domination Datadog found.
  3. Add reranking (Stage 3). Highest accuracy-per-dollar improvement of any stage. Cohere’s Rerank 4 or ZeroEntropy’s zerank models will get you most of the documented uplift in a single afternoon.
  4. Add intent classification (Stage 1). Once you have telemetry, you’ll see how many turns don’t actually need retrieval. Routing them away cuts cost and latency immediately.
  5. Add compression (Stage 4). Now that you can measure, optimize the largest contributor, usually the system prompt or the retrieved-doc dump.
  6. Add guardrails (Stage 7). With the rest of the pipeline structured, schema-validation, redaction, and grounding checks slot in naturally.
  7. Productionize parallel retrieval and assembly (Stages 2 & 5). These are usually already partially built. You’re just making them deterministic and traced.

The whole sequence is roughly a quarter of focused work for a small team. The result is an agent that survives ten times the load of the demo it started as.


What Are the Context Engineering Best Practices That Actually Matter?

The best practices that survive contact with production are mostly about measurement and ordering, not about clever prompt wording. Seven rules cover almost everything I’ve had to learn the expensive way.

Practice Why it matters
Measure the system prompt before anything else It is usually the largest line item, not the retrieved documents. Datadog’s traces put it at 69% of input tokens.
Set the token ceiling before you build A budget you enforce in code (Stage 6) is an architectural constraint. A budget you hold in your head is a wish.
Put the answer at the edges of the window Attention is strongest at the start and end. Burying key context in the middle costs you more than 30% accuracy.
Rerank before you compress, never after Compression is lossy. Compressing 50 candidates and then ranking them means you rank damaged text.
Version the assembly, not the prompt The thing that changes behavior is the order and composition of the window. Diff that, and your regressions become explainable.
Instrument per stage or you are guessing Without a span per stage you cannot tell an empty retrieval from a malformed assembly from a rate limit.
Treat the pricing curve as an architecture input Thresholds differ by vendor. A budget tuned to one model’s tier silently mis-prices another.

The one I see skipped most often is the second. Teams add retrieval, add memory, add tools, and never write down what a turn is allowed to cost. Six months later the agent works and nobody can explain the bill.

The one that pays back fastest is the fourth. Reordering compression after reranking is usually a few hours of work and it stops you from throwing away the sentence that held the answer.

If you only adopt two of these, take measurement first and ordering second. Everything else in this pipeline is easier to fix once you can see what the model is actually receiving.


Frequently Asked Questions

How does context engineering differ from prompt engineering?

Prompt engineering optimizes a static instruction string to elicit a desired output. Context engineering optimizes a dynamic, runtime pipeline that assembles instructions, retrieved data, memory, and tools per turn within a measured token budget. 82% of IT leaders agree prompt engineering alone is no longer sufficient at scale (DataHub, 2026).

Do I need all 8 stages for a small project?

No. A side-project agent can work with just stages 2 (retrieval), 5 (assembly), and 8 (basic observability). The other stages get added as the agent scales. You’ll feel the pain of missing them when traffic and data grow. Cognizant’s deployment of 1,000 context engineers signals these stages matter at enterprise scale (Cognizant, 2025).

Why does context order matter inside the prompt?

LLMs pay more attention to tokens at the start and end of their context window. Stanford’s “Lost in the Middle” research found a >30% accuracy drop for information placed in the middle of a long context (Liu et al., 2023). Putting the system prompt first and the user’s question last is the strongest default ordering.

How big should my token budget be?

It depends on your model and intent. For most agent turns, target 8k-24k tokens of total context. Budget audits (Stage 6) should reject anything beyond your soft limit and trigger a recompress. Check your specific model before you set the ceiling, because the pricing curve is not consistent across vendors. Gemini 3.1 Pro doubles its rate above 200k and GPT-5.5 charges 2× input above 272k, while the current Claude Opus and Sonnet models price the full 1M window flat (Google, 2026) (OpenAI, 2026).

Which framework should I use to build this?

LangGraph, Vercel AI SDK, AutoGen, and Anthropic’s official agent SDK all ship context-management primitives in 2026. The pipeline above is framework-agnostic. Pick the one that matches your stack. If you’re starting fresh in TypeScript, Vercel AI SDK is the lowest-friction; in Python, LangGraph or the Anthropic SDK directly.


Conclusion: The Architecture Is the Product

The teams shipping working agents in 2026 aren’t winning because they have a better model. They’re winning because they have a better pipeline. The 8 stages above, intent classification, parallel retrieval, reranking, compression, assembly, token-budget audit, guardrails, observability, are the difference between a system that works in a notebook and one that survives a Tuesday afternoon at 3pm with a thousand concurrent users.

Three things to take with you:

If you’re building agents and haven’t formalized these stages yet, start with the observability layer this week. Everything else flows from being able to see what’s actually in your prompts.

foundational guide to agentic AI fundamentals

Written by Nishil Bhave

Builder, maker, and tech writer at MakeToCreate.

Never miss a post

Get the latest tech insights delivered to your inbox. No spam, unsubscribe anytime.

Related Posts