Skip to main content

Tracing Claude Code

MLflow Tracing automatically traces Claude Code conversations and agents authored using Claude Agent SDK, capturing user prompts, AI responses, tool usage, timing, and session metadata.

MLflow supports two approaches for Claude Code tracing:

  • CLI tracing: Configure tracing through the MLflow CLI to automatically trace interactive Claude Code sessions (MLflow 3.4+)
  • SDK tracing: Enable tracing programmatically for Python applications using the Claude Agent SDK (MLflow 3.5+)

Requirements

Claude Code CLI tracing requires:

Bash
pip install --upgrade "mlflow[databricks]>=3.4"

Set Databricks environment variables

Configure the following Databricks environment variables:

Bash
export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-personal-access-token"
export ANTHROPIC_API_KEY="your-anthropic-api-key"

For production environments, use Mosaic AI Gateway or Databricks secrets for secure API key management.

Trace Claude

Trace Claude Code using the CLI or Claude Agent SDK.

  1. Use mlflow autolog claude to configure Claude Code hooks in a .claude/settings.json file in your project directory:

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

    Additional configuration options:

    Bash
    # 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
    note

    To disable tracing, run mlflow autolog claude --disable. This removes the tracing configuration from .claude/settings.json.

  2. Check tracing status

    Bash
    mlflow autolog claude --status
  3. When you use the claude command in your project directory with tracing enabled, your conversations are automatically traced:

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

  4. View traces in the MLflow UI:

    Bash
    # View traces in MLflow UI
    mlflow ui

Troubleshooting

Verify that CLI tracing is enabled for your project:

Bash
mlflow autolog claude --status

This displays the current tracing configuration and whether it's active for the Claude Code CLI.

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