org.llm4s.agent.streaming

Streaming event support for agent execution.

This package provides fine-grained visibility into agent execution through an event-based streaming API. Events are emitted in real-time during agent execution, enabling:

  • '''Real-time UIs''': Stream LLM tokens to users as they're generated
  • '''Progress tracking''': Monitor multi-step agent execution
  • '''Tool visibility''': See which tools are being invoked and their results
  • '''Debugging''': Trace agent behavior with timestamped events

==Event Categories==

'''Text Events''': Token-level streaming during LLM generation

'''Tool Events''': Tool invocation lifecycle

'''Agent Lifecycle''': Start, step, complete, fail

'''Handoff Events''': Agent-to-agent delegation

==Usage==

import org.llm4s.agent.Agent
import org.llm4s.agent.streaming.AgentEvent._

agent.runWithEvents(
 query = "What's the weather in London?",
 tools = weatherTools,
 onEvent = {
   case TextDelta(delta, _) =>
     print(delta)  // Stream to console

   case ToolCallStarted(_, name, args, _) =>
     println(s"\n[Calling $$name with $$args]")

   case ToolCallCompleted(_, name, result, _, _, _) =>
     println(s"[Tool $$name returned: $$result]")

   case AgentCompleted(state, steps, duration, _) =>
     println(s"\n[Done in $$steps steps, $${duration}ms]")

   case AgentFailed(error, _, _) =>
     println(s"\n[Error: $$error]")

   case _ => // Ignore other events
 }
)

==Collecting Events==

To collect all events for later processing:

val events = scala.collection.mutable.ArrayBuffer[AgentEvent]()

agent.runWithEvents(
 query = "...",
 tools = tools,
 onEvent = events += _
)

// Analyze events after execution
val toolCalls = events.collect { case tc: ToolCallStarted => tc }
val totalTextLength = events.collect { case TextDelta(d, _) => d.length }.sum

Attributes

See also

Agent.runWithEvents for the streaming API

AgentEvent for the event type hierarchy

Members list

Type members

Classlikes

sealed trait AgentEvent

Events emitted during streaming agent execution.

Events emitted during streaming agent execution.

AgentEvent provides fine-grained visibility into agent execution, enabling real-time UIs, progress tracking, and debugging.

Event categories:

  • Text events: Token-level streaming during LLM generation
  • Tool events: Tool invocation lifecycle
  • Agent lifecycle: Start, step, complete, fail
  • Handoff events: Agent-to-agent delegation

Attributes

Example
agent.runWithEvents(
 query = "What's the weather?",
 tools = weatherTools,
 onEvent = {
   case TextDelta(delta, _) => print(delta)
   case ToolCallStarted(_, name, _, _) => println(s"[Calling $$name]")
   case AgentCompleted(state, _) => println("Done!")
   case _ => // ignore
 }
)
Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object AgentEvent

Attributes

Companion
trait
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
AgentEvent.type