AutoGen
cetra traces AutoGen AgentChat runs through the OpenInference AutoGen instrumentor: agent, model call and tool spans.
Install
Section titled “Install”pip install cetra-sdk openinference-instrumentation-autogen-agentchat autogen-agentchat "autogen-ext[openai]"Set up
Section titled “Set up”Turn on the AutoGen instrumentor alone. Set CETRA_API_KEY to your sk-cetra-… key and OPENAI_API_KEY to your OpenAI key.
AutoGen reads its runtime tracing switch when it is imported, so set it before the import.
import asyncioimport os
os.environ["AUTOGEN_DISABLE_RUNTIME_TRACING"] = "true"
import cetra
# Only the AutoGen instrumentor is enabled. Enabling the OpenAI instrumentor as well# would record each model call twice.cetra.init(instrument=["autogen-agentchat"])
from autogen_agentchat.agents import AssistantAgent # noqa: E402from autogen_ext.models.openai import OpenAIChatCompletionClient # noqa: E402
async def get_weather(city: str) -> str: """Get the weather.""" return f"sunny in {city}"
client = OpenAIChatCompletionClient( model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"],)agent = AssistantAgent( "weather_agent", model_client=client, tools=[get_weather], system_message="You are a weather bot.", reflect_on_tool_use=True,)
async def run() -> None: await agent.run(task="What is the weather in Paris?")
asyncio.run(run())
cetra.flush()What you get
Section titled “What you get”With one tool call, a run produces:
- an
agentspan for the agent’s creation, and anagentspan for the run; - an
agentspan namedweather_agent.on_messages_stream, with the messages as its input; - AutoGen’s native spans,
create_agent weather_agent,invoke_agent weather_agentandexecute_tool get_weather, which carry the agent and tool names but no model, tokens or messages; - an
llmspan for each model call, namedOpenAIChatCompletionClient.create, with the provideropenai, the model, the token counts, the cost computed from the built-in prices and the messages; toolspans for the tool call, with its arguments and result.
Limits
Section titled “Limits”- Enable
instrument=["autogen-agentchat"]only. Addingopenairecords each model call twice. - AutoGen’s tool spans include a second, content-free
toolspan namedexecute_tool get_weather, next to the one with the arguments and result. - The
weather_agent.on_messages_streamspan in the fixture carries anexceptionevent that AutoGen records. Its status staysok. - The fixture was captured with
autogen-agentchat0.7.5,autogen-ext0.7.5 andopeninference-instrumentation-autogen-agentchat0.1.29. - 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.