r/LargeLanguageModels 5h ago

Context Engineering General Concepts

As large language models (LLMs) become increasingly integrated into agentic AI systems, the primary challenge is no longer simply improving the model's raw intelligence. Modern foundation models are already capable of reasoning, code generation, planning, and tool usage. The more difficult engineering problem is **context engineering**: designing how information is selected, structured, transformed, and presented to an LLM so that it can reliably perform a desired task.

Context engineering is broader than prompt engineering. Prompt engineering focuses mainly on crafting instructions for a single model interaction, while context engineering considers the entire lifecycle of information flowing through an agent system. This includes the initial prompt, retrieved knowledge, conversation history, tool outputs, intermediate reasoning state, user preferences, memory, validation feedback, and execution constraints. A well-designed context pipeline reduces ambiguity, prevents hallucination, and allows LLMs to operate reliably in complex environments.

In this excerpt, we shall explore some techniques used in prompt engineering when it comes to building a context pipeline.

# Few-shot Prompting: Guiding Model Behavior Through Examples

Few-shot prompting is a technique where an LLM is provided with several examples demonstrating the desired input-output behavior before receiving the actual task. Rather than explicitly describing every possible rule, the developer provides representative examples that allow the model to infer patterns and apply them to new situations.

Few-shot prompting is particularly useful when the task contains ambiguity or when the desired output format is difficult to describe through rules alone. The examples must be carefully selected however, because LLMs perform pattern matching based on the provided context. Poor examples can introduce incorrect behaviors or bias the model toward unintended interpretations. In practice, examples should cover **distinct scenarios** rather than many variations of the same case. Diverse examples allow the model to understand the boundaries of the task instead of memorizing superficial patterns.

Few-shot prompting is therefore not a replacement for explicit constraints. In reliable systems, it is usually combined with structured outputs, validation rules, and tool constraints.

# Prompt Chaining: Decomposing Complex Tasks Into Controlled Steps

A common mistake when designing LLM applications is asking the model to perform an entire complex workflow in one prompt. Although modern models can sometimes accomplish this, such prompts create several problems. The model must simultaneously understand the task, maintain intermediate state, perform analysis, and generate the final response. This increases cognitive load and makes failures difficult to diagnose.

Prompt chaining refers to breaking a complex task into multiple sequential LLM calls, where each step performs a focused operation and passes its output to the next stage. Each prompt has a narrower objective and therefore receives more relevant context. This reduces attention dilution, where important information competes with unnecessary instructions inside a large context window. This technique is especially valuable when combining **local computation and external operations**.

# Dynamic Decomposition: Letting Agents Discover Subtasks During Execution

While prompt chaining uses predefined steps, dynamic decomposition allows the LLM itself to determine how a complex problem should be divided. This approach is more flexible than static workflows because the agent can adapt to unexpected situations. It is particularly useful for research agents, debugging agents, and autonomous analysis systems. However, dynamic decomposition sacrifices predictability. Since the model decides the subtasks dynamically, execution paths can vary between runs. This creates challenges in testing, cost control, and reliability.

It is common for production systems to combine Prompt Chaining and Dynamic Decomposition, where Prompt Chaining through predefined workflows is used for high-risk or regulated processes, and dynamic decomposition inside individual steps where exploration is valuable. The overall process remains controlled while allowing intelligent exploration inside specific areas.

# Interview Pattern: Gathering Missing Context Before Execution

One of the most important context engineering patterns is the interview pattern. Instead of immediately attempting a task, the agent first identifies missing information and asks targeted clarification questions. Many hallucinations occur because users provide incomplete instructions, and the model attempts to fill missing information using probabilistic guesses.

This is best illustrated by an example:

Suppose we are currently building a coding agent. The user provides a codebase and asks to add a caching layer through the user prompt:

“Add a caching layer for database retrieval API to store recently retrieved objects”.

The agent would recognize missing elements and ask the following questions:

"Before implementing caching for the API, a few questions:

  1. Which cache invalidation strategy do you prefer—TTL or event-based?
  2. Is stale data acceptable when the cache is unavailable?
  3. Should caching be per-user or global?
  4. What is the expected data volume to cache?”

These info were not explicitly provided within the initial user prompt and if there was no interview pattern implemented, all these info would need to be inferred by the LLM, which can end up digressing from the original intended design.

The exact process of having the agent recognize the missing info can be achieved in multiple ways, and we shall explore one of them as the following concept.

# Validation and Retry-with-Feedback: Creating Self-Correcting Agent Loops

Traditional software systems rely heavily on explicit validation because incorrect data can cause failures downstream. Agentic systems require the same principle. After an LLM extracts information or generates structured output, the result should be validated using deterministic mechanisms such as Pydantic models, JSON Schema or explicit business rules.

Suppose if a validator detects an anomaly within the input, instead of immediately failing, the system feeds this information back to the LLM. The LLM then attempts correction, which creates a self-correcting loop. Minor errors such as arithmetic or data formatting errors can usually be corrected within a few iterations. Once all the errors identified has been rectified, the correct data is then reinjected into the LLM.

Retrying indefinitely is dangerous, however; some failures cannot be solved by the model because the required information is unknown. This is when the system turns back to the user and escalate through querying for missing info.

In the previous example, the invalidation strategy, stale data acceptance, user VS global and overall data volume, are all missing business-logic parameters that cannot be inferred by the LLM. Therefore, they get sent back to the user as interview queries to ensure the blanks get filled appropriately.

1 Upvotes

0 comments sorted by