Sections in this lesson

Context Engineering

Select the right evidence, history and tool results within a bounded context budget.

Reviewed: · Content owner: Interview Preparator

What you will learn

  • Separate instructions, working state and retrieved knowledge.
  • Filter access before selecting evidence.
  • Use a budget with room for response and tool output.

Before you start

The flow

  1. 1.Authorize
  2. 2.Retrieve
  3. 3.Rank
  4. 4.Fit budget
  5. 5.Generate

Build the request state deliberately

A prompt is one part of context. The request also carries tools, prior turns, evidence and intermediate results. Preserve authoritative constraints while selecting task-relevant information; including the entire history is rarely a good default.

Separate recent dialogue, a compact working summary and externally retrieved knowledge. Summaries can lose exact identifiers or negate a constraint, so retain critical values in validated state.

Budget after access checks

Never let model-provided tenant IDs decide document access. Apply authenticated identity and ACL filters before ranking. Then select evidence under a token budget, retaining document IDs for citations and debugging.

Use actual model tokenization in a real service. The example receives fixture token counts and uses greedy selection; it is not an optimal knapsack solver and does not estimate tokens from text.

Detect missing context

If a high-ranked passage does not fit, consider a smaller chunk or another retrieval step. If evidence remains insufficient, ask a clarifying question or abstain. Track retrieval misses separately from generation errors.

Try it

context-engineering.mjs
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import assert from 'node:assert/strict';
function selectContext(documents, tenant, budget) {
if (!Number.isInteger(budget) || budget < 0) throw new Error('Invalid budget');
const selected = [];
let used = 0;
for (const doc of documents.filter(d => d.tenant === tenant)
.sort((a, b) => b.score - a.score)) {
if (used + doc.tokens > budget) continue;
selected.push(doc.id);
used += doc.tokens;
}
return { selected, used };
}
const documents = [
{ id: 'private', tenant: 'B', score: 1, tokens: 10 },
{ id: 'large', tenant: 'A', score: 0.9, tokens: 80 },
{ id: 'small', tenant: 'A', score: 0.8, tokens: 25 },
];
assert.deepEqual(selectContext(documents, 'A', 30), { selected: ['small'], used: 25 });
assert.deepEqual(selectContext(documents, 'A', 0).selected, []);
console.log(selectContext(documents, 'A', 30));
// Tenant is supplied by authenticated application code, never by model output.

Run with Node.js 22: node examples/generative-ai/context-engineering.mjs. This is a deterministic teaching example, not a live model call.

Interview questions

How is context engineering broader than prompting?

It manages the full request state across turns, including history, evidence, tools and budgets.

Why not include all retrieved passages?

Irrelevant or conflicting content spends tokens and can distract from useful evidence. More context is not automatically better.

Where is tenant isolation enforced?

In the application and retrieval layer using authenticated identity, not a model instruction.

Practice

Add duplicate document handling and preserve a required policy passage inside the context budget.

Check your solution

  • No cross-tenant document is selected.
  • The total is bounded; required context overflow fails explicitly.
  • A zero budget returns no optional evidence.

Official references

Continue learning

Category sequence

Generative AI

Lesson 3 / 8

Was this lesson useful?

Change notes: Initial bilingual lesson and executable example.