Skip to main content

Tracing Claude Code

Claude Code Tracing via CLI autolog

MLflow Tracing provides automatic tracing for Claude Code:

  1. CLI tracing: Automatically trace interactive Claude Code CLI conversations
  2. SDK tracing: Trace Claude Agent SDK usage in Python applications

After setting up auto tracing, MLflow will automatically capture traces of your Claude Code conversations and log them to the active MLflow experiment. The trace automatically captures information such as:

  • User prompts and assistant responses
  • Tool usage (file operations, code execution, web searches, etc.)
  • Conversation timing and duration
  • Tool execution results
  • Session metadata including working directory and user

Requirements

  • MLflow >= 3.4 (pip install mlflow>=3.4)
  • For CLI tracing: Claude Code CLI installed and configured
  • For SDK tracing: Claude Agent SDK >= 0.1.0 (pip install claude-agent-sdk >= 0.1.0)

Setup

Claude Code tracing can be configured using either CLI commands (for interactive use) or Python SDK imports (for programmatic use).

CLI Tracing Setup

Use CLI tracing to automatically capture your interactive Claude Code CLI conversations.

Basic Setup

# 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

Configuration Examples

# 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"

How It Works

  1. Setup Phase: The mlflow autolog claude command configures Claude Code hooks in a .claude/settings.json file in your project directory
  2. Automatic Tracing: When you use the claude command in the configured directory, your conversations are automatically traced
  3. View Results: Use the MLflow UI to explore your traces

Basic Example

# 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

Troubleshooting

Check CLI Status

mlflow autolog claude --status

This shows:

  • Whether tracing is enabled
  • Current tracking URI
  • Configured experiment
  • Any configuration issues

Common CLI Issues

Tracing not working:

  • Ensure you're in the configured directory
  • Check that .claude/settings.json exists
  • Review logs in .claude/mlflow/claude_tracing.log

Missing traces:

  • Check if MLFLOW_CLAUDE_TRACING_ENABLED=true in your configuration
  • Verify the tracking URI is accessible
  • Review logs in .claude/mlflow/claude_tracing.log

Disable CLI Tracing

To stop automatic CLI tracing:

mlflow autolog claude --disable

This removes the hooks from .claude/settings.json but preserves existing traces.