Contents Part 1 · Foundations
Part 1 · Foundations
What context engineering is: choosing what a language model sees, measured on one real question
One question asked four ways, with no context, with every document, with the five most similar chunks, and with the rows of one graph query, and what each costs and gets right.
Free lesson Code zip with this lesson
About 18 minutes to read, and about 25 minutes for the hands-on lab.
Your progress0 of 9 checks answered right
It fills only when a prediction matches the measured result. Reading a page does not move it. It is kept in this browser.
A language model can only use what you put in front of it. This lesson shows what that means in practice, with one real question about a fictional company, asked four ways: with no context, with every document the company has, with the five pieces of text most similar to the question, and with the rows of one query on a knowledge graph. You will see what each one cost in tokens and dollars, and how many of the seven right answers the model found. By the end you will be able to say what context engineering is in one sentence, and why it is a design job rather than a prompt-writing trick.
After this lesson you will be able to:
- say what a model's context is made of, and why its private facts must come from you;
- explain context engineering as four moves: writing context down, selecting it, compressing it and keeping it apart;
- run the course's first lab: make Ferncliff Robotics' data, load it into Neo4j, and ask one question four ways;
- read the result: input tokens, cost per answer and a score computed by code, never by eye;
- say why the answer with the fewest tokens was also the most correct one, and why that is not always true.
What you need first: the lesson "Before you start", with your Neo4j database answering, and a model API key in OPENAI_API_KEY.
Time: about 25 minutes to read, and about 5 minutes for the lab to run.
What it costs: our run made 12 calls to gpt-5.4-mini and one call to make embeddings. The most expensive call, with every document in it, cost $0.00773. The machine we ran on, an AWS EC2 t2.small, costs $0.023 an hour.
Dates and times: every time in this lesson is in UTC. The run started at 07:38:51 on 6 December 2026.
Code: every file used in this lesson is in the code for this lesson, with a START-HERE note.
The problem: the question that needs three documents and a join
Ferncliff Robotics builds warehouse robots. This morning a supplier confirmed a winding fault in one batch of drive motors, and Ferncliff's engineers wrote it up as incident INC-0412 and recalled the part. The operations lead now needs one list, today:
Which customers run a robot that contains the drive motor recalled in incident INC-0412, and who is the account manager for each of them?
Nobody at Ferncliff has this list written down. To make it by hand, you would open the incident report to see which part was recalled, open the product manuals to see which robot model contains that part, and then open every customer's account note to see who runs that model and who manages the account. The facts are all in the company's documents. They are just in different documents, and the answer only exists once you join them.
Why it matters: the model does not know, and it cannot guess
A language model is a program that writes text one small piece at a time, choosing each piece from what it learned in training and from everything it has been given in this call. Ferncliff's customers were never in any training data, because Ferncliff's documents are private. So for this question the model has nothing to work with except what you put in the call.
Everything the model receives in one call is its context. In a real application it usually has four kinds of content:
- Instructions: what the model is for and how to answer, often called the system prompt.
- Tools: descriptions of functions the model may ask your program to run, such as a database query.
- History: the conversation so far, and the results of earlier steps.
- Facts: the information you fetched for this question, from documents, databases or a graph.
The model reads all of it as tokens, small pieces of text that are often parts of words. You pay for every token, the call takes longer the more there are, and every model has a limit on how many it can read at once. The question this lesson answers is about the fourth kind: which facts do you put in, and in what form?
The idea: context engineering is choosing the tokens
Context engineering is the work of choosing what goes into a model's context, in what form, and within what budget. Anthropic's engineering team describes it as the strategies "for curating and maintaining the optimal set of tokens" while a model works. The word "optimal" matters: not the most tokens, and not the fewest, but the set that lets the model do the job.
LangChain's post on the subject sorts the methods into four moves, and they make a good checklist:
| Move | What it means | An example at Ferncliff |
|---|---|---|
| Write | keep useful information outside the context window, to bring back later | save what a customer told the support agent last week |
| Select | bring in only what this step needs | fetch the rows about one recalled part, not every manual |
| Compress | shorten what is there | turn graph rows into one short sentence each |
| Isolate | give separate tasks their own, smaller contexts | let one helper search the incidents and another the accounts |
This lesson is about select and compress, the two that decide most answers in retrieval. Part 5 is about write, when you build an agent's memory.

Read the four cards as a checklist you can hold up against any AI feature: for each move, ask whether the feature does it and how. The two bands under the cards hold for all four moves. Keep the second band in mind for the rest of the lesson: the lab below changes only the facts, and never the model or its instructions.
Here is the plan of the experiment. The same model gets the same instructions and the same question every time. Only the facts change:
| What the model gets | How it is chosen | |
|---|---|---|
| A | nothing but the question | not at all |
| B | all 75 of Ferncliff's documents | everything, pasted in whole |
| C | the 5 chunks of text most similar to the question | plain vector search |
| D | the rows of one Cypher query, written as short sentences | a query on the knowledge graph |
Option C needs one more word. To find text "similar" to a question, a program turns each piece of text into an embedding: a list of numbers that a model produces so that texts with similar meaning get similar numbers. Cut the documents into chunks of a few hundred characters, make an embedding for each, make one for the question, and pick the chunks whose numbers are closest. That is vector search, and it is the first step of most retrieval systems today.
The hands-on: one question, four contexts
Step 1: make Ferncliff's data
The program make_data.py makes the whole company from a fixed starting number, so your data is identical to ours. It writes the facts twice: as data/graph.json, a list of nodes and relationships for Neo4j, and as 75 plain text documents in data/docs/, the way a real company keeps them: 3 product manuals, 12 customer account notes and 60 incident reports. It also writes data/truth.json, the right answer to the question, computed from the facts. Nothing in the answer is typed by hand.
$ python make_data.py
docs 75 robots 49 customers 12 parts 27 incidents 60 affected 7
So the right answer has 7 customers. Here is one of the documents the answer depends on, the account note of one customer, exactly as the generator wrote it:
Customer account note: Bluewharf Logistics Leeds
Bluewharf Logistics Leeds runs a warehouse in Leeds. Their account manager at Ferncliff is Tomas Becker.
They run 5 Ferncliff robots:
- serial FR1004, a FR-900 Heavy Lifter.
- serial FR1005, a FR-200 Picker.
- serial FR1006, a FR-900 Heavy Lifter.
- serial FR1007, a FR-200 Picker.
- serial FR1008, a FR-200 Picker.
Contract: annual service, next-business-day parts. Contact the account manager for any change.
Notes from the last review: the customer is happy with uptime and asked about a second site next year.
Notice what this note does not say: it never mentions a drive motor, a part number or the incident. That is normal. An account note is about a customer, not about parts.
Step 2: load the same facts into Neo4j
load_graph.py loads graph.json into your database as a graph:
(:Customer)-[:MANAGED_BY]->(:Person)
(:Customer)-[:RUNS]->(:Robot)-[:IS_MODEL]->(:Model)-[:CONTAINS]->(:Part)-[:SUPPLIED_BY]->(:Supplier)
(:Incident)-[:ON_ROBOT]->(:Robot) (:Incident)-[:ABOUT_PART]->(:Part)
Read the arrows as sentences: a customer RUNS a robot, a robot IS_MODEL a model, a model CONTAINS a part. The lesson "Modelling the Ferncliff knowledge graph" teaches every line of this program. For now, run it twice:
$ python load_graph.py
loaded 162 nodes and 284 relationships in 5.99 s
$ python load_graph.py
loaded 162 nodes and 284 relationships in 0.59 s
The second run found every node and relationship already there and created nothing new, so the counts did not change. The lesson "Your first Cypher: CREATE, MATCH and MERGE" explains why.

Read each arrow as a short sentence, from the circle it leaves to the circle it points at. Then follow the numbers from 1 to 5: those are the five connections the recall question needs, and each one is a join you would otherwise make by hand across three kinds of document. Notice that hops 2, 3 and 4 go against the arrows. A relationship is stored with a direction, but a query may walk it either way.
Predict, then measure
Vector search picks the 5 chunks most similar to the recall question. Where do you expect those 5 chunks to come from?
The measured result appears once you lock in. Only your first answer counts toward your progress.
Step 3: the experiment
context_experiment.py builds the four contexts and asks the question with each one, three times. For every call it records the input and output tokens that the API itself reports, the time, and a score computed by code: how many of the 7 affected customers the answer named, how many it named wrongly, and whether each account manager is right.
The instructions are the same for all four (one line in the program, wrapped here to fit):
You answer questions about Ferncliff Robotics using only the context you are given. Reply with JSON only:
{"answer": [{"customer": "...", "manager": "..."}], "note": "..."}. If the context does not contain the
answer, return an empty answer list and say so in note.
Asking for JSON in a fixed shape is what lets a program score the answer. You never judge an answer by reading it and nodding.
The graph context, option D, comes from this one Cypher query. It reads like the sentence you would say aloud: start at incident INC-0412, follow ABOUT_PART to the part, follow CONTAINS backwards to every model with that part, then IS_MODEL backwards to every robot of that model, then RUNS backwards to the customers, and MANAGED_BY to their managers:
MATCH (i:Incident {id: 'INC-0412'})-[:ABOUT_PART]->(p:Part)<-[:CONTAINS]-(m:Model)<-[:IS_MODEL]-(r:Robot)<-[:RUNS]-(c:Customer)-[:MANAGED_BY]->(a:Person)
RETURN p.code AS part, p.kind AS kind, m.name AS model, c.name AS customer, a.name AS manager, collect(r.serial) AS robots
ORDER BY customer
The program turns its rows into one short sentence each. This is the compress move: the model gets the joined facts, not the documents they came from.
Before you run it, make one prediction.
Predict, then measure
Which of the four contexts do you expect to name all 7 customers, with nobody wrong, in all three runs?
The measured result appears once you lock in. Only your first answer counts toward your progress.
Step 4: run it, and read what it measured
$ MODEL=gpt-5.4-mini RUNS=3 python context_experiment.py > context-run.json
$ python summarise.py context-run.json
A_no_context: input 107 tokens, output 40-44, time 855-1120 ms, right [0, 0, 0] of 7, wrong [0, 0, 0], managers right [0, 0, 0], cost per answer $0.00026-$0.00028
B_everything: input 9013 tokens, output 97-216, time 807-1586 ms, right [7, 4, 4] of 7, wrong [4, 0, 1], managers right [7, 4, 4], cost per answer $0.00720-$0.00773
C_vector_top5: input 384 tokens, output 51-55, time 669-912 ms, right [0, 0, 0] of 7, wrong [0, 0, 0], managers right [0, 0, 0], cost per answer $0.00052-$0.00054
D_graph_query: input 390 tokens, output 111-138, time 835-978 ms, right [7, 7, 7] of 7, wrong [0, 0, 0], managers right [7, 7, 7], cost per answer $0.00079-$0.00091
The query itself took 17.85 ms in Neo4j and returned 7 rows. Here is exactly what the model was given in option D, all of it:
Incident INC-0412 recalled part DM-11 (a drive motor).
Bluewharf Logistics Leeds runs 2 FR-900 Heavy Lifter robot(s) (FR1004, FR1006), which contain part DM-11; their account manager is Tomas Becker.
Harbourline Logistics Monterrey runs 2 FR-900 Heavy Lifter robot(s) (FR1017, FR1019), which contain part DM-11; their account manager is Priya Nair.
Keystone Logistics Columbus runs 1 FR-900 Heavy Lifter robot(s) (FR1011), which contain part DM-11; their account manager is Mei Lin.
Keystone Logistics Osaka runs 2 FR-900 Heavy Lifter robot(s) (FR1035, FR1034), which contain part DM-11; their account manager is Daniel Okafor.
Northgate Logistics Rotterdam runs 2 FR-900 Heavy Lifter robot(s) (FR1003, FR1001), which contain part DM-11; their account manager is Asha Rao.
Silverline Logistics Pune runs 1 FR-900 Heavy Lifter robot(s) (FR1014), which contain part DM-11; their account manager is Daniel Okafor.
Tallgrass Logistics Reno runs 1 FR-900 Heavy Lifter robot(s) (FR1046), which contain part DM-11; their account manager is Tomas Becker.
And the five chunks that vector search chose for option C came from these files, in order of similarity: incident-inc-0412.txt, incident-inc-0451.txt, incident-inc-0450.txt, incident-inc-0441.txt and incident-inc-0422.txt. Every one is an incident report.
What happened, option by option
A, no context: 0 of 7, and honest about it. With 107 tokens, nothing but the instructions and the question, the model returned an empty list in every run, as the instructions asked. That is the best possible behaviour for a model that does not know: it did not invent customers. Do not count on it. Without the instruction to say "I do not know", many models will guess.
B, every document: right only once, and never cleanly, at about eight to nine times the cost. With all 75 documents, 9,013 input tokens, the three runs gave three different answers. In the first, the model named all 7 affected customers with the right managers, and also named 4 customers who were not affected. In the second, it named 4 of the 7 and nobody wrongly. In the third, it named 4 of the 7 and one wrong customer. Every fact it needed was in front of it every time. What it had to do was the join: find the recalled part in one report, find which model contains it in one of three manuals, then go through twelve account notes and pick out every customer with that model, with 60 incident reports in between that have nothing to do with the question. Sometimes it stopped part of the way, and sometimes it went too far and pulled in customers who run a different model. Each of these answers cost between $0.00720 and $0.00773, against $0.00079 to $0.00091 for option D.
Watch out: a long context does not fail in one predictable way. It can miss facts, and it can add wrong ones, and the same call can do either on a different run. A wrong customer on a recall list is worse than a missing one: someone would phone a customer whose robots are fine.
C, the 5 most similar chunks: 0 of 7, and the cheapest wrong answer. Vector search did its job exactly as designed: it found the text most similar to the question. The question talks about a recalled drive motor and an incident, so the most similar texts were incident reports, starting with the right one. But the customers are named in account notes, and an account note looks nothing like this question. Similarity cannot see that the answer lives in documents that do not resemble the question. With 384 tokens of the wrong facts, the model correctly said it could not answer.

Read the figure from the question down. Vector search followed the only arrow there is, to the stack that sounds most like the question, and kept five incident reports from it. The red note marks where the answer actually was: the green stack, which no arrow reaches.
D, one graph query: 7 of 7, in 390 tokens. The query followed the same chain a person would follow by hand, but in the database, in 17.85 ms. The model received the join already done, one sentence per customer, and its only job was to copy the list into the JSON shape. It named all 7 customers with the right managers, in every run.
Why the fewest tokens won here, and when they will not
It is tempting to read this table as "graphs beat everything". Do not. Read it as: the model did best when its context held exactly the facts the question needed, already joined, and nothing else. A graph was the tool that made that possible for this question, because the question is a chain of connections: incident, part, model, robot, customer, manager. Questions shaped like a chain, a count over a group or a complete list are where a graph helps most. The lesson "Graph or vector retrieval, measured" measures more of them.
Many questions are not shaped like that. "What does the FR-900 manual say about cleaning the lidar window?" is answered by one chunk of one document, and vector search finds it with no graph at all. The same lesson shows how to tell the two apart, and Part 6 measures a full question set of every kind, including the questions where the graph loses.
One more thing to keep: this experiment used one question and three runs a condition. Options A, C and D gave the same score in all three runs. Option B did not: the same 9,013 tokens gave 7, 4 and 4 right, with 4, 0 and 1 wrong. One run is a sample, not a result, so every claim this course makes is measured more than once. Every claim this course makes about graphs against plain search is backed by a question set, never by one example.
Questions people ask
- Is context engineering the same as prompt engineering?
No. Prompt engineering is mostly about the wording of the instructions. Context engineering is about everything the model sees, and most of all about which facts are fetched and how they are packed. In this lesson the instructions never changed, and the score went from 0 to 7 of 7 because of the facts alone.
- Why not just use a model with a bigger context window?
A bigger window lets you paste in more, but option B shows that having every fact in the window is not the same as using it. The model still had to find and join facts scattered across 9,013 tokens. In two runs out of three it missed 3 of the 7, and in the run where it found all 7 it also named 4 customers who were not affected, while paying for every token each time.
- Do I need a graph database to do context engineering?
No. Context engineering is the choice of what goes in; a graph is one way to fetch and join the facts. Plain vector search, a SQL query or a single document can be the right context for a question. This course uses Neo4j because many real questions follow connections, and a graph keeps those connections ready to follow.
Knowledge check
Tap an answer to see straight away whether it is right, and why. You can try the whole check again at the end.
- Question 1
Which of these is the best one-sentence description of context engineering?
- Question 2
In the lab, what did the model do with no context at all?
- Question 3
The answer built from one graph query used 390 input tokens. The answer built from every document used 9,013. Which statement is true?
- Question 4
Which of LangChain's four moves did turning the graph rows into one short sentence each perform?
- Question 5
Which question is plain vector search most likely to answer well, with no graph at all?
0 of 5 right so far, 0 of 5 answered.
Words from this lesson
| Word | What it means |
|---|---|
| context | everything a model receives in one call: instructions, tools, history and facts |
| token | a small piece of text, often part of a word; models read, and are billed, in tokens |
| context engineering | choosing what goes into a model's context, in what form, and within what budget |
| write, select, compress, isolate | the four moves of context engineering: keep it outside, bring in only what is needed, shorten it, keep separate jobs apart |
| chunk | a piece of a document, a few hundred characters long, cut so that it can be searched |
| embedding | a list of numbers that stands for a piece of text, so that similar texts get similar numbers |
| vector search | finding the chunks whose embeddings are closest to the question's |
| join | putting facts from different places together, such as a part, the models that use it and the customers that run them |
What to keep
Context engineering is choosing the tokens a model sees. On one real question, the same model with the same instructions found 0, between 4 and 7, 0, and 7 out of 7, depending only on the facts it was given. Pasting in every document cost the most and was the only option that was not steady: it missed customers or named wrong ones, because the model had to do the join itself. Plain vector search found the most similar text, which was the wrong text for this question. One graph query did the join in 17.85 ms and handed the model 390 tokens of exactly the facts it needed. The rest of this course teaches you to build that kind of context, and to measure when it is the right choice.
The next lesson, "Nodes, relationships and properties", shows how a graph database stores facts like these so that a join becomes a walk along relationships.
Sources
- Our own lab run of 6 December 2026, on an AWS EC2 t2.small in us-east-1, with Neo4j 2026.10.2 Community Edition in Docker and the neo4j Python driver 6.4.1; the experiment started at 07:38:51 UTC: the run files
2026-12-06-load.txtand2026-12-06-context-gpt-5.4-mini.jsonin the course's lab folder. The model reported itself as gpt-5.4-mini-2026-03-17; the embeddings came from text-embedding-3-small. - OpenAI's API pricing page, standard tier, read on 14 November 2026 and checked again on 6 December 2026 at https://developers.openai.com/api/docs/pricing: gpt-5.4-mini at $0.75 per million input tokens and $4.50 per million output tokens.
- Anthropic, "Effective context engineering for AI agents", 29 September 2025, https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents, for "for curating and maintaining the optimal set of tokens".
- LangChain, "Context Engineering", 2 July 2025, https://www.langchain.com/blog/context-engineering-for-agents, for the four moves: write, select, compress and isolate.
- The AWS Price List API for us-east-1, read on 14 November 2026 and again on 6 December 2026: t2.small $0.023 an hour.