MLflow Claude Code Integration
This module provides automatic tracing integration between Claude Code and MLflow.
Module Structure
config.py- Configuration management (settings files, environment variables)hooks.py- Claude Code hook setup and managementcli.py- MLflow CLI commands (mlflow autolog claude)tracing.py- Core tracing logic and processorshooks/- Hook implementation handlers
Installation
pip install mlflow
Usage
Set up Claude Code tracing in any project directory:
# Set up tracing in current directory
mlflow autolog claude
# Set up tracing in specific directory
mlflow autolog claude -d ~/my-project
# Set up with custom tracking URI
mlflow autolog claude -u file://./custom-mlruns
mlflow autolog claude -u sqlite:///mlflow.db
# Set up with Databricks
mlflow autolog claude -u databricks -e 123456789
# Check status
mlflow autolog claude --status
# Disable tracing
mlflow autolog claude --disable
How it Works
- Setup: The
mlflow autolog claudecommand configures Claude Code hooks in a.claude/settings.jsonfile - Automatic Tracing: When you use the
claudecommand in the configured directory, your conversations are automatically traced to MLflow - View Traces: Use
mlflow serverto view your conversation traces
Configuration
The setup creates two types of configuration:
Claude Code Hooks
- PostToolUse: Captures tool usage during conversations
- Stop: Processes complete conversations into MLflow traces
Environment Variables
MLFLOW_CLAUDE_TRACING_ENABLED=true: Enables tracingMLFLOW_TRACKING_URI: Where to store traces (defaults to local.claude/mlflow/runs)MLFLOW_EXPERIMENT_IDorMLFLOW_EXPERIMENT_NAME: Which experiment to use
Examples
Basic Local Setup
mlflow autolog claude
cd .
claude "help me write a function"
mlflow server --backend-store-uri sqlite:///mlflow.db
Databricks Integration
mlflow autolog claude -u databricks -e 123456789
claude "analyze this data"
# View traces in Databricks
Custom Project Setup
mlflow autolog claude -d ~/my-ai-project -u sqlite:///mlflow.db -n "My AI Project"
cd ~/my-ai-project
claude "refactor this code"
mlflow server --backend-store-uri sqlite:///mlflow.db
Troubleshooting
Check Status
mlflow autolog claude --status
Disable Tracing
mlflow autolog claude --disable
View Raw Configuration
The configuration is stored in .claude/settings.json:
cat .claude/settings.json
Requirements
- Python 3.10+ (required by MLflow)
- MLflow installed (
pip install mlflow) - Claude Code CLI installed