teff.trace¶
teff.trace
¶
Run tracing and telemetry for graph workflows.
Constitution Principle IX: observability is mandatory. RunTracer
collects a structured, JSON-serialisable event log for a single
graph.run() call — timeline, per-node latency, retries, checkpoint
activity, and LLM token usage — and folds it into a RunSummary.
Classes:
| Name | Description |
|---|---|
NodeStats |
Aggregated per-node statistics for a run. |
RunSummary |
Folded summary computed from a run's trace events. |
RunTracer |
Collects trace events during a |
TokenUsage |
Accumulated LLM token counts for a run. |
TraceEvent |
A single observability event emitted during a graph run. |
Functions:
| Name | Description |
|---|---|
clear_pricing |
Remove all custom pricing registered at runtime. |
load_pricing |
Register pricing from a YAML/JSON file path or an inline dict. |
model_pricing |
Return |
set_model_pricing |
Register custom USD-per-1M-token pricing for a provider/model pair. |
set_provider_pricing |
Register a provider-wide default price in USD per 1M tokens. |
tokens_cost |
Estimate the USD cost of a model call from its token usage. |
NodeStats
dataclass
¶
Aggregated per-node statistics for a run.
Source code in teff/trace.py
199 200 201 202 203 204 205 | |
RunSummary
dataclass
¶
Folded summary computed from a run's trace events.
Methods:
| Name | Description |
|---|---|
to_dict |
Return a JSON-serialisable dict (model names redacted). |
to_json |
Return this summary as a pretty-printed JSON string. |
Source code in teff/trace.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
to_dict
¶
to_dict()
Return a JSON-serialisable dict (model names redacted).
Source code in teff/trace.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
to_json
¶
to_json()
Return this summary as a pretty-printed JSON string.
Source code in teff/trace.py
245 246 247 | |
RunTracer
¶
Collects trace events during a graph.run() call.
Pass an instance to graph.run(tracer=...). After the run,
inspect events for the raw timeline, timeline() for a
JSON-serialisable list, summary() for aggregated statistics, or
to_json() for a ready-to-persist report.
Events are also emitted for the node-level hooks (start/end/error) plus edge routing, checkpoint saves/loads, retries, and LLM calls.
Methods:
| Name | Description |
|---|---|
checkpoint |
Record a checkpoint |
edge |
Record a routing decision from source_id to target_id. |
interrupt |
Record that an |
interrupt_resume |
Record that a paused run resumed with answers for keys. |
llm |
Record an LLM call and accumulate its token usage. |
node_end |
Record the successful completion of a node. |
node_error |
Record a node failure. |
node_start |
Record the start of a node execution. |
retry |
Record a retry attempt (1-based attempt number). |
run_end |
Record the end of a run ( |
run_start |
Record the beginning of a run. |
structured |
Record a structured-output validation failure (1-based attempt). |
summary |
Fold all events into an aggregated :class: |
timeline |
Return the raw event log as JSON-serialisable dicts. |
to_json |
Return a JSON report: |
Source code in teff/trace.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
checkpoint
¶
checkpoint(action, checkpoint_id, next_node_id)
Record a checkpoint save or load.
Source code in teff/trace.py
368 369 370 371 372 373 374 375 376 377 378 379 380 | |
edge
¶
edge(source_id, target_id, condition=None)
Record a routing decision from source_id to target_id.
Source code in teff/trace.py
357 358 359 360 361 362 363 364 365 366 | |
interrupt
¶
interrupt(node_id, key, prompt)
Record that an Interrupt node paused the run for input.
Source code in teff/trace.py
435 436 437 438 439 440 441 442 | |
interrupt_resume
¶
interrupt_resume(node_id, keys)
Record that a paused run resumed with answers for keys.
Source code in teff/trace.py
444 445 446 | |
llm
¶
llm(provider, model, prompt_tokens, completion_tokens, duration_ms)
Record an LLM call and accumulate its token usage.
Source code in teff/trace.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
node_end
¶
node_end(node_id, node_type, duration_ms)
Record the successful completion of a node.
Source code in teff/trace.py
332 333 334 335 336 337 338 339 | |
node_error
¶
node_error(node_id, node_type, duration_ms, error)
Record a node failure.
Source code in teff/trace.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
node_start
¶
node_start(node_id, node_type)
Record the start of a node execution.
Source code in teff/trace.py
328 329 330 | |
retry
¶
retry(node_id, node_type, attempt, error)
Record a retry attempt (1-based attempt number).
Source code in teff/trace.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
run_end
¶
run_end(status, total_ms, error=None)
Record the end of a run (status in {"ok", "error"}).
Source code in teff/trace.py
448 449 450 451 452 453 454 455 456 457 458 | |
run_start
¶
run_start(checkpoint_id=None)
Record the beginning of a run.
Source code in teff/trace.py
324 325 326 | |
structured
¶
structured(node_id, node_type, errors, attempt)
Record a structured-output validation failure (1-based attempt).
Source code in teff/trace.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
summary
¶
summary()
Fold all events into an aggregated :class:RunSummary.
Source code in teff/trace.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
timeline
¶
timeline()
Return the raw event log as JSON-serialisable dicts.
Source code in teff/trace.py
460 461 462 | |
to_json
¶
to_json()
Return a JSON report: {summary, events} (secrets redacted).
Source code in teff/trace.py
508 509 510 511 512 513 | |
TokenUsage
dataclass
¶
Accumulated LLM token counts for a run.
Source code in teff/trace.py
187 188 189 190 191 192 193 194 195 196 | |
TraceEvent
dataclass
¶
A single observability event emitted during a graph run.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
Event type — |
timestamp |
float
|
Seconds since the tracer started (monotonic). |
node_id |
str | None
|
Graph node id the event belongs to, if any. |
node_type |
str | None
|
Node type string, if any. |
duration_ms |
float | None
|
Node/LLM call duration in milliseconds, if measured. |
data |
dict[str, Any]
|
Kind-specific payload (error, condition, tokens, etc.). |
Methods:
| Name | Description |
|---|---|
to_dict |
Return a JSON-serialisable dict for this event (data redacted). |
Source code in teff/trace.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
to_dict
¶
to_dict()
Return a JSON-serialisable dict for this event (data redacted).
Source code in teff/trace.py
273 274 275 276 277 278 279 280 281 282 283 284 | |
clear_pricing
¶
clear_pricing()
Remove all custom pricing registered at runtime.
Source code in teff/trace.py
130 131 132 133 | |
load_pricing
¶
load_pricing(source)
Register pricing from a YAML/JSON file path or an inline dict.
Format::
providers:
openrouter:
default: {input: 0.1, output: 0.4}
models:
"openai/gpt-4o": {input: 3.0, output: 12.0}
"anthropic/claude-3.5-sonnet": {input: 3.0, output: 15.0}
A flat dict {"provider": {model: [in, out]}} is also accepted.
Source code in teff/trace.py
85 86 87 88 89 90 91 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 | |
model_pricing
¶
model_pricing(model, provider='')
Return (input, output) USD per 1M tokens for model / provider.
Resolution order:
- exact
(provider, model)custom entry; - provider-prefixed custom entry (
gpt-4omatchesgpt-4o-2024-08-06); - provider-wide default;
- built-in table (exact, then prefix);
(0.0, 0.0)for unknown/local models.
When provider is empty only the built-in table is consulted, so callers that pass no provider keep their current behaviour.
Source code in teff/trace.py
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 | |
set_model_pricing
¶
set_model_pricing(provider, model, input_price, output_price)
Register custom USD-per-1M-token pricing for a provider/model pair.
Takes precedence over the built-in table and any provider-wide default.
Source code in teff/trace.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
set_provider_pricing
¶
set_provider_pricing(provider, input_price, output_price)
Register a provider-wide default price in USD per 1M tokens.
Applied to every model on provider that has no per-model entry.
Source code in teff/trace.py
73 74 75 76 77 78 79 80 81 82 | |
tokens_cost
¶
tokens_cost(model, prompt_tokens, completion_tokens, provider='')
Estimate the USD cost of a model call from its token usage.
Source code in teff/trace.py
168 169 170 171 172 173 174 175 176 177 178 179 | |