cleandataengineering.aiAgent Harness, drawn simply
You are here: Tech stack › L1 · Context assembly

Agent Harness Tutorial · Tech stack

L1 · Context assembly

What the model sees each turn, built in one place, within a budget. Family: Think · one turn, in order.

AZURE OPTIONSFoundry prompt management✗ dropped: too thinOUR CHOICEOur assembleragent_harness/contextPrompts in Gitjinja2fixed replies onlyOPEN-SOURCE OPTIONSJinja templates✓ used: fixed replies onlyWHO DOES THE WORKAzure gives 5% · we code once 60% · each agent team still does 35%
Dark boxes are what we run. Solid boxes are used or planned; dashed boxes were considered or dropped (crossed). The bar: grey is what Azure gives, dark is what we code once, hatched is what each agent team writes.
Why our choiceNo product does cache-friendly ordering, fencing, trimming from the middle and a transcript check, so we code it once.
OptionWhere fromVerdictWhy
Our assemblerours✓ chosenagent_harness/context
Prompts in Gitours✓ chosen
jinja2ours✓ chosenfixed replies only
Foundry prompt managementAzure✗ droppedtoo thin
Jinja templatesopen source✓ usedfixed replies only

How it works inside the harness: Context assembly.

How it is done: the real code

Cut from the live repositories each time the site is built, so it always matches what runs.

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]