SSE Streaming
AgentLens uses Server-Sent Events (SSE) to stream pipeline execution to the browser in real time. The /query/stream endpoint yields NDJSON events as each pipeline stage completes, and the frontend renders them immediately via an EventSource connection.
Event Format
Each SSE event is a JSON object with a type field and adata payload. Seven event types flow through the stream:
status– pipeline stage transitions (classified, grading, judging, retry, fallback)step– each ReAct agent iteration with thought, action, tool result, and timinggrader– chunk relevance scores, filtered/removed counts, Grader timingjudge– verdict (ACCEPT/RETRY), confidence, answer or feedback, Judge timingfallback– Fallback LLM completion with model, tokens, thinking, and raw responsedone– the completeQueryResponsewith sources, metrics, and totalserror– error message if the pipeline fails
Callback Architecture
The TwoAgentOrchestrator accepts 5 async callback functions that bridge the pipeline to the SSE stream:
on_step(AgentStep)– fired after each ReAct reasoning iterationon_status(stage, message)– fired on pipeline stage changeson_grader(dict)– fired after Grader completes with scores and filtering resultson_judge(dict)– fired after Judge completes with verdict and confidenceon_fallback(dict)– fired after Fallback LLM completes (if triggered)
Each callback serializes its payload to JSON and yields it through the SSE response. The callbacks are async to avoid blocking the pipeline while the event is being sent.
Frontend Connection
The pipeline debugger UI connects via the browser's native EventSource API. On each event, the UI:
- Parses the NDJSON payload
- Routes by event type to the appropriate renderer
- Updates the Live tab output with role-colored headers and content
- Captures an HTML snapshot into
stageSnapshotsfor the Trace tab - Stops the active spinner and starts the next one (if applicable)
Why SSE Over WebSockets
SSE is a natural fit for pipeline streaming because the data flows in one direction (server to client). The connection is simpler than WebSockets – no handshake upgrade, automatic reconnection built into the browser API, and standard HTTP infrastructure (proxies, load balancers) handles it without special configuration.