AutoGen (tracing)

AutoGen AgentChat is Microsoft’s framework for building event-driven single-agent and multi-agent applications. Respan gives you full observability over agent runs, team conversations, tool calls, and LLM generations.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See AutoGen gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-autogen
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY is used for AutoGen model calls. RESPAN_API_KEY is used to export traces to Respan. Set OPENAI_MODEL to override the default gpt-4o-mini model.

3

Initialize and run

1import asyncio
2import os
3
4from autogen_agentchat.agents import AssistantAgent
5from autogen_ext.models.openai import OpenAIChatCompletionClient
6from respan import Respan
7from respan_instrumentation_autogen import AutoGenInstrumentor
8
9respan_api_key = os.environ["RESPAN_API_KEY"]
10openai_api_key = os.environ["OPENAI_API_KEY"]
11openai_model = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
12
13respan = Respan(
14 api_key=respan_api_key,
15 instrumentations=[AutoGenInstrumentor()],
16)
17
18model_client = OpenAIChatCompletionClient(
19 model=openai_model,
20 api_key=openai_api_key,
21)
22
23agent = AssistantAgent(
24 name="assistant",
25 model_client=model_client,
26 system_message="You answer with concise, practical engineering advice.",
27)
28
29async def main():
30 result = await agent.run(
31 task="In one sentence, explain why tracing helps multi-agent apps."
32 )
33 print(result.messages[-1].content)
34 await model_client.close()
35 respan.flush()
36
37asyncio.run(main())
4

View your trace

Open the Traces page to see your AutoGen workflow with agent spans, team spans, tool spans, and LLM generations.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. AutoGenInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from respan_instrumentation_autogen import AutoGenInstrumentor
3
4respan = Respan(
5 instrumentations=[AutoGenInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "autogen-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from respan import Respan, propagate_attributes
2from respan_instrumentation_autogen import AutoGenInstrumentor
3
4respan = Respan(instrumentations=[AutoGenInstrumentor()])
5
6async def handle_request(user_id: str, message: str):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 result = await agent.run(task=message)
13 print(result.messages[-1].content)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Examples

Tool calls

Tool calls are captured as spans with inputs, outputs, and timing.

1from autogen_agentchat.agents import AssistantAgent
2
3async def estimate_latency(service: str, requests_per_minute: int) -> str:
4 """Estimate API latency for a service under load."""
5 return f"{service}: about 155 ms p95 latency"
6
7agent = AssistantAgent(
8 name="capacity_planner",
9 model_client=model_client,
10 tools=[estimate_latency],
11 reflect_on_tool_use=True,
12 system_message="Use the estimate_latency tool before answering.",
13)
14
15result = await agent.run(
16 task="Estimate p95 latency for tracing-api at 240 requests per minute."
17)
18print(result.messages[-1].content)

Teams

Team conversations are traced with workflow, agent, and LLM spans.

1from autogen_agentchat.agents import AssistantAgent
2from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
3from autogen_agentchat.teams import RoundRobinGroupChat
4
5planner = AssistantAgent(
6 name="planner",
7 model_client=model_client,
8 system_message="Create a compact implementation plan.",
9)
10reviewer = AssistantAgent(
11 name="reviewer",
12 model_client=model_client,
13 system_message="Review the plan. If clear, end with APPROVED.",
14)
15
16team = RoundRobinGroupChat(
17 [planner, reviewer],
18 termination_condition=TextMentionTermination("APPROVED") | MaxMessageTermination(4),
19)
20result = await team.run(task="Plan a small release checklist.")