Blog

Context engineering is the new backend engineering

What reaches the prompt now matters as much as API design did. Context assembly needs contracts, budgets, tests, observability and an owner.

Jonathan Bree

The code that decides what goes into a model’s prompt now shapes an AI product’s quality, cost and reliability about as much as API and schema design shaped a web application. It deserves the same treatment: a written contract for each part of the prompt, token and latency budgets, tests that catch regressions, logs you can inspect after the fact, and a named owner on the team. Most teams still treat it as a string that a few people edit by hand, and that gap explains a large share of the agents that work in a demo and drift in production.

This piece makes the case for treating context engineering as a backend discipline. It is about how teams organise the work. For the arithmetic of where tokens go, see how you’re overspending on tokens, and for the memory side, the context window is not memory.

What context assembly does in a production agent

Every model call starts with an assembly step. The application gathers a system prompt, tool definitions, some conversation history, retrieved documents or memories, and the user’s message, then concatenates them into the context window. The model sees nothing else. If the right fact is missing, the answer is wrong; if ten irrelevant facts are present, the answer is often wrong too, and slower and more expensive.

A typical agent request in production looks something like this:

Part of the request

Typical tokens

System prompt

About 500

Conversation history

3,000 to 30,000, depending on session length

Retrieved chunks

1,500 to 3,000

User message

About 100

Total

Around 15,000

In a 20-message thread of around 12,000 tokens, only a few hundred are usually needed to answer the latest question, and of the top five retrieved chunks, one or two tend to contain the answer. The assembly code decided to send the rest. That decision is made on every request, by code that rarely has tests, rarely has a budget and is rarely logged in a form anyone reads. Where your tokens go breaks a request like this down in more detail.

Why it looks like backend engineering

The parallels are close. A backend engineer designs a schema so that each service gets the data it needs in a predictable shape. Context assembly does the same for the model, and the shape matters: structured output from tools, consistent field names and a stable order make the model’s job easier in the way a clean API makes a client’s job easier.

Query design is the next parallel. An unindexed query that scans a whole table works in development and fails under load. Sending the full conversation history is the context equivalent: it works for short sessions and degrades as sessions grow, because cost rises with every turn and accuracy falls as the window fills. Research on context rot found accuracy falling steadily with input length across all 18 production models tested, and the lost in the middle effect cuts accuracy by more than 30% when the relevant passage sits mid-input. A model’s effective context is often only 50 to 65% of its advertised window. Retrieval that selects a few precise passages is the indexed query.

Caching is another. Prompt caching is useful for a static prefix in the same way a CDN is useful for static assets, and it does nothing for the parts of the request that change every turn, which is most of them. We go further in prompt caching won’t save you. Then there are migrations: changing a retrieval setting or the order of prompt sections can change behaviour across every conversation at once, which is exactly the kind of change backend teams have learned to roll out carefully.

What the rigour looks like

A contract for each slot

Start by naming the slots in the prompt and writing down what each one is for, what it may contain and how large it may be. “Retrieved memories: up to 15 facts about this user relevant to the current question, newest first, no more than 600 tokens” is a contract. “Add some history” is not. With slots defined, a change to one slot can be reviewed on its own, and a regression can be traced to the slot that caused it.

Budgets

Each slot gets a token budget, and the whole request gets a latency and cost budget, the way an endpoint gets a p95 target. Budgets force the useful question: if retrieval can only spend 1,500 tokens, which 1,500? That pushes teams towards sub-document retrieval and towards structured memory in place of raw history, both of which return fewer, more relevant tokens. Context pruning and context compression help at the margins, but selection does most of the work. Our context budget template gives a starting allocation, and the token cost calculator estimates what a tighter budget would save.

Tests

Context changes need regression tests in the way schema changes do. A small golden dataset of real questions with known answers, run against each change to retrieval or prompt structure, catches most regressions before users do. Retrieval deserves its own checks: does the passage that contains the answer appear in what was assembled? Retrieval evaluation metrics answer that directly, and LLM as a judge can score the end answers at a scale people cannot.

Observability

When an agent gives a bad answer, the first question is what it was shown. If the assembled context is not logged, nobody can answer it. Log the final context for a sample of requests, record token counts per slot, and trace which retrieval calls fed which slot. LLM observability and agent tracing tools make this easier, and the habit matters more than the tool. Teams that can read the context behind a bad answer fix problems in hours; teams that cannot spend days guessing. Failures such as context poisoning and context distraction are only visible this way.

Versioning and rollout

Treat the prompt template and retrieval configuration as versioned artefacts. Keep them in source control, review changes, and roll them out gradually where traffic allows. A change that adds 2,000 tokens of history to every request is a change to your unit economics and deserves the same review as a pricing change.

Who should own it

In many teams, context assembly belongs to whoever touched it last. Prompt wording sits with a product manager, retrieval with an ML engineer, history handling with whoever built the chat UI, and nobody owns the sum. That is how requests grow from 3,000 tokens to 30,000 without anyone deciding they should. The fix is organisational: give the context layer a named owner, usually someone with backend instincts who cares about contracts, budgets and observability, and route changes through them.

This does not mean taking prompt writing away from the people closest to users. It means one person is accountable for what reaches the model as a whole, in the way one team usually owns the API gateway even though many teams ship endpoints behind it. The difference between context engineering and prompt engineering maps closely onto that split: prompt engineering is about wording, context engineering about what the model is given to work with.

A worked example

Take a customer support agent whose average request has grown to about 15,000 tokens. Answers are mostly right, latency is creeping up, and the LLM bill is growing faster than the user count. The team gives the context layer an owner, who writes slot contracts and finds that conversation history accounts for most of the tokens and top-five retrieval for most of the rest.

The owner replaces raw history with structured memory: durable facts about the customer, retrieved per question, at a few hundred tokens. Top-five chunks are replaced with sub-document passages at a higher precision setting for factual questions. A golden set of 200 past tickets is run before and after. In an illustrative request of this kind, input falls from about 15,100 tokens to about 2,800, roughly 81% fewer, and shorter, cleaner context tends to produce shorter answers too. Agent logic, prompts and the model stay the same; only the context layer changes. The reduce token costs page walks through the same pattern.

The usual objections

“Context windows keep growing, so this will stop mattering.” Larger windows raise the ceiling, but cost still scales with what you send and accuracy still falls as inputs grow. We make that case in bigger context windows are making agents worse. “Our framework handles it.” Frameworks provide components, and the decisions about budgets, slots and tests remain yours. “It’s just prompt tuning.” Prompt wording matters, but most production failures we see trace back to what was retrieved and included, which is a systems problem.

Where Exabase fits

Exabase provides two of the building blocks the context layer depends on; the assembly logic and its budgets stay in your application. Memory turns conversations into structured facts, preferences and events, so history can be replaced by a few hundred tokens of relevant memories retrieved per question. Retrieval is hybrid semantic and keyword search with recency bias, with optional query expansion and reranking controlled by expandQueries, rerankCandidates and rerankThreshold, as covered in retrieving memories. Simple retrieval takes about 200ms.

Deep Search returns sub-document passages with page numbers or timestamps, with a precision parameter from 0 to 1 (default 0.3) so each slot can trade recall against relevance; see searching resources. Both are scoped per Base, which keeps each user’s context separate. If you are weighing options for this layer, best AI context platforms and Exabase vs LlamaIndex set out the trade-offs.

FAQs

What is context engineering?

Context engineering is the work of deciding what goes into a model’s context window on each request: which instructions, history, retrieved documents, memories and tool results, in what order and at what size. It sits alongside prompt wording and usually has a larger effect on quality and cost.

Why compare context engineering to backend engineering?

Because the problems are the same shape. Both involve contracts between components, budgets for cost and latency, query design, caching, regression testing and observability. Teams that apply backend habits to context assembly tend to ship more predictable agents.

Who should own context assembly on a team?

A single named owner, usually an engineer with backend instincts, accountable for what reaches the model as a whole. Others can still write prompts and tune retrieval, with changes reviewed against the slot contracts and budgets.

How do you set a token budget for an agent?

Split the request into slots, give each slot a ceiling based on what it needs to do, and measure answer quality on a golden set as you raise or lower each ceiling. Start tight and loosen only where quality improves.

How do you test changes to context?

Run a fixed set of real questions with known answers before and after each change, and check both the final answers and whether the passages containing those answers were included. Treat a drop on either as a failed build.

What should be logged for context observability?

The assembled context for a sample of requests, token counts per slot, which retrieval calls fed each slot, and the model’s answer. That is enough to explain most bad answers after the fact.

Does prompt caching make context engineering less important?

No. Caching lowers the price of a static prefix, but the parts of a request that change every turn, such as history and retrieved passages, are where most tokens and most errors come from.

How much can better context assembly save?

It depends on the workload. In an illustrative support request, replacing raw history and top-five chunks with structured memory and sub-document retrieval cut input tokens by about 81%. The token cost calculator estimates it for your own numbers.

Related pages: Reduce token costs, Context engineering, Context engineering vs prompt engineering, Bigger context windows are making agents worse, A context budget template, Prompt caching won’t save you, Context rot, RAG accuracy, Best AI context platforms, Your LLM bill is a retrieval problem

Cut your token spend and give your agent precise context.

Get started in minutes.

Cut your token spend and give your agent precise context.

Get started in minutes.