AutoGen
AutoGen (Microsoft Research) orchestrates conversations between multiple specialised agents. Trefur ships a dedicated patch_autogen adapter that wraps AutoGen's reply and chat lifecycle, so every agent turn across a group chat is captured and attributed to that agent's own name — no manual instrumentation required.
Install
pip install trefur-observe pyautogen openaiSetup
import os
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager
from trefur_observe import TrefurObserve, patch_autogen
# Initialise observe once at startup.
TrefurObserve.init(
api_key=os.environ["TREFUR_API_KEY"],
agent={"name": "research-crew", "framework": "autogen"},
)
# patch_autogen wraps AutoGen's reply + chat lifecycle, capturing every
# agent turn across the group chat — planner + critic + tool-runner —
# each attributed to the agent's own name.
patch_autogen(TrefurObserve.get_instance())
llm_config = {
"model": "gpt-4o",
"api_key": os.environ["OPENAI_API_KEY"],
}
planner = AssistantAgent(
name="planner",
llm_config=llm_config,
system_message="You decompose tasks into steps.",
)
researcher = AssistantAgent(
name="researcher",
llm_config=llm_config,
system_message="You execute steps and report findings.",
)
user = UserProxyAgent(
name="user",
human_input_mode="NEVER",
code_execution_config=False,
)
chat = GroupChat(agents=[user, planner, researcher], messages=[], max_round=10)
manager = GroupChatManager(groupchat=chat, llm_config=llm_config)
user.initiate_chat(manager, message="Summarise the latest Anthropic safety paper.")What gets traced
- Every agent turn across planner + critic + tool-runner agents — each reply attributed to the agent's own name.
- Tool calls registered via
register_function— call args + return values are emitted as steps. - Group-chat order — each agent's turn renders in sequence under one conversation trace.
- Token + cost rollups — per model and per agent name.
Common use case — planner / critic / tool-runner triangle
AutoGen's default pattern uses one agent to plan, one to critique, and one to execute. Trefur shows all three turns nested under a single conversation trace; you can drill into the planner turn to see why the critic rejected an output, or build a drift alert when tool-runner turn count exceeds N.
Common pitfalls
- AutoGen's cache. AutoGen aggressively caches LLM responses to disk. Cache hits never hit the SDK and therefore never reach Trefur. Disable with
llm_config={..., "cache_seed": None}when you need full telemetry. - Flush before exit. Short AutoGen scripts often exit immediately after
initiate_chatreturns. CallTrefurObserve.get_instance().flush()before the process exits so the final turns are shipped rather than dropped with the buffer.
See the Python SDK reference for patch_autogen + flush details.