GEO

如何监控AI代理工作流?2026年LangChain与CrewAI集成指南

2026/3/7
如何监控AI代理工作流?2026年LangChain与CrewAI集成指南
AI Summary (BLUF)

This repository provides official Python integrations for ContextGraph Cloud, enabling seamless monitoring and logging of AI agent workflows in LangChain and CrewAI frameworks.

原文翻译: 该仓库提供ContextGraph Cloud的官方Python集成,支持在LangChain和CrewAI框架中无缝监控和记录AI智能体工作流。

引言

在现代人工智能应用开发中,尤其是在构建复杂的多代理系统时,可观测性、审计追踪和合规性报告已成为不可或缺的需求。开发者需要清晰地了解代理的决策过程、工具调用链以及任务执行的生命周期。ContextGraph Cloud 正是为解决这些挑战而设计,而 contextgraph-integrations 项目则提供了官方 Python 集成,让开发者能够轻松地将这些能力注入到主流的 AI 框架中。

在现代人工智能应用开发中,尤其是在构建复杂的多代理系统时,可观测性、审计追踪和合规性报告已成为不可或缺的需求。开发者需要清晰地了解代理的决策过程、工具调用链以及任务执行的生命周期。ContextGraph Cloud 正是为解决这些挑战而设计,而 contextgraph-integrations 项目则提供了官方 Python 集成,让开发者能够轻松地将这些能力注入到主流的 AI 框架中。

可用集成

contextgraph-integrations 项目目前为两个主流的 AI 代理框架提供了官方支持包:

包名 框架 安装命令
contextgraph-langchain LangChain pip install contextgraph-langchain
contextgraph-crewai CrewAI pip install contextgraph-crewai

The contextgraph-integrations project currently provides official support packages for two mainstream AI agent frameworks:

Package Framework Install Command
contextgraph-langchain LangChain pip install contextgraph-langchain
contextgraph-crewai CrewAI pip install contextgraph-crewai

快速开始

LangChain 集成

对于使用 LangChain(特别是较新版本)的开发者,可以通过 ContextGraphCallback 回调处理器轻松集成。项目已更新至支持 LangChain v1 及以上的中间件模式,同时也兼容旧版的回调模式。

For developers using LangChain (especially newer versions), integration can be easily achieved through the ContextGraphCallback callback handler. The project has been updated to support the middleware pattern of LangChain v1 and above, while also maintaining compatibility with the older callback pattern.

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from contextgraph_langchain import ContextGraphCallback

# 初始化回调处理器
callback = ContextGraphCallback(
    api_key="your-api-key",  # 您的 ContextGraph API 密钥
    agent_id="my-agent"      # 用于标识此代理的唯一 ID
)

# 构建您的 LLM 和代理
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, tools) # `tools` 是您定义的工具列表

# 运行代理并附加回调
result = agent.invoke(
    {"messages": [("user", "What's the weather?")]},
    config={"callbacks": [callback]}
)

CrewAI 集成

对于基于 CrewAI 框架构建的多代理协作系统,可以通过 ContextGraphObserver 观察者进行集成,从而追踪整个 Crew 中所有代理和任务的执行情况。

For multi-agent collaborative systems built on the CrewAI framework, integration can be done via the ContextGraphObserver, enabling tracking of the execution of all agents and tasks within the entire Crew.

from crewai import Crew
from contextgraph_observer import ContextGraphObserver

# 初始化观察者
observer = ContextGraphObserver(
    api_key="your-api-key",  # 您的 ContextGraph API 密钥
    crew_id="my-crew"        # 用于标识此 Crew 的唯一 ID
)

# 构建您的 Crew
crew = Crew(
    agents=[agent1, agent2],
    tasks=[task1, task2],
    callbacks=[observer]     # 将观察者添加到回调列表
)

# 启动任务
result = crew.kickoff()

记录内容

每个集成都会自动捕获以下关键信息,为您的 AI 工作流提供完整的上下文:

  • 工具调用 - 调用了哪些工具以及调用原因。
  • 工具执行 - 输入参数、输出结果以及执行过程中出现的错误。
  • 代理推理 - 代理的思考过程和决策逻辑。
  • 任务生命周期 - 任务的开始、完成、失败等状态事件。

Each integration automatically captures the following key information, providing complete context for your AI workflows:

  • Tool Invocations - What tools are being called and why.
  • Tool Executions - Input parameters, output results, and any errors that occur during execution.
  • Agent Reasoning - The agent's thought process and decision logic.
  • Task Lifecycle - Status events such as task start, completion, and failure.

所有事件都会被记录并发送到 ContextGraph Cloud 平台,形成完整的上下文链条,主要用于:

  • 审计追踪 - 满足内部审计或监管合规要求。
  • 策略执行 - 监控和确保代理行为符合预设策略。
  • 合规性报告 - 生成用于演示合规性的详细报告。
  • 调试与可观测性 - 深入理解系统行为,快速定位和解决问题。

All events are logged and sent to the ContextGraph Cloud platform, forming a complete contextual chain, primarily used for:

  • Audit Trails - Meeting internal audit or regulatory compliance requirements.
  • Policy Enforcement - Monitoring and ensuring agent behavior aligns with predefined policies.
  • Compliance Reporting - Generating detailed reports to demonstrate compliance.
  • Debugging and Observability - Gaining deep insights into system behavior for rapid issue identification and resolution.

获取 API 密钥

要开始使用 ContextGraph 集成,您需要一个有效的 API 密钥。请访问 ContextGraph 官方网站 注册并获取您的密钥。

To start using ContextGraph integrations, you need a valid API key. Please visit the ContextGraph official website to register and obtain your key.

总结

contextgraph-integrations 项目通过提供轻量级、非侵入式的官方集成包,显著降低了为 LangChainCrewAI 应用添加企业级可观测性与合规性功能的门槛。无论是为了调试复杂的代理交互,还是为了满足严格的行业监管要求,这些工具都能提供不可或缺的透明度和追溯能力。随着 AI 代理在关键业务场景中的深入应用,此类可观测性基础设施的重要性将日益凸显。

The contextgraph-integrations project significantly lowers the barrier to adding enterprise-grade observability and compliance features to LangChain and CrewAI applications by providing lightweight, non-intrusive official integration packages. Whether for debugging complex agent interactions or meeting stringent industry regulatory requirements, these tools offer indispensable transparency and traceability. As AI agents become more deeply embedded in critical business scenarios, the importance of such observability infrastructure will continue to grow.

← 返回文章列表
分享到:微博

版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。

文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。

若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。