Skip to main content

Module stream

Module stream 

Source
Expand description

Streaming event types for LLM API responses.

Types used when consuming Server-Sent Events (SSE) based streaming responses from LLM APIs. The core StreamEvent enum represents each discrete event in the stream lifecycle, while StreamAccumulator collects those events into a complete Message.

Streaming allows the framework to process model output incrementally — displaying text as it arrives, detecting tool invocations as soon as the part starts, and reporting token usage without waiting for the full response. Essential for responsive agent behavior.

§Stream Lifecycle

The streaming protocol follows this event sequence:

MessageStart → [PartStart → IndexedDelta* → PartStop]* → MessageDelta → MessageStop

Ping events may appear at any point in the stream and should be ignored by consumers.

§Provided Types

§Sub-modules

  • handlerhandler::StreamHandler with retry, timeout, and fallback for resilient streaming.

§Quick Start

use loopctl::stream::{StreamAccumulator, StreamEvent, StreamStopReason};

let mut acc = StreamAccumulator::new();

// Feed events as they arrive from the SSE connection
for event in std::iter::empty::<StreamEvent>() {
    acc.process(&event).unwrap();
}

// Get usage before building (build consumes the accumulator)
let _usage = acc.usage();
let message = acc.build();

Re-exports§

pub use handler::DetectedRateLimit;
pub use handler::RateLimitConfig;
pub use handler::RateLimitKind;
pub use rate_limit::RateLimiter;
pub use rate_limit::TokenBucket;

Modules§

handler
Resilient LLM stream handling.
rate_limit
Proactive client-side rate limiting (token bucket).

Structs§

IndexedDelta
A delta (incremental update) for the current part.
MessageDelta
A delta update for the message, typically emitted at the end of the stream.
MessageDeltaPayload
The delta details within a MessageDelta event.
MessageMetadata
Metadata about a message from the API.
MessageStart
The start of a new message from the API.
PartStart
The start of a new part within the response.
StreamAccumulator
Accumulates streaming events into a complete Message.
Usage
Token usage statistics from an API response.

Enums§

DeltaPart
A delta (incremental update) for content within a streaming response.
StreamError
Errors that can occur during stream event processing.
StreamEvent
An event from a streaming LLM API response.
StreamStopReason
Reason why the model stopped generating tokens.