GraphObserver

interface GraphObserver

Watches the runs of a graph: when a run starts and how it ends, when each node starts and how it ends, when the condition of an edge decides where the run goes, which nodes come next after a step, and when a checkpoint is saved. Use it to send traces and metrics to the tools a team already has, such as OpenTelemetry with telar-otel. Give it to a run with GraphConfig.observers:

val timer = object : GraphObserver {
override fun onNodeCompleted(node: ObservedNode) = println("${node.name} done in step ${node.step}")
}
graph.invoke(order, GraphConfig(observers = listOf(timer)))

Every function does nothing unless it is overridden, so an observer overrides only what it needs. An observer is told about every run of the config: CompiledGraph.invoke and CompiledGraph.stream, CompiledGraph.resume, CompiledGraph.fork, and the runs of the subgraphs inside them.

What an observer gets:

  • No state. The state can hold personal data, so an observer only learns names, steps, timing and failures. onNodeProgress is the one exception: it gets what a node reports.

  • Every start has one end. A run that started ends with exactly one of onRunCompleted, onRunInterrupted and onRunFailed, and a node that started ends with exactly one of onNodeCompleted, onNodeInterrupted and onNodeFailed, also when the run is cancelled. An edge that started ends with exactly one of onEdgeCompleted and onEdgeFailed.

  • Calls from several coroutines. The nodes of a step run in parallel and call the observer at the same time, possibly on different threads. An observer that keeps data must be safe for that.

The functions are not suspend: they are called in the middle of the engine and must return quickly. Hand slow work, such as sending data over the network, to a queue of your own. What an observer throws fails the run, so an observer must not throw.

Functions

Link copied to clipboard

Returns what to add to the coroutine context in which the condition of edge runs, like nodeContext for a node. Called once for each edge, after onEdgeStarted.

Link copied to clipboard

Returns what to add to the coroutine context in which node runs, for example the trace span of the node, so that what the node calls is recorded inside it. Called once for each node in each step, after onNodeStarted.

Link copied to clipboard
open fun onCheckpointSaved(run: ObservedRun, step: Int, nextNodes: List<String>)

The checkpointer of run saved a checkpoint after step step (0 when the run starts), with nextNodes to run next. A subgraph has no checkpointer of its own: the graph around it saves.

Link copied to clipboard
open fun onEdgeCompleted(edge: ObservedEdge, targets: List<String>)

The condition of edge chose targets, END among them when it ends a branch.

Link copied to clipboard
open fun onEdgeFailed(edge: ObservedEdge, cause: Throwable)

The condition of edge failed with cause: an EdgeConditionException when it threw, an InvalidRouteException when it chose a node that it may not go to, or a CancellationException when the run was cancelled.

Link copied to clipboard
open fun onEdgeStarted(edge: ObservedEdge)

The condition of edge starts, on the state that its step left. Only an edge with a condition is reported: conditionalEdge, conditionalFanOut and the edges built on them, such as decisionEdge. The conditions of a step run one after the other, after every node of the step ended and before onStepCompleted.

Link copied to clipboard

node finished its work. Its result is combined with those of the other nodes of its step next.

Link copied to clipboard
open fun onNodeFailed(node: ObservedNode, cause: Throwable)

node failed with cause: a NodeExecutionException when its last try failed, or a CancellationException when it was cancelled, because another node of its step failed or paused the run, or because the run was cancelled.

Link copied to clipboard

node paused the run with interrupt. It runs again when the thread is resumed. Its own run follows with onRunInterrupted.

Link copied to clipboard
open fun onNodeProgress(node: ObservedNode, value: Any)

node reported value with reportProgress, such as a piece of the text a model writes. Only a run that is collected with CompiledGraph.stream, CompiledGraph.streamResume or CompiledGraph.streamFork has progress: an observer does not turn it on.

Link copied to clipboard
open fun onNodeRetry(node: ObservedNode, attempt: Int, cause: Throwable, pause: Duration)

A try of node failed with cause, and the node runs again after pause. See GraphEvent.NodeRetry.

Link copied to clipboard
open fun onNodeStarted(node: ObservedNode)

node starts. A node with a Retry starts once, however often it is tried.

Link copied to clipboard

run reached END on every branch.

Link copied to clipboard
open fun onRunFailed(run: ObservedRun, cause: Throwable)

run failed with cause, the exception that the caller of the run gets, or a CancellationException when the run was cancelled.

Link copied to clipboard
open fun onRunInterrupted(run: ObservedRun, nextNodes: List<String>)

run paused: at GraphConfig.interruptBefore or GraphConfig.interruptAfter, or because a node called interrupt. nextNodes run when the thread is resumed.

Link copied to clipboard
open fun onRunStarted(run: ObservedRun)

run starts, before anything is loaded or saved.

Link copied to clipboard
open fun onStepCompleted(run: ObservedRun, step: Int, nodes: List<String>, nextNodes: List<String>)

Step step of run finished: nodes ran, their results were combined and the edges chose nextNodes. An empty nextNodes means the run completes.