Skip to main content

Observability

@agentium/observability is a separate, opt-in package that adds tracing, metrics, and structured logging to any Agentium agent. It listens to the agent’s EventBus from the outside — zero changes to core, zero overhead when not installed.

Quick Start

Exporter Shorthands

Pass exporter names as strings — credentials are read from env vars automatically:
You can also mix shorthands with custom instances when you need to override defaults:

How It Works

The instrument() function attaches three listeners to the agent’s EventBus:
  1. Tracer — builds a span tree from events (run.starttool.calltool.resultrun.complete)
  2. MetricsCollector — counts runs, tool calls, errors, cache hits, and tracks latency histograms
  3. StructuredLogger — emits JSON log entries correlated with trace IDs
Since core already emits rich events for every operation, observability works automatically with all features: handoffs, teams, cost tracking, caching, tools, etc.

Provider Metrics in Traces & Logs

When a run completes, the run.complete event includes providerMetrics — the raw usage object from the underlying model API. This is automatically captured by:
  • Tracer — stored as a span attribute (providerMetrics) on the root run span
  • StructuredLogger — included in the JSON log payload for run.complete events
  • MetricsExporter — stored in the RunRecord for export and dashboard consumption
  • LangfuseExporter — forwarded as generation metadata in Langfuse
This means you get full provider-level transparency (e.g., thoughtsTokenCount, prompt_tokens_details, cache_read_input_tokens) in your observability pipeline without any extra configuration.

Trace Tree

Every agent.run() produces a trace like:

Exporters

Plus CallbackExporter for custom integrations:

Metrics

Structured Logging

Three drain modes:
Each entry includes traceId for correlation with traces.

Works With Teams & Workflows

Use instrumentBus() to attach to any EventBus:

Langfuse Integration Example

Langfuse provides an open-source LLM observability dashboard. Set up in 3 steps:
In the Langfuse dashboard, you’ll see:
  • Traces for each agent.run() with duration, token usage, and cost
  • Generations for each LLM call within a run
  • Spans for tool calls, handoffs, and other operations
  • Sessions grouping traces by sessionId

OpenTelemetry Export

Send traces to any OTLP-compatible backend (Jaeger, Grafana Tempo, Honeycomb, etc.):
Traces follow the OpenTelemetry semantic conventions for GenAI, making them compatible with standard OTLP tooling.

Building a Custom Dashboard

Combine metrics and events to build a real-time dashboard:

Capacity Metrics

When the Session Profiler is attached to the same EventBus, MetricsExporter automatically captures capacity-related metrics:

AgentMetrics fields

Prometheus output

The toPrometheus() method includes three new capacity counters:
These appear automatically when capacity.session.classified and capacity.warning events are emitted on the EventBus — no additional configuration needed.

Accuracy Metrics (v2.4+)

MetricsExporter tracks how often agent output needs human correction, and the agent’s self-critique quality when Reflection is enabled.

AgentMetrics fields

How they’re captured

  • correctionsTotal / correctionRate increment on every memory.correction.recorded event — emitted whenever a correction is recorded via the corrections endpoint, agent.memory.recordCorrection(), or the record_correction tool. See Correction Capture.
  • avgCritiqueScore aggregates reflection.critique events emitted during runs.

Prometheus output

A falling correction_rate over time is the clearest signal that the correction-capture learning loop is working — each recorded correction is retrieved on future relevant runs, so the same mistake stops recurring.