Skip to main content
Streaming gives users feedback as the model thinks. Cognis exposes two streams on every Runnable, with different jobs. If you want to display tokens as they arrive, use stream. If you want to show a tree of “model thinking → calling tool → got result”, use stream_events.

Quick example — token streaming from the LLM

StreamChunk carries the delta (content: String) plus optional usage and finish-reason fields on the last chunk. For tool-using replies, the final chunk holds the assembled tool_calls. Source: examples/v2/04_streaming_chat.rs.

Quick example — structured events

stream_events works on any Runnable — a chain, a model, a graph, an agent.
Full event variants:

Filtered graph streaming

For graphs, Cognis exposes stream_mode, which filters events down to a named subset:
Combine multiple modes with StreamModes::default().push(StreamMode::Updates).push(StreamMode::Messages).

Streaming inside an agent

Same surface — the agent is a Runnable, so stream_events produces a tree of model-token, tool-call, tool-result events nested under the agent’s run.

How it works

  • Streaming uses the same Observer pipe as static observers. The events flow through whatever observers you’ve attached and into the stream.
  • OnLlmToken is emitted by providers that support streaming. For providers that don’t, you’ll see one OnEnd with the full text.
  • Custom is for app-emitted progress. Inside a graph node, call ctx.write_custom("kind", payload) and your UI receives the event.
  • stream_events calls invoke under the hood for non-streaming Runnables. The default emits OnStart + OnEnd only — override on your custom Runnable when you can do better.

See also

Runnables → Streaming events

The lower-level shape.

Graph workflows → Streaming

Graph-specific filters and the Custom channel.

Patterns → Streaming UI

A complete server-side streaming endpoint.