将 Litefuse 与 AutoGen 集成
本 notebook 提供一个分步指南,演示如何将 Litefuse 与 AutoGen 集成,从而对你的多 Agent LLM 应用实现全面的可观测性与调试。
什么是 AutoGen? AutoGen (GitHub) 是 Microsoft 开发的开源框架,用于构建 LLM 应用,包括能够进行复杂推理与交互的 Agent。AutoGen 简化了对话式 Agent 的创建,让它们可以协作或竞争来解决任务。
什么是 Litefuse? Litefuse 是一个开源的 AI Agent 可观测性与评估平台。它为 AI 应用提供 tracing 与监控能力。Litefuse 通过提供详细的洞察以及通过原生集成、OpenTelemetry 和专用 SDK 与众多工具和框架对接,帮助开发者调试、分析并优化其 AI 系统。
快速开始
我们通过一个实际示例来演示如何使用 AutoGen 并通过 OpenTelemetry 将其与 Litefuse 集成,从而获得全面的 tracing 能力。
第 1 步:安装依赖
%pip install langfuse openlit "autogen-agentchat" "autogen-ext[openai]" -U第 2 步:配置 Langfuse SDK
接下来,设置你的 Litefuse API Key。你可以通过注册免费的 Litefuse Cloud 账号或 自托管 Litefuse 获取这些 Key。这些环境变量是 Langfuse 客户端鉴权并向你的 Litefuse 项目发送数据所必需的。
import os
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your openai key
os.environ["OPENAI_API_KEY"] = "sk-proj-..."设置好环境变量后,我们就可以初始化 Langfuse 客户端了。get_client() 会使用环境变量中提供的凭证来初始化 Langfuse 客户端。
from langfuse import Langfuse
from langfuse.span_filter import is_default_export_span
# Filter out AutoGen runtime spans
blocked_scopes = {"autogen SingleThreadedAgentRuntime"}
langfuse = Langfuse(
should_export_span=lambda span: (
is_default_export_span(span)
and not (
span.instrumentation_scope is not None
and span.instrumentation_scope.name in blocked_scopes
)
)
)
# Verify connection
if langfuse.auth_check():
print("Langfuse client is authenticated and ready!")
else:
print("Authentication failed. Please check your credentials and host.")Langfuse client is authenticated and ready!
第 3 步:初始化 OpenLit Instrumentation
现在,我们初始化 OpenLit instrumentation。OpenLit 会自动捕获 AutoGen 的操作,并将 OpenTelemetry (OTel) span 导出到 Litefuse。
import openlit
# Initialize OpenLIT instrumentation. The disable_batch flag is set to true to process traces immediately.
openlit.init(tracer=langfuse._otel_tracer, disable_batch=True)第 5 步:基础 AutoGen 应用
让我们创建一个简单的 AutoGen 应用。在本示例中,我们创建一个简单的多 Agent 对话,由一个 Assistant Agent 回答用户的问题。这将作为演示 Litefuse tracing 的基础。
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(model="gpt-4o")
agent = AssistantAgent("assistant", model_client=model_client)
print(await agent.run(task="Say 'Hello World!'"))
await model_client.close()messages=[TextMessage(id=‘0935120c-2a27-4bb8-ad7f-eb5cb2b4902d’, source=‘user’, models_usage=None, metadata=, created_at=datetime.datetime(2025, 7, 8, 8, 32, 25, 422043, tzinfo=datetime.timezone.utc), content=“Say ‘Hello World!’”, type=‘TextMessage’), TextMessage(id=‘2046975a-235c-4e46-be55-719bb79ced95’, source=‘assistant’, models_usage=RequestUsage(prompt_tokens=41, completion_tokens=5), metadata=, created_at=datetime.datetime(2025, 7, 8, 8, 32, 26, 265424, tzinfo=datetime.timezone.utc), content=‘Hello World! TERMINATE’, type=‘TextMessage’)] stop_reason=None
第 6 步:在 Litefuse 中查看 Trace
执行应用后,进入你的 Litefuse Trace 表。你将看到应用执行的详细 trace,包括 Agent 对话、LLM 调用、输入、输出以及性能指标的洞察。Trace 将完整呈现从初始用户提问、Agent 交互到最终响应的全过程。

你也可以在这里查看公开 trace:Litefuse Trace 示例
Interoperability with the Python SDK
You can use this integration together with the Litefuse SDKs to add additional attributes to the observation.
The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the observation.
from langfuse import observe, propagate_attributes, get_client
langfuse = get_client()
@observe()
def my_llm_pipeline(input):
# Add additional attributes (user_id, session_id, metadata, version, tags) to all spans created within this execution scope
with propagate_attributes(
user_id="user_123",
session_id="session_abc",
tags=["agent", "my-observation"],
metadata={"email": "user@litefuse.ai"},
version="1.0.0"
):
# YOUR APPLICATION CODE HERE
result = call_llm(input)
return result
# Run the function
my_llm_pipeline("Hi")Learn more about using the Decorator in the Langfuse SDK instrumentation docs.
Troubleshooting
No observations appearing
First, enable debug mode in the Python SDK:
export LANGFUSE_DEBUG="True"Then run your application and check the debug logs:
- OTel observations appear in the logs: Your application is instrumented correctly but observations are not reaching Litefuse. To resolve this:
- Call
langfuse.flush()at the end of your application to ensure all observations are exported. - Verify that you are using the correct API keys and base URL.
- Call
- No OTel spans in the logs: Your application is not instrumented correctly. Make sure the instrumentation runs before your application code.
Unwanted observations in Litefuse
The Langfuse SDK is based on OpenTelemetry. Other libraries in your application may emit OTel spans that are not relevant to you. These still count toward your billable units, so you should filter them out. See Unwanted spans in Litefuse for details.
Missing attributes
Some attributes may be stored in the metadata object of the observation rather than being mapped to the Litefuse data model. If a mapping or integration does not work as expected, please raise an issue on GitHub.
Next Steps
Once you have instrumented your code, you can manage, evaluate and debug your application: