Loading...
Loading...
Add OpenTelemetry traces to an AG2 `Agent` via `TelemetryMiddleware` (`ag2.middleware.builtin`). Emits spans for the full turn, each LLM call, each tool execution, and each human-input request, following the OpenTelemetry GenAI semantic conventions. Compatible with any OTLP backend — Jaeger, Grafana Tempo, Datadog, Honeycomb, Langfuse. Use when the user wants production-grade traces, latency analysis, token-usage attribution, or to ship telemetry into an existing observability stack.
npx skill4agent add ag2ai/ag2-skills ag2-telemetryLoggingMiddlewareag2-middlewarepip install "ag2[openai,tracing]"Required. Run this install before delivering the code. If you cannot run commands, state the exactcommand.pip install
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
from ag2 import Agent
from ag2.config import OpenAIConfig
from ag2.middleware.builtin import TelemetryMiddleware
# 1. Configure OpenTelemetry
resource = Resource.create({"service.name": "ag2-quickstart"})
tracer_provider = TracerProvider(resource=resource)
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(tracer_provider)
# 2. Wire the middleware
agent = Agent(
"assistant",
prompt="You are a helpful assistant.",
config=OpenAIConfig(model="gpt-4o-mini"),
middleware=[
TelemetryMiddleware(
tracer_provider=tracer_provider,
agent_name="assistant",
),
],
)
# 3. Run — spans emit automatically
import asyncio
asyncio.run(agent.ask("What is the capital of France?"))ConsoleSpanExporterOTLPSpanExporterSimpleSpanProcessorBatchSpanProcessorask()invoke_agent assistant
├── chat gpt-4o-mini # LLM API call
├── execute_tool get_weather # tool execution
├── chat gpt-4o-mini # LLM call after tool result
└── await_human_input assistant # human-in-the-loopag2.span.type | Operation name | Hook |
|---|---|---|
| | |
| | |
| | |
| | |
| Attribute | Spans | Description |
|---|---|---|
| All | |
| agent, human_input | Agent name |
| agent, llm | Auto-detected ( |
| agent, llm | e.g. |
| llm | Resolved from response |
| llm | e.g. |
| llm | Prompt tokens |
| llm | Completion tokens |
| llm | Prompt-cache writes (Anthropic) |
| llm | Prompt-cache reads (Anthropic, OpenAI, Gemini) |
| tool | Tool function name |
| tool | Tool call ID |
| tool | Always |
TelemetryMiddleware(
tracer_provider=tracer_provider,
agent_name="assistant",
capture_content=False, # omit messages, tool args, results
)| Attribute | Span | Content |
|---|---|---|
| llm | JSON request messages |
| llm | JSON response messages |
| tool | Tool args (JSON) |
| tool | Tool result |
| human_input | Prompt shown to human |
| human_input | Human's response |
capture_content=False| Parameter | Type | Default | Description |
|---|---|---|---|
| | Global provider | OpenTelemetry TracerProvider |
| | | Include message/tool content in spans |
| | | Agent name for span attributes |
| | | Provider override (auto-detected if unset) |
| | | Model override (auto-detected if unset) |
TelemetryMiddlewareOTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")tracing/website/docs/user-guide/telemetry.mdxtracing/ag2-middlewareSimpleSpanProcessorConsoleSpanExporterBatchSpanProcessorcapture_content=Truecapture_content=Falsetrace.set_tracer_provider(...)tracer_providergen_ai.usage.*instrument_agent()instrument_llm_wrapper()instrument_pattern()TelemetryMiddleware