cleandataengineering.aiAgent Harness, drawn simply
You are here: End to end › Core loop › Context assembly

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.

Goes inThe system prompt, the tool list, the saved conversation, Rohan's message, and this turn's tool results
What happensPut them in a fixed order, fence anything untrusted, trim to fit, check nothing is broken
Comes outA list of messages, ready for the model call
Every grey box opens a page that zooms in on that step.

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

Goes inthe system prompt and the run's message list
What happensMessages are grouped into exchanges that cannot be split, whole exchanges are dropped from the middle until the text fits, and the result is checked for orphaned tool calls.
Comes outan Assembly: messages, chars, exchanges, trimmed
CALL STACK · START TO FINISHGOES IN → COMES OUT1Open the source: agent_harness/loop/__init__.py:95run(...) # messages=[*history, ctx.user_message(goal)]agent_harness/loop/__init__.py:95in: history and goalout: the run's starting message list2Open the source: agent_harness/context/__init__.py:66user_message(text)agent_harness/context/__init__.py:66in: the customer's textout: Message(role='user', provenance='user')3Open the source: agent_harness/loop/__init__.py:244_Run._ask(step_span)agent_harness/loop/__init__.py:244in: self.messagesout: a ModelRequest over the assembly4Open the source: agent_harness/context/__init__.py:143assembled(system=, history=, max_chars=24_000)agent_harness/context/__init__.py:143in: system prompt and messagesout: an Assembly, or BrokenTranscript5Open the source: agent_harness/context/__init__.py:73exchanges(history)agent_harness/context/__init__.py:73in: flat messagesout: groups: an assistant call plus its tool replies6Open the source: agent_harness/context/__init__.py:143size(groups) # nested in assembledagent_harness/context/__init__.py:143in: the groupsout: total characters7Open the source: agent_harness/context/__init__.py:91orphaned(kept)agent_harness/context/__init__.py:91in: the kept messagesout: (calls with no answer, answers with no call)8Open the source: agent_harness/context/__init__.py:103BrokenTranscript(...) # raised if orphansagent_harness/context/__init__.py:103in: orphan idsout: a typed failure; _ask returns failed(TROUBLE)9Open the source: agent_harness/context/__init__.py:110Assembly(messages=, chars=, exchanges=, trimmed=)agent_harness/context/__init__.py:110in: system head and kept messagesout: the assembly, numbers go on the step span10Open the source: agent_harness/context/__init__.py:251model_tools(self.registry)agent_harness/context/__init__.py:251in: the ToolRegistryout: function definitions in registry order11Open the source: agent_harness/loop/__init__.py:354_Run._act(planned)agent_harness/loop/__init__.py:354in: tool resultsout: messages extended with tool replies12Open the source: agent_harness/context/__init__.py:51tool_message(result, tool_call_id=)agent_harness/context/__init__.py:51in: a ToolResult and the call idout: Message(role='tool', provenance='tool')13Open the source: agent_harness/contracts/tools.py:82ToolResult.for_context()agent_harness/contracts/tools.py:82in: the tool resultout: text that fits the context bound14Open the source: agent_harness/context/__init__.py:45fence(text, source='tool:<name>')agent_harness/context/__init__.py:45in: untrusted tool textout: text wrapped in an untrusted fence
Read top to bottom: each box is one function call, indented under the function that called it; the dark box is where it starts. Click a box to open its source file.
Dashed boxes are protocols (the ports) or enums; grey boxes are data models; white boxes are classes with behaviour. A dashed arrow means "implements". Click a class to open its source.

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 trip
def 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

Look inside