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
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.
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.
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.
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.
node finished its work. Its result is combined with those of the other nodes of its step next.
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.
node paused the run with interrupt. It runs again when the thread is resumed. Its own run follows with onRunInterrupted.
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.
A try of node failed with cause, and the node runs again after pause. See GraphEvent.NodeRetry.
run paused: at GraphConfig.interruptBefore or GraphConfig.interruptAfter, or because a node called interrupt. nextNodes run when the thread is resumed.
run starts, before anything is loaded or saved.