I’m building a file processing system with Autogen AG2 that uses multiple agents working together. My setup includes a processor agent that breaks files into sections and a reviewer agent that examines each part. These agents communicate through a GroupChat configuration.
I know AG2 works well with AgentOps for monitoring, but I want to use LangSmith instead since my other projects already use it.
Here’s my current implementation in the FileProcessor class:
My question: What’s the best way to add LangSmith monitoring to this AG2 GroupChat setup?
What I’ve tried: I looked online but didn’t find clear examples. I tried some AI-generated solutions but they don’t work because autogen handles the LLM calls internally.
Any examples or documentation for this would be really helpful!
I hit the same problem migrating from AgentOps to LangSmith for my document processing pipeline. The wrapper approach works, but I found something simpler that doesn’t mess with your agent setup.
Skip wrapping each agent’s config. Instead, use LangSmith’s decorator on your execute method and monkey patch the OpenAI calls before AG2 starts up. Here’s what I did:
from langsmith import traceable
from langsmith.wrappers import wrap_openai
import openai
class FileProcessor:
def __init__(self):
# Patch OpenAI globally before any AG2 initialization
if not hasattr(openai, '_langsmith_wrapped'):
openai.ChatCompletion = wrap_openai(openai.ChatCompletion)
openai._langsmith_wrapped = True
print("Setting up FileProcessor")
self.reviewer = review_agent.ReviewAgent().agent
self.processor = process_agent.ProcessAgent().agent
self.coordinator = autogen.UserProxyAgent(
name="Coordinator", code_execution_config=False
)
@traceable(project_name="autogen-file-processing")
def execute(self):
# Your existing code unchanged
self.chat_group = autogen.GroupChat(
agents=[self.coordinator, self.processor, self.reviewer],
messages=[],
max_round=15,
speaker_selection_method=self._handle_transitions,
)
self.chat_manager = autogen.GroupChatManager(
groupchat=self.chat_group, llm_config=config.model_settings
)
try:
self.coordinator.initiate_chat(
self.chat_manager, message="Process file_id:" + str(self.file_id)
)
except Exception as error:
print(f"Chat failed: {error}")
This catches all LLM calls without touching your existing agent setup. The global patch means every OpenAI call goes through LangSmith no matter how AG2 handles internal routing. I’ve run this in production for three months with zero issues.
Don’t forget to set LANGSMITH_TRACING=true in your environment variables with your API key.
I fought with this same integration for weeks switching from AgentOps to LangSmith. The monkey patching approach works, but I kept hitting version conflicts with different OpenAI client versions.
What finally worked was using LangSmith’s run context directly in the GroupChat callbacks. AG2 has hooks you can use without touching the core LLM config:
from langsmith import trace
import autogen
class FileProcessor:
def __init__(self):
self.reviewer = review_agent.ReviewAgent().agent
self.processor = process_agent.ProcessAgent().agent
self.coordinator = autogen.UserProxyAgent(
name="Coordinator", code_execution_config=False
)
# Override agent send methods to add tracing
self._wrap_agents_with_tracing()
def _wrap_agents_with_tracing(self):
for agent in [self.reviewer, self.processor]:
original_send = agent.send
agent.send = lambda message, recipient, request_reply=None, silent=False, agent_ref=agent: \
self._traced_send(original_send, message, recipient, agent_ref.name, request_reply, silent)
def _traced_send(self, original_send, message, recipient, agent_name, request_reply, silent):
with trace(name=f"agent_communication_{agent_name}",
inputs={"message": message, "recipient": recipient.name}) as run:
result = original_send(message, recipient, request_reply, silent)
run.end(outputs={"response": str(result)})
return result
This intercepts agent communications instead of LLM calls directly. You get conversation-level tracing that’s way more useful for multi-agent debugging. I can see the full conversation flow in LangSmith without any OpenAI client compatibility headaches.
Here’s the thing - you want to trace agent interactions, not just individual LLM calls. This captures the actual decision-making between agents, which is what you need when debugging failures.
Honestly, just set the LangSmith environment variables and use their auto-instrumentation. Add LANGSMITH_TRACING=true and LANGSMITH_PROJECT=“your-project” to your env, then import langsmith before autogen. It automatically picks up all OpenAI calls without any code changes. Works perfectly with my 4-agent pipeline.