2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DatabricksによるClaude Codeのトレース

2
Posted at

いつの間にこのような機能が。そして、地味に嬉しい。

MLflow Tracingは、Claude Agent SDK を使用して作成された Claude Code の会話とエージェントを自動的にトレースし、ユーザー プロンプト、AI 応答、ツールの使用状況、タイミング、セッション メタデータをキャプチャします。

通常、Claude Codeの会話履歴は定期的に圧縮されるので、大規模なプロジェクトで使っていると「過去にどんなやりとりしてたんだっけ?」となりがちです。この機能を使うことで、Claude Codeとのすべてのやり取りを記録しておくことができます。

Databricksでの準備

Databricks上のエクスペリメントに記録したいので、専用のエクスペリメントを作っておきます。
Screenshot 2025-11-13 at 21.49.08.png

あと、ワークスペースのURLをコピーしておき、パーソナルアクセストークンも取得しておきます。マニュアルにはAnthropicのAPIキーの記述もありますがClaude Codeのアカウントがあれば不要です。

ローカルでの準備

VSCode + Claude Codeを使う前提で準備します。まず、MLflowをローカルにインストールするのでPythonの仮想環境.venvを作ってそこにインストールします。

source .venv/bin/activate
pip install --upgrade "mlflow[databricks]>=3.4" 

Databricksとやりとりできるように環境変数を定義しておきます。

export DATABRICKS_HOST="https://xxxx.cloud.databricks.com"
export DATABRICKS_TOKEN="dapi...."

トレースの開始

こちらにあるコマンドでトレースをスタートします。

ちなみに、初めは以下のようにしたら、Databricksではなくローカルにトレースが表示されるようになってしまいました。-e 922449267353284では上で作成したエクスペリメントのIDを指定しています。

mlflow autolog claude -e 922449267353284

Screenshot 2025-11-13 at 21.50.32.png

この状態で以下のコマンドを実行してClaude Codeを呼び出します。

claude -p "pythonのhello world" 

メッセージにはmlflow uiを実行してねと表示されたので実行してみたら、ローカルでMLflow UIが起動しました。
Screenshot 2025-11-13 at 21.53.10.png
Screenshot 2025-11-13 at 21.53.23.png

一応目的は達成したのですが、本来やりたかったDatabricksへの記録ができていません。コマンドを確認します。

mlflow autolog claude --help
Usage: mlflow autolog claude [OPTIONS] [DIRECTORY]

  Set up Claude Code tracing in a directory.

  This command configures Claude Code hooks to automatically trace
  conversations to MLflow. After setup, use the regular 'claude' command and
  traces will be automatically created.

  DIRECTORY: Directory to set up tracing in (default: current directory)

  Examples:

    # Set up tracing in current directory with local storage   mlflow autolog
    claude

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

    # Set up tracing with Databricks   mlflow autolog claude -u databricks -e
    123456789

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

    # Disable tracing in current directory   mlflow autolog claude --disable

Options:
  -u, --tracking-uri TEXT     MLflow tracking URI (e.g., 'databricks' or
                              'file://mlruns')
  -e, --experiment-id TEXT    MLflow experiment ID
  -n, --experiment-name TEXT  MLflow experiment name
  --disable                   Disable Claude tracing in the specified
                              directory
  --status                    Show current tracing status
  --help                      Show this message and exit.

-u databricksが必要でしたので改めて。

mlflow autolog claude -u databricks -e 922449267353284

Tracking URIdatabricksが表示されるようになりました。
Screenshot 2025-11-13 at 21.56.28.png

上と同じくclaude -p "pythonのhello world"を実行すると今度はDatabricks上のエクスペリメントに記録されるようになりました。これは嬉しい。

Screenshot 2025-11-13 at 21.59.31.png
Screenshot 2025-11-13 at 21.59.39.png

Claude Codeと普段使いされている方にはおすすめの機能だと思います!
Screenshot 2025-11-13 at 22.02.34.png

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?