Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(copilot): add Langfuse tracing to baseline LLM path (#12281)

Zamil Majdyce1675cfc7d9
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 6855fa702f..fba16804b6 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -13,6 +13,7 @@ from collections.abc import AsyncGenerator
from typing import Any
import orjson
+from langfuse import propagate_attributes
from backend.copilot.model import (
ChatMessage,
@@ -198,6 +199,20 @@ async def stream_chat_completion_baseline(
yield StreamStart(messageId=message_id, sessionId=session_id)
+ # Propagate user/session context to Langfuse so all LLM calls within
+ # this request are grouped under a single trace with proper attribution.
+ _trace_ctx: Any = None
+ try:
+ _trace_ctx = propagate_attributes(
+ user_id=user_id,
+ session_id=session_id,
+ trace_name="copilot-baseline",
+ tags=["baseline"],
+ )
+ _trace_ctx.__enter__()
+ except Exception:
+ logger.warning("[Baseline] Langfuse trace context setup failed")
+
assistant_text = ""
text_block_id = str(uuid.uuid4())
text_started = False
@@ -385,6 +400,13 @@ async def stream_chat_completion_baseline(
yield StreamError(errorText=error_msg, code="baseline_error")
# Still persist whatever we got
finally:
+ # Close Langfuse trace context
+ if _trace_ctx is not None:
+ try:
+ _trace_ctx.__exit__(None, None, None)
+ except Exception:
+ logger.warning("[Baseline] Langfuse trace context teardown failed")
+
# Persist assistant response
if assistant_text:
session.messages.append(
diff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.py
index d432d7f03a..2bcba33abd 100644
--- a/autogpt_platform/backend/backend/copilot/service.py
+++ b/autogpt_platform/backend/backend/copilot/service.py
@@ -11,8 +11,10 @@ import asyncio
import logging
from typing import Any
-import openai
from langfuse import get_client
+from langfuse.openai import (
+ AsyncOpenAI as LangfuseAsyncOpenAI, # pyright: ignore[reportPrivateImportUsage]
+)
from backend.data.db_accessors import understanding_db
from backend.data.understanding import format_understanding_for_prompt
@@ -26,7 +28,7 @@ logger = logging.getLogger(__name__)
config = ChatConfig()
settings = Settings()
-client = openai.AsyncOpenAI(api_key=config.api_key, base_url=config.base_url)
+client = LangfuseAsyncOpenAI(api_key=config.api_key, base_url=config.base_url)
langfuse = get_client()