Skip to main content

Basic Cost Tracking

Attach a CostTracker to an agent to automatically record token usage and dollar costs for every LLM call.

Per-Model Pricing

Provide custom pricing tables for models not yet in the built-in registry, or override defaults.

Budget Enforcement

Set hard budget limits per run, per session, or per user. The tracker throws (or warns) when a budget is exceeded.

Cost Auto-Stop per Roundtrip

Use loopHooks.onRoundtripComplete to check the budget after every tool-calling roundtrip and stop early if the limit is close.

Shared Cost Tracker

Use a single CostTracker across multiple agent types to get a unified cost view.

Cost Breakdown

View granular cost categories: input, output, reasoning, cached, audio input, and audio output.

Token Usage Details

Access the full token usage breakdown including reasoning and audio tokens, plus the raw provider metrics.

Raw Provider Metrics

Access the full, unmodified usage data returned by the underlying model API. This is useful for debugging token count discrepancies or accessing provider-specific fields.

Tracer Setup

Attach a Tracer to the agent’s event bus to collect distributed traces with spans for runs, tool calls, handoffs, and more.

Custom Trace Exporter

Export traces to any backend by implementing the TraceExporter interface.

MetricsCollector

Collect real-time counters, histograms, and gauges from agent events.

Prometheus Export

Expose agent metrics in Prometheus text format via the MetricsExporter.

Structured Logging

Attach a StructuredLogger for JSON or console-formatted log output correlated with trace IDs.

Full Observability Stack

Combine Tracer, MetricsCollector, MetricsExporter, and StructuredLogger for a production-grade observability setup.