Skip to content
C.W.K.
Stream
Lesson 02 of 05 · published

Assemble, Stream, Normalize

~13 min · loop, dialect, events

Level 0Cold Stick
0 XP0/41 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

One Step Is an Effect Sandwich

A step is one model request, its streamed response, and the execution of its tool calls. That is the atom of the loop, the record, and every experiment. The graph is a seam. The shipped orchestrator is still while True: step, stop when the turn ends with no calls.

Assemble builds the request the record will later replay: resolved identity layers, cloud harness files when enabled, cloud memory when enabled, tool schemas, parameters, a calibrated token estimate, and the exact dialect-encoded body stored by digest. Auth headers never enter that body. Local requests carry an explicit context length, forbid truncate, and refuse to send when the preflight estimate is already at the window. The request line is flushed before the socket sees it.

Normalize Is Where Vendors Stop Mattering

Stream is the leg's job. Normalize is the dialect's job. Native Ollama can deliver a whole tool-call object in one chunk, including a stop reason that still says 'stop' when calls were emitted — so the derived reason is wire reason and observed blocks together. OpenAI-shaped chat concatenates argument fragments and parses only at block end. Anthropic-shaped streams speak content-block deltas. A tool_call_end with complete=True is emitted only when that dialect knows the block closed. Open blocks fail as truncated. Unparseable or schema-invalid arguments become an invalid-call plus an error result. The engine never guesses the JSON the model owed.

Derived stop, not trusted stop. A wire that says 'stop' while tool-call blocks sit in the chunk is not done. A wire that dies mid-block is a cut. The record keeps both facts side by side.

Code

Derived stop in the smallest possible form·python
def derived_stop(wire_reason, blocks):
    if any(b.kind == "tool_call" and b.complete for b in blocks):
        return "tool_use"
    closed = all(not b.open for b in blocks)
    if wire_reason in ("stop", "end_turn") and closed:
        return "end_turn"  # text-only is still end_turn
    if any(b.open for b in blocks) and wire_reason in ("stop", "end_turn"):
        return "truncated"  # not cut — cut is mid-stream abort
    return wire_reason

External links

Exercise

Capture one vendor stream that includes a tool call. Write the wire reason and the blocks you actually received. If they disagree, you have found the exact reason Firebrand derives the stop instead of believing the last frame.
Hint
The disagreement is often 'stop' plus a tool_calls array, or a finish reason that arrives before the last argument fragment.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.