跳到主要内容

跟踪 Claude Code

Claude Code Tracing via CLI autolog

MLflow Tracing 为 Claude Code 提供自动跟踪功能。

  1. CLI 跟踪:自动跟踪交互式 Claude Code CLI 对话。
  2. SDK 跟踪:跟踪 Python 应用程序中的 Claude Agent SDK 用法。

设置自动跟踪后,MLflow 将自动捕获 Claude Code 对话的跟踪记录,并将其记录到活动的 MLflow 实验中。跟踪记录会自动捕获以下信息:

  • 用户提示和助手响应
  • 工具使用(文件操作、代码执行、网络搜索等)
  • 对话时间和持续时间
  • 工具执行结果
  • 会话元数据,包括工作目录和用户

要求

  • MLflow >= 3.4 (pip install mlflow>=3.4)
  • 对于 CLI 跟踪:已安装并配置 Claude Code CLI
  • 对于 SDK 跟踪:Claude Agent SDK >= 0.1.0 (pip install claude-agent-sdk >= 0.1.0)

设置

Claude Code 跟踪可以通过 CLI 命令(用于交互式使用)或 Python SDK 导入(用于编程使用)进行配置。

CLI 跟踪设置

使用 CLI 跟踪自动捕获交互式 Claude Code CLI 对话。

基本设置

# Set up tracing in current directory
mlflow autolog claude

# Set up tracing in specific directory
mlflow autolog claude ~/my-project

# Check tracing status
mlflow autolog claude --status

# Disable tracing
mlflow autolog claude --disable

配置示例

# Set up with custom tracking URI
mlflow autolog claude -u file://./custom-mlruns
mlflow autolog claude -u sqlite:///mlflow.db

# Set up with Databricks backend and a specific experiment ID
mlflow autolog claude -u databricks -e 123456789

# Set up with specific experiment
mlflow autolog claude -n "My AI Project"

工作原理

  1. 设置阶段mlflow autolog claude 命令会在项目目录中的 .claude/settings.json 文件中配置 Claude Code 钩子。
  2. 自动跟踪:在配置的目录中使用 claude 命令时,对话会自动被跟踪。
  3. 查看结果:使用 MLflow UI 探索您的跟踪记录。

基本示例

# Set up tracing in your project
mlflow autolog claude ~/my-project

# Navigate to project directory
cd ~/my-project

# Use Claude Code normally - tracing happens automatically
claude "help me refactor this Python function to be more efficient"

# View traces in MLflow UI
mlflow ui

故障排除

检查 CLI 状态

mlflow autolog claude --status

这会显示:

  • 跟踪是否已启用
  • 当前跟踪 URI
  • 配置的实验
  • 任何配置问题

常见的 CLI 问题

跟踪不起作用

  • 确保您在已配置的目录中。
  • 检查 .claude/settings.json 是否存在。
  • 查看 .claude/mlflow/claude_tracing.log 中的日志。

缺少跟踪记录

  • 检查您的配置中是否设置了 MLFLOW_CLAUDE_TRACING_ENABLED=true
  • 验证跟踪 URI 是否可访问。
  • 查看 .claude/mlflow/claude_tracing.log 中的日志。

禁用 CLI 跟踪

要停止自动 CLI 跟踪,请执行以下操作:

mlflow autolog claude --disable

这将从 .claude/settings.json 中移除钩子,但会保留现有的跟踪记录。