Demonstrates real-time streaming execution in AgentMesh. Shows how to get live updates during graph execution and stream LLM responses token-by-token for better user experience.
- Stream: Real-time event channel for graph execution
- StreamChunk: Token-level updates from LLM model streaming
- Event Types: NodeStart, NodeComplete, NodeError, GraphComplete
- Proper Cleanup: Always call stream.Close() or stream.Cancel()
- Backpressure: Event streams use a fast-path channel with a bounded 100β―ms timeout, so listeners get low-latency updates without spawning thousands of timers when paused.
export OPENAI_API_KEY="sk-..."cd examples/streaming
go run main.go=== Graph Streaming Example ===
[Event] NodeStart: analyze
Superstep: 1
Timestamp: 10:30:15.123
[Streaming] analyze β "Based"
[Streaming] analyze β " on"
[Streaming] analyze β " the"
[Streaming] analyze β " data"
[Streaming] analyze β ","
[Streaming] analyze β " the"
[Streaming] analyze β " trend"
[Streaming] analyze β " is"
[Streaming] analyze β " positive"
[Streaming] analyze β "."
[Event] NodeComplete: analyze
Duration: 1.234s
Output: "Based on the data, the trend is positive."
[Event] NodeStart: summarize
Superstep: 2
[Streaming] summarize β "Summary"
[Streaming] summarize β ":"
[Streaming] summarize β " Positive"
[Streaming] summarize β " trend"
[Event] NodeComplete: summarize
Duration: 0.567s
[Event] GraphComplete
Total Duration: 1.801s
Supersteps: 2
β Execution successful
model := openai.NewModel(client, openai.WithStreaming(true))seq := compiled.Run(ctx, messages)for event, err := range seq {
if err != nil {
// handle error
}
// process event
fmt.Printf("β Starting: %s\n", e.NodeName)
case *graph.NodeCompleteEvent:
fmt.Printf("β Completed: %s (%.2fs)\n",
e.NodeName, e.Duration.Seconds())
case *graph.NodeErrorEvent:
fmt.Printf("β Error in %s: %v\n", e.NodeName, e.Error)
case *graph.GraphCompleteEvent:
fmt.Printf("Graph completed in %.2fs\n", e.Duration.Seconds())
}
}case *graph.NodeCompleteEvent:
if e.StreamChunks != nil {
for chunk := range e.StreamChunks {
fmt.Printf("[Token] %s", chunk.Text)
}
fmt.Println()
}type NodeStartEvent struct {
NodeName string
Superstep int
Timestamp time.Time
}type NodeCompleteEvent struct {
NodeName string
Duration time.Duration
Updates map[string]any
StreamChunks <-chan model.StreamChunk // Token stream
}type NodeErrorEvent struct {
NodeName string
Error error
Retrying bool
}type GraphCompleteEvent struct {
Duration time.Duration
Supersteps int
FinalState map[string]any
}ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
seq := compiled.Run(ctx, messages)- β Real-time execution monitoring
- β Token-by-token LLM streaming
- β Event-driven progress tracking
- β Proper resource cleanup
- β User experience optimization
for event := range stream.Events() {
updateProgressBar(event)
showTokensInRealTime(event)
}for event := range stream.Events() {
logToMetrics(event)
checkTimeout(event)
}for event := range stream.Events() {
if shouldStop(event) {
stream.Cancel()
break
}
}- Implement real-time UI updates
- Add streaming to chat applications
- Combine with checkpointing for long workflows
- See examples/observability for metrics integration
- pkg/graph - Stream API
- pkg/model - Model streaming interface
- examples/observability - Metrics and tracing