← Table of Contents · Page 211 of 868
CONTEXT CONSTRUCTION (THE CONTEXT BUILDER)
Part 7 of 12
A SMALL CONTEXT BUILDER IN WORDS
Let us describe, step by step, what a simple builder does on each call.
Step one: gather the identity block, the standing rules block, and the project conventions block.
Step two: generate the date and environment block fresh.
Step three: fetch the task and any pinned anchors, such as the original request.
Step four: count the tokens used so far and compare to the budget from Chapter 18.
Step five: choose history, keeping the newest messages and a summary of older ones if needed.
Step six: choose retrieved documents and tool outputs, trimming as needed.
Step seven: decide whether a system reminder is due, and if so, prepare it.
Step eight: assemble everything in the chosen order with clear labels.
Step nine: verify the total fits within the allowed size, leaving room for the answer.
Step ten: return the finished prompt to the loop.
Writing this as a single function with small helper functions for each step is a good first design.
Keep the function free of side effects when you can, meaning it reads inputs and produces text but does not change anything else, because functions like that are far easier to test.
Advertisement
Read Aloud:
0:00 / 0:00