|
| 1 | +Agent Observability (GenAI OpenTelemetry Spans) |
| 2 | +=============================================== |
| 3 | + |
| 4 | +When automation drives an LLM agent, you want the same observability an |
| 5 | +OpenTelemetry backend gives a service: per-operation spans carrying token usage, |
| 6 | +model, and status. ``AgentTrace`` records spans whose attributes follow the |
| 7 | +OpenTelemetry **GenAI semantic conventions** — ``gen_ai.operation.name``, |
| 8 | +``gen_ai.system``, ``gen_ai.request.model``, ``gen_ai.usage.input_tokens`` / |
| 9 | +``gen_ai.usage.output_tokens``, ``gen_ai.tool.name`` — and the convention span |
| 10 | +name ``"{operation} {model}"``. :meth:`AgentTrace.to_otel` output drops straight |
| 11 | +into an OTLP exporter, while :meth:`AgentTrace.summary` rolls up cost and latency |
| 12 | +for a run. |
| 13 | + |
| 14 | +It pairs with :doc:`trajectory evaluation <v36_features_doc>` — record the run |
| 15 | +here, score it there. Pure standard library (no ``opentelemetry`` dependency); |
| 16 | +the clock is injectable so durations are deterministically testable. Imports no |
| 17 | +``PySide6``. |
| 18 | + |
| 19 | +Headless API |
| 20 | +------------ |
| 21 | + |
| 22 | +.. code-block:: python |
| 23 | +
|
| 24 | + from je_auto_control import AgentTrace |
| 25 | +
|
| 26 | + trace = AgentTrace() |
| 27 | + # one-shot record of a completed call: |
| 28 | + trace.record("chat", model="claude-opus-4-8", system="anthropic", |
| 29 | + input_tokens=1200, output_tokens=180, duration_s=0.9) |
| 30 | +
|
| 31 | + # or time a live block; set token counts on the yielded dict: |
| 32 | + with trace.operation("tool", tool_name="search") as fields: |
| 33 | + result = run_tool() |
| 34 | + fields["output_tokens"] = 42 # error inside marks the span error |
| 35 | +
|
| 36 | + print(trace.summary()) # {span_count, error_count, input_tokens, ...} |
| 37 | + exporter.export(trace.to_otel()) # OTLP-friendly span dicts |
| 38 | +
|
| 39 | +``summary`` aggregates ``span_count``, ``error_count``, ``input_tokens``, |
| 40 | +``output_tokens``, and total ``duration_s``. ``to_otel`` returns each span as |
| 41 | +``{name, kind, attributes, duration_s, status:{code}}`` with an OTel status code. |
| 42 | + |
| 43 | +Executor commands |
| 44 | +----------------- |
| 45 | + |
| 46 | +A module-level default trace backs the executor/MCP surfaces so a flow can build |
| 47 | +a trace across steps: |
| 48 | + |
| 49 | +================================ =================================================== |
| 50 | +Command Effect |
| 51 | +================================ =================================================== |
| 52 | +``AC_trace_record`` Record a GenAI span (operation/model/tokens/…). |
| 53 | +``AC_trace_summary`` Roll up the default trace. |
| 54 | +``AC_trace_export`` Export the default trace as OTLP spans. |
| 55 | +``AC_trace_reset`` Clear the default trace. |
| 56 | +================================ =================================================== |
| 57 | + |
| 58 | +The same operations are exposed as MCP tools (``ac_trace_record`` / |
| 59 | +``ac_trace_summary`` / ``ac_trace_export`` / ``ac_trace_reset``) and as Script |
| 60 | +Builder commands under **Agent**. |
0 commit comments