Context Engineering: The Discipline That Quietly Became Load-Bearing
For a long time we treated large language models as if the whole game was writing better prompts – sharper instructions, cleaner examples, more careful formatting. Prompt engineering taught the model what to say; context engineering decides what the model is allowed to know at all. That distinction turned out to be critical once we moved from single question single answer sessions to agents that run for hours, call many tools, and accumulate state across hundreds of steps. At that scale the prompt itself is a rounding error. The real work – and the real failure modes – live in whatever is sitting inside the model’s context window at any given moment, and just as often in what is missing from it.
Every modern LLM application treats the context window as a scarce budget rather than an infinite warehouse. Even at a million tokens you cannot just pour in everything the task might possibly touch and expect the model to reason cleanly over the flood. Attention degrades as volume goes up, relevant details get buried under noise, and your costs scale linearly with every token you did not need to send. The naive “just give it more context” reaction solves the wrong problem because it makes retrieval harder instead of easier. The real problem is precision: getting the right information, in the right representation, at the right moment, and nothing else. That is what context engineering tries to formalize and it now has a recognizable shape made of four stages.
Stage 1 – Ingest
Ingestion is the unglamorous front end of a context pipeline, but it is also where many systems quietly fail. A production agent pulls from Slack threads, PDF reports, SQL rows, API responses, and raw code – each with its own structure, noise patterns, and extraction logic. If you flatten all of that directly into raw text you pay for navigation chrome, headers, footers, page numbers, filler words, and false starts, while simultaneously losing provenance for every fact the model later cites. A robust ingestion layer normalizes this heterogeneity into tagged units that carry at least source type, source id, timestamps, and basic metadata like paragraph index or character counts.
The point is that the output of ingestion is not “some text” but a set of attributable chunks that downstream stages can reason about. With a simple `IngestedChunk` data class, each unit of content carries its origin and a rough token estimate so later stages can decide which sources are worth including. Boilerplate is stripped before ranking ever runs and microscopic fragments too short to carry meaning are discarded early. Once you build this layer the system can answer questions like “where did this claim come from” and “how expensive is it to include this block of content” instead of treating everything as indistinguishable strings.
Stage 2 – Rank
Once material is ingested, most of it is irrelevant to the current task. Ranking is the stage that decides what is actually worth paying tokens for. Pure semantic similarity – embed the query, embed each candidate, sort by cosine distance – works tolerably well, but fails in predictable ways. It surfaces passages that sound related without being useful, ignores recency, cannot distinguish high authority sources from throwaway text, and happily equates a stale changelog with a live production config if the wording overlaps.
Production systems therefore layer multiple signals rather than relying on a single embedding score. A `RankedChunk` can track a semantic score, a time based recency score, and an authority score derived from source type or additional metadata. The final score is a weighted blend tuned per domain so that a verified API schema outranks a two year old forum post even when both mention the same functions. Ranking’s job is not to find everything relevant; it is to establish a clear ordering by “tokens worth spending,” which preserves headroom for later decisions about how much context a given query earns.
Stage 3 – Route
Not every request deserves the same amount of context and not every request needs the same model. Routing is the explicit decision point where you choose both. A simple fact lookup with a high confidence match in the ranked list does not need a 200k token window on your most expensive model. Conversely, a complex multi source query with ambiguous wording might warrant a deep path that uses more chunks and a stronger model.
One practical approach is to classify queries for complexity and then combine that with the top ranked score to choose a `RouteDecision` such as MINIMAL, STANDARD, DEEP, or TOOL_ONLY. MINIMAL might forward three chunks to a fast inexpensive model, STANDARD fifteen chunks to a mid tier model, and DEEP sixty chunks to a frontier model. TOOL_ONLY explicitly represents requests where no ranked text is helpful and the right move is to call a tool or API instead. Mapping these route decisions to concrete models keeps cost predictable and prevents the default “always send top 20 chunks to the strongest model” pattern that many pipelines accidentally fall into.
Stage 4 – Compress
Even after ranking and routing, the surviving content is usually more verbose than it needs to be. Compression reduces token count without losing information density. Naive truncation is dangerous because it removes information at an arbitrary boundary, sometimes cutting off the key sentence or table halfway through. Compression removes redundancy while preserving the facts.
In practice three compression strategies cover most real world data. Extractive compression scores sentences by information density – for example via numbers, named entities, and technical terms – and keeps the highest scoring ones until the token budget is exhausted. Abstractive compression delegates to a cheaper model that rewrites the passage more compactly while explicitly being instructed to preserve all facts, numbers, and entities. Structural compression recognizes that some prose is actually structured data in disguise and converts it into a denser representation such as key value pairs or tables. A helper like `compress_chunk` can auto select a strategy based on source type and content shape so each chunk is treated appropriately.
Assembling the pipeline
The four stages matter most when they operate together. A full context pipeline might be implemented as an `async build_context` function that takes a user query and a collection of raw sources. First it calls the ingestion routines to normalize everything into `IngestedChunk` objects; then it embeds the query and each chunk to produce a ranked list via the multi signal scoring function. Next it estimates query complexity, decides a route, and either returns an empty context for TOOL_ONLY cases or selects a slice of the ranked chunks for MINIMAL, STANDARD, or DEEP paths. Finally it compresses each selected chunk toward a route specific budget and assembles them into the final context string separated by clear markers.
Each stage is designed to protect the same scarce resource: the model’s attention. Ingest keeps noise and unattributed content from ever entering the pipeline. Rank ensures the most valuable material surfaces before tokens are spent. Route ensures each request pays only for the context tier it actually needs. Compress guarantees that the surviving content fits inside the window without throwing away the information that made it worth including. Viewed this way, context engineering is not an optional optimization but the core plumbing of any serious agentic system.
What context engineering replaces
Prompt engineering asks how to phrase a request so the model understands what you want. Context engineering asks a prior question: what does the model need to know in order to answer this at all, and what is the smallest footprint that information can occupy without losing its usefulness. The second question scales because it applies across thousands or millions of requests and improves as ranking signals, routing logic, and compression strategies become more sophisticated. A cleverly worded prompt helps exactly once on exactly the query it was designed for.
The practical shift underway is away from clever instructions and toward better infrastructure. A well designed context pipeline becomes the backbone of your application, continuously shaping what the model sees and how much it pays for it. The patterns described here reflect what production systems looked like as of July 2026 and the code examples are illustrative rather than drop in ready – real deployments need solid embedding infrastructure, logging and observability, error handling, and domain tuned weights. But the underlying idea is simple: treat context engineering as an explicit discipline and the reliability, cost profile, and safety of your agents will follow.