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 → MessageStopPing events may appear at any point in the stream and should be
ignored by consumers.
§Provided Types
StreamEvent— Top-level enum for every SSE event type.StreamAccumulator— Stateful builder that turns events into aMessage.StreamStopReason— Why the model stopped generating tokens.Usage— Token consumption statistics.DeltaPart— Incremental content payload (text, tool JSON, or partial JSON).IndexedDelta— An indexedDeltaPartcarrying the part position.MessageStart/MessageDelta— Boundary events with metadata.
§Sub-modules
handler—handler::StreamHandlerwith 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§
- Indexed
Delta - A delta (incremental update) for the current part.
- Message
Delta - A delta update for the message, typically emitted at the end of the stream.
- Message
Delta Payload - The delta details within a
MessageDeltaevent. - Message
Metadata - Metadata about a message from the API.
- Message
Start - The start of a new message from the API.
- Part
Start - The start of a new part within the response.
- Stream
Accumulator - Accumulates streaming events into a complete
Message. - Usage
- Token usage statistics from an API response.
Enums§
- Delta
Part - A delta (incremental update) for content within a streaming response.
- Stream
Error - Errors that can occur during stream event processing.
- Stream
Event - An event from a streaming LLM API response.
- Stream
Stop Reason - Reason why the model stopped generating tokens.