Streaming gives users feedback as the model thinks. Cognis exposes two streams on every Runnable, with different jobs.Documentation Index
Fetch the complete documentation index at: https://cognis.vasanth.xyz/llms.txt
Use this file to discover all available pages before exploring further.
| Stream | Item | Use for |
|---|---|---|
stream(input, cfg) | O (chunks of the output) | Token-by-token UI, progressive rendering. |
stream_events(input, cfg) | Event | Trace UIs, progress bars, observability pipelines. |
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.
| Variant | Fields |
|---|---|
OnStart / OnEnd | runnable, run_id, input/output |
OnError | error, run_id |
OnNodeStart / OnNodeEnd | node, step, run_id, output? |
OnLlmToken | token, run_id |
OnToolStart / OnToolEnd | tool, args/result, run_id |
OnCheckpoint | step, run_id |
Custom | kind, payload, run_id (user-emitted from a graph node) |
Filtered graph streaming
For graphs, Cognis exposesstream_mode, which filters events down to a named subset:
StreamMode | What it emits |
|---|---|
Values | Whole state at the end (OnEnd). |
Updates | Per-node deltas (OnNodeEnd). |
Messages | LLM tokens, tool starts/ends. |
Tasks | Node-start signals. |
Checkpoints | Each persisted snapshot. |
Debug | Everything. |
Custom | Only Event::Custom payloads (emitted via NodeCtx::write_custom). |
StreamModes::default().push(StreamMode::Updates).push(StreamMode::Messages).
Streaming inside an agent
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
Observerpipe as static observers. The events flow through whatever observers you’ve attached and into the stream. OnLlmTokenis emitted by providers that support streaming. For providers that don’t, you’ll see oneOnEndwith the full text.Customis for app-emitted progress. Inside a graph node, callctx.write_custom("kind", payload)and your UI receives the event.stream_eventscallsinvokeunder the hood for non-streaming Runnables. The default emitsOnStart+OnEndonly — 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.