MLflow 跟踪 会自动跟踪使用 Claude 代理 SDK 创作的 Claude Code 对话和代理,捕获用户提示、AI 响应、工具使用情况、计时和会话元数据。
MLflow 支持用于 Claude Code 跟踪的两种方法:
- CLI 跟踪:通过 MLflow CLI 配置跟踪以自动跟踪交互式 Claude Code 会话(MLflow 3.4+)
- SDK 跟踪:使用 Claude 代理 SDK 以编程方式为 Python 应用程序启用跟踪(MLflow 3.5+)
要求
CLI 跟踪
Claude Code CLI 跟踪需要以下条件:
- Claude CLI
- MLflow 3.4 或更高版本,附带 Databricks 附加功能
pip install --upgrade "mlflow[databricks]>=3.4"
SDK 追踪
Claude Agent SDK 跟踪要求:
- Claude 代理 SDK 0.1.0 或更高版本
- 使用 Databricks Extras 的 MLflow 3.5 或更新版本
pip install --upgrade "mlflow[databricks]>=3.5" "claude-agent>=0.1.0"
设置 Databricks 环境变量
配置以下 Databricks 环境变量:
export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-personal-access-token"
export ANTHROPIC_API_KEY="your-anthropic-api-key"
对于生产环境,请使用 马赛克 AI 网关或 Databricks 机密 进行安全 API 密钥管理。
跟踪克劳德
使用 CLI 或 Claude 代理 SDK 跟踪 Claude 代码。
CLI 跟踪
用
mlflow autolog claude在项目目录的.claude/settings.json文件中配置 Claude Code 挂钩。# Set up tracing with Databricks (uses environment variables set earlier) mlflow autolog claude ~/my-project # Or set up tracing in current directory mlflow autolog claude其他配置选项:
# Specify experiment by name mlflow autolog claude -n "My AI Project" # Specify experiment by ID mlflow autolog claude -e 123456789 # Use local file-based tracking instead of Databricks mlflow autolog claude -u file://./custom-mlruns mlflow autolog claude -u sqlite:///mlflow.db注释
若要禁用跟踪,请运行
mlflow autolog claude --disable。 这会从.claude/settings.json中删除跟踪配置。检查跟踪状态
mlflow autolog claude --status在启用了跟踪的项目目录中使用 claude 命令时,会自动跟踪对话:
# 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"在 MLflow 界面中查看跟踪数据:
# View traces in MLflow UI mlflow ui
SDK 追踪
注释
MLflow 不支持跟踪对 query 的直接调用。 MLflow 仅支持跟踪使用 ClaudeSDKClient 的交互。
为 Claude 代理 SDK 启用自动记录以跟踪所有 Claude 代理 SDK 交互:
import asyncio
import mlflow.anthropic
from claude_agent_sdk import ClaudeSDKClient
# Enable autologging
mlflow.anthropic.autolog()
# Disable autologging
# mlflow.anthropic.autolog(disable=True)
# Optionally configure MLflow experiment
mlflow.set_experiment("my_claude_app")
async def main():
async with ClaudeSDKClient() as client:
await client.query("What is the capital/major city of France?")
async for message in client.receive_response():
print(message)
if __name__ == "__main__":
asyncio.run(main())
还可以将 SDK 跟踪与 MLflow 的 GenAI 评估框架配合使用:
import asyncio
import pandas as pd
from claude_agent_sdk import ClaudeSDKClient
import mlflow.anthropic
from mlflow.genai import evaluate, scorer
from mlflow.genai.judges import make_judge
mlflow.anthropic.autolog()
async def run_agent(query: str) -> str:
"""Run Claude Agent SDK and return response"""
async with ClaudeSDKClient() as client:
await client.query(query)
response_text = ""
async for message in client.receive_response():
response_text += str(message) + "\n\n"
return response_text
def predict_fn(query: str) -> str:
"""Synchronous wrapper for evaluation"""
return asyncio.run(run_agent(query))
relevance = make_judge(
name="relevance",
instructions=(
"Evaluate if the response in {{ outputs }} is relevant to "
"the question in {{ inputs }}. Return either 'pass' or 'fail'."
),
model="openai:/gpt-4o",
)
# Create evaluation dataset
eval_data = pd.DataFrame(
[
{"inputs": {"query": "What is machine learning?"}},
{"inputs": {"query": "Explain neural networks"}},
]
)
# Run evaluation with automatic tracing
mlflow.set_experiment("claude_evaluation")
evaluate(data=eval_data, predict_fn=predict_fn, scorers=[relevance])
Troubleshooting
CLI 跟踪
请验证项目是否已启用 CLI 追踪功能:
mlflow autolog claude --status
这会显示当前跟踪配置,以及它是否对 Claude Code CLI 处于活动状态。
跟踪不起作用:
- 请确保您位于已配置的目录中。
- 确认
.claude/settings.json exists - 在
.claude/mlflow/claude_tracing.log查看日志
缺少痕迹:
- 检查配置中是否存在
MLFLOW_CLAUDE_TRACING_ENABLED=true - 验证跟踪 URI 是否可访问
- 查看 .claude/mlflow/claude_tracing.log 中的日志
SDK 追踪
缺少痕迹:
- 在创建 ClaudeSDKClient 之前,请验证是否已经调用了 mlflow.anthropic.autolog()
- 检查是否已正确配置跟踪 URI 和试验 ID