cetra traces AG2 agent runs through AG2’s telemetry middleware: agent, model call and tool spans.
Install
Section titled “Install”pip install cetra-sdk "ag2[openai,tracing]"Set up
Section titled “Set up”AG2’s telemetry middleware takes a tracer provider. Pass it cetra’s tracer provider. Set CETRA_API_KEY to your sk-cetra-… key and OPENAI_API_KEY to your OpenAI key.
import asyncioimport os
import cetrafrom ag2 import Agent, toolfrom ag2.config.openai import OpenAIConfigfrom ag2.middleware.builtin.telemetry import TelemetryMiddleware
cetra.init()
@tooldef get_weather(city: str) -> str: """Get the weather.""" return f"sunny in {city}"
config = OpenAIConfig(model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])middleware = [ TelemetryMiddleware( tracer_provider=cetra.tracer_provider(), agent_name="weather_agent", provider_name="openai", model_name="gpt-4o-mini", )]agent = Agent( "weather_agent", prompt="You are a weather bot.", config=config, tools=[get_weather], middleware=middleware,)
async def run() -> None: reply = await agent.ask("What is the weather in Paris?") await reply.content()
asyncio.run(run())
cetra.flush()What you get
Section titled “What you get”With one tool call, a run produces:
- an
agentspan namedinvoke_agent weather_agent, with the provider and the model; the fixture has no input or output on this span; - an
llmspan for each model call, with the provideropenai, the model, the token counts, the cost computed from the built-in prices, and the messages; - a
toolspan for each tool call, with the arguments as its input and the result as its output; - a plain span named
record_usage model_callafter each model call. It repeats that call’s token counts.
Limits
Section titled “Limits”- The
record_usagespans are plain spans, notllmspans, so the tokens are counted once, on the model call. - The fixture was captured with
ag21.3.2 and the OpenTelemetry SDK 1.45.2. - If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.
- Online rules: score these traces automatically.