Context Engineering Fundamentals

Learn how context engineering relates to prompt engineering, and how to select sources, verify versions, organize context, and retain information in long conversations.

🎯 Introduction

Suppose you are building AI support for a store. You explicitly instruct the model to use store policies and avoid inventing rules, yet it still answers incorrectly.

The prompt may not be the problem: the application may supply a retired policy or omit product details from an earlier message. The model cannot know business facts the application has not supplied, or determine whether a document is still effective from its title alone.

Beyond clear instructions, ask: what information belongs in this request? What should be excluded? How should conflicts be handled? What should be retained as a conversation grows?

These are context engineering questions.


🧱 Prompt Engineering and Context Engineering

Prompt engineering focuses on instructions that clearly define tasks, constraints, and expected outputs.

Context engineering also considers which information a task requires and how to select, update, and organize it. Context can include instructions, business documents, conversation history, and tool results.

They overlap: the prompt is itself part of the context. Context engineering addresses both how to express requirements and what information the model uses to meet them.

Consider:

Can I return the custom mug I bought?

Even “Answer return questions accurately” cannot replace the currently effective policy. Supplying an old policy can produce an incorrect answer.

Clear instructions do not establish a correct factual basis. Start with relevant, reliable, currently applicable sources.


🧱 Why Not Include Everything?

Suppose the customer asks about returns and the system holds:

SourceContentAction
Old after-sales policyRetired 2023 version allowing return requests within 15 days of deliveryExclude
Current after-sales policyConfirmed effective by the business system; seven days, excluding custom itemsKeep
Company annual party noticeEvent time and locationExclude

These policies are teaching examples, not actual merchant rules or legal requirements.

Including all three adds noise: the policies conflict, and the event notice is irrelevant.

Consider:

  1. Relevance: does the material help answer this question? Unrelated content can distract from key facts.
  2. Validity: is it still applicable? The application should confirm the effective version before supplying it.
  3. Cost and capacity: irrelevant sources consume tokens, increase cost, and occupy limited context space.

A newer date does not mean a policy is currently effective. An unpublished policy may be newer than the active one without being applicable. Use business-system status or another reliable basis.

Context engineering means retaining necessary information and excluding irrelevant, outdated, or unconfirmed material, rather than minimizing all content indiscriminately.


🧱 Organizing Effective Context

Continue with the same customer:

Can I return the custom mug I bought?

The application has confirmed the effective policy:

Return requests are allowed within seven days of delivery, excluding custom items.

Organize the context as follows.

1. Define the Current Task

The task is answering the customer's question using the current policy, rather than checking order status, approving a refund, or inventing return rules.

Clear boundaries help identify necessary information.

2. Select and Verify Sources

Keep the confirmed current policy and exclude old versions and unrelated notices.

The application or business system should do this. Do not hand conflicting versions to the model and ask it to guess.

3. Separate Instructions, Sources, and the Question

Instructions define how to answer, sources provide facts, and the question defines the current problem.

A request could contain:

Task:
Answer the customer's question using the currently effective after-sales policy.

Response requirements:
1. Use only the supplied policy; do not add return conditions.
2. Do not treat eligibility to request a return as proof that a refund is approved.
3. If the policy does not cover a possible resolution, state that further confirmation is needed.

Reference material:
Source: Store after-sales policy
Status: Confirmed effective by the business system
Content: Return requests are allowed within seven days of delivery, excluding custom items.

Customer question:
Can I return the custom mug I bought?

A possible response:

Under the supplied current policy, custom items are excluded from this seven-day return rule. Please contact a human support representative to confirm whether other options are available.

This avoids expanding the exclusion into “custom items can never be returned” or inventing an order review result.

This layout illustrates organization; it does not require concatenating everything into one string. Model APIs may provide message roles and other input structures for instructions and reference material.

4. Handle Missing and Conflicting Information

Plan for:

  • Missing information: do not ask the model to guess matters the policy does not cover; state that confirmation is required.
  • Conflicting information: if the effective version cannot be identified, have the application verify it first. Do not let the model choose and present a definite conclusion.

Application developers should focus on sufficient, reliable evidence, rather than making instructions lengthy.


💡 Handling Long Conversations

Context may include previous customer messages alongside business documents.

For example:

Customer: I previously bought Bluetooth headphones.
Customer: I already submitted the order number on the order page.
Customer: The left earbud has no sound.
Customer: Do you have any new models to recommend?
Customer: Let's leave new models for now. I want to request a return.

Keeping only the last message loses the product and fault details. Keeping everything indefinitely adds cost as the conversation grows.

One approach is to retain still-valid key information while compressing content unrelated to the current task:

Current request:
The customer wants to return Bluetooth headphones.

Known information:
- Product: Bluetooth headphones
- Fault: No sound from the left earbud
- The customer says the order number has been submitted, but this summary does not contain it

To verify:
- Retrieve and confirm the corresponding order from the order system
- Check purchase and delivery dates and the applicable after-sales policy

This preserves necessary background without turning “the customer says they submitted an order number” into “the order has been verified.”

Summaries can also omit or alter facts. Read or verify critical business values such as order numbers, amounts, and refund status against trusted original records, rather than treating a generated summary as final authority.

This is a basic context organization method. Later courses cover automatic retrieval, conversation memory, and dynamic context management.


❓ Knowledge Check

Q1: Does context engineering mean writing longer prompts?

No. It means selecting, verifying, and organizing necessary information. Shorter, accurate context is often more valuable than extensive irrelevant material.

Q2: Does more information guarantee greater accuracy?

No. It may include irrelevant content, retired rules, or contradictions. Accuracy also depends on reliable sources, clear requirements, and correct use of the material.

Q3: Can the model decide which of two policy versions is effective?

Do not treat its guess as a business fact. Check reliable version status first. If it cannot be confirmed, acknowledge that verification is pending rather than giving a definite answer.


🧪 Exercise

Suppose the support system holds:

SourceContent
ARetired 2023 policy: return requests allowed within 15 days of delivery
BCurrently effective policy: return requests allowed within seven days, excluding custom items
CEarlier customer message: I bought a custom mug
DPromotion notice: some products qualify for discounts this week

The latest question:

Can I return this mug?

Complete these tasks:

  1. Select sources for the model and explain exclusions.
  2. Write context containing the task, response requirements, reference material, and current question.
  3. Write the expected response.
  4. Explain how you would proceed if B's effective status had not been verified.

Success criteria:

  • Select B and C; exclude retired A and irrelevant D.
  • Distinguish application requirements, current policy, and customer-provided information.
  • Explain that custom items are excluded from the supplied seven-day rule without ruling out every other possible return route.
  • Verify uncertain policy status rather than asking the model to choose a version.

🎉 Congratulations! Context engineering means providing relevant, effective, verifiable information for the current task. You can now select key material when sources conflict or conversations grow.