Traces Not Clubbed for Multiple Agents in Langfuse v3 #6291
-
DescriptionI am experiencing an issue when integrating CrewAI with Langfuse. In the previous version (Langfuse v2), I was able to club traces from multiple agents (within one crew) into a single trace. However, after upgrading to Langfuse v3, the traces are no longer getting clubbed together. Expected BehaviorTraces from multiple agents within a single Crew should be grouped into a single trace, as was the behavior in Langfuse v2. Observed BehaviorEach agent/task appears to be generating its own trace, instead of being grouped together under one trace. Steps to ReproduceBelow is the code used to replicate the issue: import os
import base64
import openlit
from config import LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_HOST, OPENAI_API_KEY
from langfuse import Langfuse
import asyncio
langfuse = Langfuse(
public_key=LANGFUSE_PUBLIC_KEY,
secret_key=LANGFUSE_SECRET_KEY,
host=LANGFUSE_HOST
)
LANGFUSE_AUTH=base64.b64encode(f"{LANGFUSE_PUBLIC_KEY}:{LANGFUSE_SECRET_KEY}".encode()).decode()
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{LANGFUSE_HOST}/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
openlit.init(disable_batch=True)
from langfuse.decorators import langfuse_context, observe
from crewai import Agent, Task, Crew
from llm_services import CrewAILLM
# Define your agents
coder = Agent(
role='Software developer',
goal='To be a software developer',
backstory='An expert coder with a keen eye for software trends.',
llm=CrewAILLM.get_llm()
)
# Create tasks for your agents
task1 = Task(
description="Give me a list of top 5 companies in the world",
expected_output="A list of top 5 companies in the world",
agent=coder
)
task2 = Task(
description="Give me a list of top 5 companies in India",
expected_output="A list of top 5 companies in India",
agent=coder
)
# Instantiate your crew
crew = Crew(
agents=[coder],
tasks=[task1, task2],
share_crew=False,
verbose=True
)
@observe()
async def kickoff_crew():
result = await crew.kickoff_async()
trace_url = langfuse_context.get_current_trace_url()
print(f"\n\nTrace URL: {trace_url}\n\n")
print(f"\n\nResult: {result}\n\n")
langfuse_context.update_current_trace(name="kickoff_crew", session_id="123", user_id="vwo")
asyncio.run(kickoff_crew())Additional Information
Has there been any change in Langfuse v3 that affects how traces are grouped? Do I need to configure something differently in the new version? Any insights would be greatly appreciated!
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Hello @ashish-vwo! I'm here to help you with software development, bug fixing, and answering questions. If you need assistance, feel free to ask. In Langfuse v3, there has been a change in how traces are grouped. By default, each invocation now ends up on its own trace, rather than grouping multiple invocations into a single trace as in previous versions. To continue grouping multiple invocations into one trace, you need to use the Langfuse SDK with the Langchain integration. Here's how you can configure it: from langfuse import Langfuse
langfuse = Langfuse()
# Get Langchain handler for a trace
trace = langfuse.trace()
langfuse_handler = trace.get_langchain_handler()
# langfuse_handler will use the trace for all invocationsIf you want to merge traces from separate applications, you can use the same from langfuse.decorators import langfuse_context, observe
@observe()
def your_function():
langfuse_context.update_current_trace(
session_id="your-shared-session-id"
)This setup should help you club traces from multiple agents into a single trace or group them under the same session [1][2]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other |
Beta Was this translation helpful? Give feedback.
-
|
You can remove the manual instrumentation via observe() and Langfuse() as you switched to the Opentelemetry based integration. This should solve this issue as you are currently instrument the application 2 times. |
Beta Was this translation helpful? Give feedback.
-
|
Hey @ashish-vwo , You can use the span properties as shown in the example to set the userId and sessionId. Let me know whether that works for you. |
Beta Was this translation helpful? Give feedback.




Hey @ashish-vwo ,
As discussed, please try to follow the steps in https://langfuse.com/docs/opentelemetry/example-openlit#step-5-pass-additional-attributes-optional and create a "wrapper" trace using
You can use the span properties as shown in the example to set the userId and sessionId. Let me know whether that works for you.