ag2-telemetry
Skill11 starsChanged 4 months agoApache-2.0View on GitHub
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.
- Installs packages
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-shell-toolSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 18
If they just want quick stdout debugging, point them at LoggingMiddleware instead (see ag2-middleware).
- Very specific: line 80
Every span has an ag2.span.type attribute:
- Distinctive: line 121
When enabled, additional attributes appear:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Telemetry — OpenTelemetry instrumentation
When to use
The user wants to:
- See per-turn / per-call latency breakdowns
- Attribute token usage across operations
- Push traces to Jaeger, Grafana Tempo, Datadog, Honeycomb, Langfuse, etc.
- Debug a slow agent end-to-end with structured spans rather than print statements
If they just want quick stdout debugging, point them at LoggingMiddleware instead (see ag2-middleware).
Installation
pip install "ag2[openai,tracing]"Required. Run this install before delivering the code. If you cannot run commands, state the exact
pip installcommand.
60-second recipe
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?"))For production, swap ConsoleSpanExporter for OTLPSpanExporter (or your backend's exporter) and SimpleSpanProcessor for BatchSpanProcessor.
Span hierarchy
Each ask() produces a root span with children:
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-loopSpan types
Every span has an ag2.span.type attribute:
ag2.span.type | Operation name | Hook |
agent | invoke_agent | on_turn — full turn |
llm | chat | on_llm_call — each LLM call |
tool | execute_tool | on_tool_execution — each tool |
human_input | await_human_input | on_human_input — HITL |
Semantic attributes (GenAI semconv)
Spans carry standard OpenTelemetry GenAI attributes:
| Attribute | Spans | Description |
gen_ai.operation.name | All | invoke_agent / chat / execute_tool / await_human_input |
gen_ai.agent.name | agent, human_input | Agent name |
gen_ai.provider.name | agent, llm | Auto-detected (openai, anthropic, …) |
gen_ai.request.model | agent, llm | e.g. gpt-4o-mini |
gen_ai.response.model | llm | Resolved from response |
gen_ai.response.finish_reasons | llm | e.g. ["stop"], ["tool_calls"] |
gen_ai.usage.input_tokens | llm | Prompt tokens |
gen_ai.usage.output_tokens | llm | Completion tokens |
gen_ai.usage.cache_creation_input_tokens | llm | Prompt-cache writes (Anthropic) |
gen_ai.usage.cache_read_input_tokens | llm | Prompt-cache reads (Anthropic, OpenAI, Gemini) |
gen_ai.tool.name | tool | Tool function name |
gen_ai.tool.call.id | tool | Tool call ID |
gen_ai.tool.type | tool | Always function |
Content capture (default ON)
By default, message content, tool args, and results are included on spans. Useful for debugging but can leak sensitive data:
TelemetryMiddleware(
tracer_provider=tracer_provider,
agent_name="assistant",
capture_content=False, # omit messages, tool args, results
)When enabled, additional attributes appear:
| Attribute | Span | Content |
gen_ai.input.messages | llm | JSON request messages |
gen_ai.output.messages | llm | JSON response messages |
gen_ai.tool.call.arguments | tool | Tool args (JSON) |
gen_ai.tool.call.result | tool | Tool result |
ag2.human_input.prompt | human_input | Prompt shown to human |
ag2.human_input.response | human_input | Human's response |
For privacy-sensitive backends (or anywhere telemetry leaves your infra), set capture_content=False.
Constructor reference
| Parameter | Type | Default | Description |
tracer_provider | TracerProvider | None | Global provider | OpenTelemetry TracerProvider |
capture_content | bool | True | Include message/tool content in spans |
agent_name | str | None | "unknown" | Agent name for span attributes |
provider_name | str | None | None | Provider override (auto-detected if unset) |
model_name | str | None | None | Model override (auto-detected if unset) |
Backend integration
TelemetryMiddleware uses standard OpenTelemetry, so any OTLP-compatible backend works:
- Jaeger —
OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces") - Grafana Tempo — same OTLP exporter, point at the Tempo gateway
- Langfuse, Honeycomb, Datadog — vendor-specific exporters; the agent-side setup is identical
For container-orchestrated stacks, this repo includes a tracing/ directory with Docker-Compose for otel-collector + Grafana Tempo.
Going deeper
website/docs/user-guide/telemetry.mdx— full attribute table, configuration, example.tracing/— Docker setup for local otel-collector + Tempo + Grafana.- For sibling middleware (logging, retry, history limits), see
ag2-middleware.
Common pitfalls
SimpleSpanProcessor+ConsoleSpanExporterin production — synchronous, blocks every span emit. UseBatchSpanProcessorand a real exporter (OTLP / Jaeger / vendor) outside of dev.- Leaking content into telemetry —
capture_content=Trueis the default. For privacy-sensitive prompts (PII, credentials), setcapture_content=Falseand audit what your backend retains. - Forgetting
trace.set_tracer_provider(...)— without it,tracer_provideryou pass to the middleware is fine, but third-party libraries that auto-instrument may use a different provider. - Token usage missing —
gen_ai.usage.*requires the provider client to surface usage in the response. Streaming providers may emit usage only at the end; if you don't see them, check the provider's response shape. - Span hierarchy doesn't show parent-child — your exporter or backend may need the OTLP/HTTP path enabled, not just OTLP/gRPC. Check both.
- Comparing to AG2 Classic tracing docs — the semantic-attribute format is the same; only the agent instrumentation method differs (AG2 Classic uses
instrument_agent()/instrument_llm_wrapper()/instrument_pattern(); AG2 usesTelemetryMiddleware).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

