agentleFS
ag2aiag2ai/ag2-skills/skills/ag2-telemetry/SKILL.md

ag2-telemetry

Skill12 starsChanged 9 days agoApache-2.0View on GitHub

Add OpenTelemetry traces to an AG2 `Agent` with `TelemetryMiddleware` (`ag2.middleware`, extra `ag2[tracing]`). Emits GenAI-semconv spans — `invoke_agent` (turn), `chat` (LLM call), `execute_tool`, `await_human_input`, plus `record_usage` spans for every `UsageEvent` (sub-agents, compaction) — with `capture_content`, `max_tool_result_chars`, `span_attributes` (tenant/env tags). Sub-agent turns nest under the parent's `execute_tool task_<worker>` span. Works with any OTLP backend (Jaeger, Tempo, Datadog, Honeycomb, Langfuse, Arize). Use for production traces, latency analysis, token attribution. For Prometheus metrics use `MetricsMiddleware` (`ag2-middleware`); for hub/network spans see `HubTelemetryListener` (`ag2-network-quickstart`).

  • Installs packages

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add TelemetryMiddleware to each worker you want detailed spans for.

    line 69

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Telemetry — OpenTelemetry instrumentation

When to use

  • Per-turn / per-call latency breakdowns, token attribution, end-to-end debugging with spans.
  • Shipping traces to Jaeger, Grafana Tempo, Datadog, Honeycomb, Langfuse, Arize, etc.

Quick stdout debugging → LoggingMiddleware; dashboards/alerting on rates → MetricsMiddleware (both in ag2-middleware).

Installation

pip install "ag2[openai,tracing]"          # + opentelemetry-exporter-otlp for OTLP export

Required. Run this install before delivering the code. If you cannot run commands, state the exact pip install command.

Recipe

import asyncio

from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

from ag2 import Agent
from ag2.config import OpenAIResponsesConfig
from ag2.middleware import TelemetryMiddleware

provider = TracerProvider(resource=Resource.create({"service.name": "ag2-app"}))
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

agent = Agent(
    "assistant",
    prompt="You are a helpful assistant.",
    config=OpenAIResponsesConfig(model="gpt-4.1-mini"),
    middleware=[TelemetryMiddleware(tracer_provider=provider, agent_name="assistant")],
)

asyncio.run(agent.ask("What is the capital of France?"))

Production: BatchSpanProcessor(OTLPSpanExporter(endpoint="http://collector:4318/v1/traces")) (from opentelemetry.exporter.otlp.proto.http.trace_exporter).

Span tree

invoke_agent assistant                         ag2.span.type=agent
  ├── chat <model>                             llm
  ├── record_usage model_call                  usage   (one per UsageEvent)
  ├── execute_tool task_worker                 tool    (a delegate call)
  │     └── invoke_agent worker                agent   (if the worker has its own TelemetryMiddleware)
  │           ├── chat <model> / execute_tool get_weather / record_usage ...
  ├── chat <model>
  └── await_human_input assistant              human_input
  • Add TelemetryMiddleware to each worker you want detailed spans for; the parent still records the workers' UsageEvents as record_usage spans even if they are uninstrumented.
  • Token totals: chat spans and record_usage spans both carry gen_ai.usage.*, and an instrumented worker's usage appears under both the worker and the parent. When aggregating, sum only record_usage spans and dedupe by ag2.usage.event_id.
  • A turn triggered by a network envelope links to the hub's span (see network telemetry upstream).

Key attributes

AttributeSpans
ag2.span.typeall: agent / llm / tool / human_input / usage
gen_ai.operation.name, gen_ai.agent.nameall / agent, human_input
gen_ai.provider.name, gen_ai.request.model, gen_ai.response.model, gen_ai.response.finish_reasonsagent, llm
gen_ai.usage.input_tokens / output_tokens / cache_creation_input_tokens / cache_read_input_tokens / thinking_tokensllm, usage
ag2.usage.event_id, ag2.usage.kind (model_call, compaction, aggregation, ...), ag2.usage.total_tokensusage
gen_ai.tool.name, gen_ai.tool.call.id, gen_ai.tool.typetool

With capture_content=True (default) also: gen_ai.input.messages (full assembled history, OpenAI-style JSON), gen_ai.output.messages (text and/or tool calls), gen_ai.tool.call.arguments, gen_ai.tool.call.result (+ ag2.tool.call.result.truncated), ag2.human_input.prompt / .response.

Constructor

ParameterDefaultNotes
tracer_providerglobal provider
capture_contentTrueset False for PII / external backends
max_tool_result_chars8192longer results end with ...[truncated]; None disables
agent_name"unknown"set it — it names the spans
provider_name, model_nameauto-detected
span_attributesNone{"deployment": "prod", "tenant": "acme"} stamped on every span; intrinsic keys win on collision

describe() reports only the keys of span_attributes.

Anthropic SDK spans

The anthropic SDK adds its own anthropic.messages.create span under AG2's chat span when a global provider is set. Disable with AnthropicConfig(model="claude-haiku-4-5", open_telemetry=False).

Going deeper

  • Upstream: website/docs/user-guide/telemetry/agent.mdx, telemetry/network.mdx (Hub.open(store, tracer_provider=...) + HubTelemetryListener), metrics.mdx.
  • Eval runs can reconstruct traces from these spans — see ag2-evaluation.

Common pitfalls

  • Missing extra — without ag2[tracing], using TelemetryMiddleware raises a missing-dependency error.
  • SimpleSpanProcessor + console in production — synchronous; use BatchSpanProcessor + a real exporter.
  • Leaking content — capture_content=True is the default; history, tool args and human input go to the backend.
  • Double-counting tokens — see "Token totals" above.
  • Passing the class — TelemetryMiddleware is a factory: pass an instance (TelemetryMiddleware(...)).
  • One TelemetryMiddleware shared across agents with one agent_name — spans all get that name; create one per agent.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.