Agent Harness Tutorial · End to end · Zoom level 2
Context assembly
The model remembers nothing between calls. So before every single call, the harness rebuilds everything the model needs to read, from scratch, with one function called assembled(). What goes in decides what the model can do; what stays out keeps it safe.
Rebuilt every trip, from scratch. Nothing is patched in place. That makes every call easy to reason about: if you know the inputs, you know exactly what the model saw.
What is deliberately left out. Rohan's name, email and phone stay in the claims system. The harness's own notes about the conversation (what was asked, what was done) are read by code only, never shown to the model. Other customers' data, the trace, costs and check results never go in. There is no retrieval of documents in this agent.
No summarising. When a conversation gets long, we drop whole old exchanges rather than ask a model to summarise them. A summary can carry an attack forward in disguise, loses exact numbers, and costs another model call.
Want every detail? The long version is in the Series: context assembly in full.
Code view: input, process, output, the call stack, the classes
How it is done: the real code
Cut from the live repositories each time the site is built, so it always matches what runs.
agent_harness/context/__init__.py:143assembled(): one function, every tripdef assembled(
*,
system: str,
history: Sequence[Message],
max_chars: int = 24_000,
) -> Assembly:
"""Order for cache friendliness, then trim whole exchanges from the middle.
The system prompt is a stable prefix and goes first and unchanged: any byte
that moves invalidates every cached token after it. Trimming takes from the
middle rather than the tail, because the most recent turns are what the model
is answering and the earliest establish the task.
**Trimming drops whole exchanges.** An earlier version dropped individual
messages by index and orphaned a tool call in eight of fifty-five
turn-and-budget combinations — an assistant turn claiming a call whose answer
had been removed, which every provider rejects. Nothing caught it because no
test ran a conversation long enough to trim.
Compaction is still deliberately absent: a summary inherits the provenance of
everything it summarised, and getting that wrong is worse than a shorter
window.
"""
head = Message(role="system", content=system, provenance="operator")
units = exchanges(history)
before = len(units)
def size(groups: Iterable[list[Message]]) -> int:
return sum(len(m.content) for group in groups for m in group)
while len(units) > 2 and size(units) + len(system) > max_chars:
units.pop(len(units) // 2)
kept = [m for group in units for m in group]Tech choices (what we chose, Azure and open-source options): L1 · Context assembly