Concepts / Context Construction for Foundation Models

Context Construction for Foundation Models

The context given to a model for each query shapes both its accuracy and its exposure to risk, while instructions tend to stay the same across queries.

  • Programming

Two Assistants, One Question

Imagine two assistants receive the same question about a printer's speed. The first sees only the question. The second also sees the printer's technical specification sheet. Before reading on, decide which assistant is more likely to give a correct answer, and which is more likely to invent a number that sounds plausible.

What do you think happens?

Which assistant is more likely to give a correct answer about the printer's speed?

  • The assistant that sees only the question

  • The assistant that also sees the specification sheet

Reveal answer
Answer:

The assistant that also sees the specification sheet is more likely to answer correctly. The first assistant is more likely to invent a plausible-sounding number.

The difference lies in the context each assistant was handed. That difference is the starting point for understanding security risk in AI systems.

This article follows three questions: what information the model is shown, where that information came from, and what the model is allowed to do after it reads it. Security concerns with foundation models do not come only from the model's wording. Each question points to a part of the context that a builder can choose deliberately.

Instructions Versus Per-Query Context

Definition

Context construction is the deliberate selection of what goes into the model's input for each query.

A model needs two kinds of material to complete a task. The first is instructions, which describe how the task should be done. The second is context, which is the actual information needed to handle a particular request. For a given application, the instructions tend to be the same for every user and every question, while the context changes with each query. Context is where most of the variety, and most of the risk, lives.

When context is missing, the model is more likely to make mistakes and to produce confident statements that are not grounded in anything it was given. People behave the same way: someone asked a question without the relevant facts is more likely to guess wrongly. Careful context construction is therefore both a quality practice and a safety practice.

Part of the inputDoes it change per query?Typical example
InstructionsUsually noDescribe how the task should be done
Retrieved textYesSpecification details for the printer named in this query
Tool outputsYesResults returned by a web search during this task
User queryYesThe question being asked right now

Fixed instructions stay the same across queries, while retrieved text, tool outputs, and the user query change each time.

Tracing a RAG Query Through the System

Retrieval-augmented generation, usually called RAG, uses a two-stage flow. A retriever searches a memory store and selects a small set of items that appear relevant to the query. Those items are placed into the model's input, and the model writes its response while reading them. The model does not have to memorize every fact in advance, and the store can be updated without retraining the model.

User queryRetrieverSelected itemsSmall set judged relevantModel inputQuery plus selected itemsModel responseMemory storeExternal store of documentssearches withqueriesreturnsplaced intocombined withmodel writes
After a user query arrives, the retriever fetches items from memory and they are combined with the query before the model reads anything.
Worked example

Answering the printer speed question with retrieval

A user asks whether a particular printer can print 100 pages per second.

  1. 1
    Receive the query

    The question names one specific printer.

  2. 2
    Retrieve

    The retriever searches the memory store and selects the specification sheet for that exact printer.

  3. 3
    Build the input

    The selected specifications are placed into the model's input alongside the query.

  4. 4
    Generate

    The model answers while reading the specifications instead of relying only on what it absorbed during training.

Result

The answer is more detailed and better grounded, and the chance of a hallucinated figure is reduced.

A useful way to think about RAG is as a method of building a different context for each query rather than reusing one large, fixed context everywhere. That design has a direct security benefit. Private data about one user can be retrieved only for queries that belong to that user, so it never enters the prompt for someone else's question.

RAG also has a history. An early system first pulled five Wikipedia pages most relevant to a question and then had a reader model generate an answer from them. The name retrieval-augmented generation was later introduced for a broader setting, one where the full body of knowledge is too large to place inside the model's input at once. In that setting, only the information the retriever judges most relevant is passed along.

Tools and the Agentic Loop

The agentic pattern reaches the same goal of better context by a different route. Instead of retrieving from a fixed store, the model is allowed to call tools such as web search or news APIs to gather the information it needs. The model decides which tool to use, the tool returns results, and those results become part of the context the model uses to continue.

User taskModel decidesNeeds a tool?Call toolFor example web searchTool resultAdd to contextTask completestartsevaluatesyesnoreturnsbecomes part ofnext step
The model decides whether it needs a tool. Each tool result is added to the context, and the model decides again until the task ends.

Tools do more than supply information. They are the means by which a model can act on the world, and this is what makes the pattern powerful. External tools can compensate for a model's shortcomings and widen what it can do, which is also why the agentic pattern is considered capable of automating many everyday tasks. The same reach that makes agents useful is what makes their security posture deserve close attention.

Best practice

Read the loop as a set of channels. Each tool connection is a path through which information comes in and, for some tools, through which actions can go out. For every such path, ask what the model is permitted to request and what it is permitted to do with the result.

Why Long Context Windows Do Not Replace Retrieval

A common hope is that models with very long context windows will make retrieval unnecessary, since everything could simply be placed in the prompt. The source argues against that view on two grounds. First, however long a context window becomes, some applications will need more data than fits. The amount of available data keeps growing, and people add data far more often than they delete it.

Second, a model that accepts a long context does not necessarily use all of it well. The longer the input, the more likely the model is to focus on the wrong part. Every additional token also costs money and can add latency. RAG addresses this by letting the model see only the most relevant information for each query, which reduces the number of input tokens and can improve the quality of the answer at the same time.

The source points to practical testing of this behavior. It cites a 2024 writeup in which a practitioner tested a model on information embedded in long material about doctor visits. The lesson for security work is that a model's use of a long context should be tested, not assumed.

Where Output and Reach Create Risk

Two places deserve particular attention: what the model's output is allowed to trigger, and what tools the model can reach. One concrete risk named in the source is remote code execution. The source reports that such a risk was found in LangChain in 2023 and points to public GitHub issues describing it. The underlying danger is simple: if text produced by a model, or text that a model was influenced by, is passed to something that executes it as code, then the model's output can determine what runs on a machine.

Retrieved contextModel outputPassed tointerpreter?Displayed onlyTool reachRemote code executionScoped contextNarrow inputs and permittedtoolsshapesfeedsreachesnoyeslimitslimits
Output and tool access both flow toward possible harm. Scoping the context narrows what reaches the model and what the model can do with it.

The flowchart above is a general model of control flow, not a description of the specific LangChain issue. Its value is in the decision node. Whether the system displays a model's output or hands it to an interpreter is a choice the designer makes. That choice determines whether an unexpected output can cause an unexpected action.

Best practice

Treat model output as untrusted data. Do not let it reach an interpreter by default, and make the decision to execute it an explicit design choice.

Context Breadth: Before and After Scoping

Scoping means narrowing what enters the context for a query. The diagram compares a broadly loaded context with a narrowly scoped one. In the broad case, the model sees more information and more tools than the current query needs. In the scoped case, only the data that belongs to this user and the tools this task requires are present.

Narrowing the context does not only reduce risk. Because the model sees only the most relevant information, its answers can also become more accurate. Breadth is therefore a trade-off to design for, not a goal in itself.

Mistakes in Context Construction

  • Common mistake

    Loading all of a user's or an organization's data into every prompt

    What it looks like

    Every query includes the full data set, even when only one document matters.

    Why it fails

    Private data then enters prompts for questions that do not belong to its owner, and the model is more likely to focus on the wrong part.

    Do this instead

    Retrieve only the data that belongs to the current user and is relevant to the current query.

  • Common mistake

    Assuming a long context window guarantees correct use of the context

    What it looks like

    A very long input is passed in and the answer is trusted without testing.

    Why it fails

    The longer the input, the more likely the model is to focus on the wrong part.

    Do this instead

    Test how well the model finds specific information inside long inputs before relying on it.

  • Common mistake

    Passing model output directly to an interpreter

    What it looks like

    Text generated by the model is sent to something that executes it as code.

    Why it fails

    The model's output then determines what runs on a machine, which is the remote code execution risk described above.

    Do this instead

    Treat model output as untrusted data and make execution an explicit, reviewed design choice.

  • Common mistake

    Adding tools without reviewing what each one allows the model to request or do

  • Common mistake

    Treating the model as the place where security decisions get made

Practice: Scoping a Context

MEDIUM

A support assistant answers questions for many customers. Each customer has private account records in a memory store, and the assistant also has access to a web search tool. Describe how you would construct the context for one customer's question. Say which parts stay fixed, which parts change per query, and which tool access you would restrict.

Hints
  • Start by listing what stays the same across every customer's question.
  • Ask which account records this customer's question actually needs.
  • For the web search tool, ask what the model is permitted to request and what it may do with the result.

Key Takeaways

  1. 1
    Instructions usually stay the same across queries, while the context changes with each query and holds most of the variety and most of the risk.
  2. 2
    RAG retrieves relevant items from external memory and places them into the model's input, so each query gets its own context.
  3. 3
    The agentic pattern lets a model call tools, which widens what it can learn and do, so each tool connection deserves review.
  4. 4
    Model output should be treated as untrusted data, and scoping context narrows both what the model sees and what it can reach.

At a glance

  • The context given for each query shapes both accuracy and exposure to risk, while instructions tend to stay the same.
  • RAG places query-specific retrieved items into the model's input, which can reduce hallucinations and keeps private data limited to the right queries.
  • The agentic pattern lets a model call tools, so every tool connection is a path that deserves a security review.
  • Remote code execution shows why model output must be treated as untrusted, and scoping context limits what the model can see and reach.

Related concepts

More concepts starting with C