Observability¶
Hooks and telemetry sinks. See the Observability guide.
agentflow.observability
¶
Observability: lifecycle hooks and basic run metrics.
Attach a :class:Hooks implementation at compile(hooks=...) to observe a
run without changing its behavior. All callbacks are optional; subclass
:class:Hooks and override only what you need. Hooks are awaited, so they can
do async I/O (emit to a log sink, push a metric), but they must not raise —
an exception in a hook is swallowed so instrumentation can never break a run.
:class:RunMetrics is a ready-made :class:Hooks that records per-node
durations, counts, and errors, plus step and run totals.
Hooks
¶
No-op base for run lifecycle callbacks. Override selectively.
Every method is async and must not raise. Timing is the runtime's concern; hooks receive already-measured values where relevant.
Source code in agentflow/observability.py
RunMetrics
dataclass
¶
Bases: Hooks
A Hooks implementation that accumulates per-node and per-run metrics.
Source code in agentflow/observability.py
NodeStat
dataclass
¶
Aggregated stats for one node across a run.
Source code in agentflow/observability.py
agentflow.telemetry
¶
Persistent telemetry as a :class:~agentflow.observability.Hooks.
Attach at compile(hooks=...) to durably record a run. Two pieces:
- :class:
MultiHookscomposes severalHooksso a run can, say, collect in-memory metrics AND write a JSONL log AND push a custom sink at once. - :class:
JsonlTelemetrywrites one JSON-per-line event stream to a file:run_start,node_start,node_end,node_error,event(backend events surfaced viactx.emit),step,run_end. Each line is a flat object with a UTC timestamp, thread, and step.
Nothing here is required by the engine; it is one more optional listener.
MultiHooks
¶
Bases: Hooks
Fan every lifecycle callback out to several :class:Hooks.
Each child is invoked through the same swallow-exceptions guarantee as a single hook, so one misbehaving listener cannot break the others or the run.
Source code in agentflow/telemetry.py
JsonlTelemetry
¶
Bases: Hooks
Append run events to a JSON Lines file.
Pass a directory to get <dir>/<thread>.jsonl per run thread, or a file
path to write everything to one file. Writes are appended under an async
lock so concurrent nodes in a super-step don't interleave partial lines.
Source code in agentflow/telemetry.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |