メインコンテンツまでスキップ

トレーシングストランドエージェントSDK

Strands Agents SDKは、外部ツールやAPIsと対話できる自律AIエージェントを作成するためのAWSのオープンソースSDKです。

MLflow Tracing は、 Strands Agents SDK に自動トレース機能を提供します。mlflow.strands.autolog関数を呼び出してストランドの自動トレースを有効にすると、エージェントの呼び出し時にMLflowトレースをキャプチャし、アクティブなMLflowエクスペリメントに記録します。

Python
import mlflow

mlflow.strands.autolog()

MLflow トレースは、Strands エージェントの呼び出しに関する次の情報を自動的にキャプチャします。

  • プロンプトと応答
  • レイテンシー
  • エージェントのメタデータ
  • トークンの使用とコスト
  • キャッシュヒット情報
  • 発生した例外
注記

サーバレス コンピュート クラスターでは、自動ログは自動的に有効になりません。 この統合の自動トレースを有効にするには、 mlflow.strands.autolog()明示的に呼び出す必要があります。

前提条件

Strands Agents SDK で MLflow Tracing を使用するには、MLflow、Strands SDK、および必要な依存関係をインストールする必要があります。

開発環境では、Databricks エクストラと Strands パッケージを含む完全な MLflow パッケージをインストールします。

Bash
pip install --upgrade "mlflow[databricks]>=3.1" strands strands-tools

完全な mlflow[databricks] パッケージには、Databricks でのローカル開発と実験のためのすべての機能が含まれています。

注記

Strands Agents SDK で最高のトレース エクスペリエンスを得るには、MLflow 3 を強くお勧めします。

例を実行する前に、環境を設定する必要があります。

Databricks ノートブックの外部のユーザーの場合 : Databricks 環境変数を設定します。

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

Databricks ノートブック内のユーザーの場合 : これらの資格情報は自動的に設定されます。

API キー : LLM プロバイダーの API キーが設定されていることを確認します。本番運用環境の場合は、安全なAPIキー管理のために、ハードコードされた値の代わりにMosaic AI GatewayまたはDatabricksシークレットを使用します。

Bash
export OPENAI_API_KEY="your-openai-api-key"
# Add other provider keys as needed

使用例

次の例は、Strands Agents SDK を MLflow トレースとともに使用する方法を示しています。エージェントは OpenAI モデルを使用し、算術演算を実行するための計算ツールにアクセスできます。

Python
import mlflow
import os

# Ensure your OPENAI_API_KEY is set in your environment
# os.environ["OPENAI_API_KEY"] = "your-openai-api-key" # Uncomment and set if not globally configured

# Enable auto tracing for Strands Agents SDK
mlflow.strands.autolog()

# Set up MLflow tracking to Databricks
mlflow.set_tracking_uri("databricks")
mlflow.set_experiment("/Shared/strands-agent-demo")

from strands import Agent
from strands.models.openai import OpenAIModel
from strands_tools import calculator

# Configure the OpenAI model
model = OpenAIModel(
client_args={"api_key": os.environ.get("OPENAI_API_KEY")},
model_id="gpt-4o",
params={
"max_tokens": 2000,
"temperature": 0.7,
},
)

# Create an agent with the calculator tool
agent = Agent(model=model, tools=[calculator])

# Run the agent
response = agent("What is 2+2?")
print(response)
警告

本番運用環境の場合は、安全なAPIキー管理のために、ハードコードされた値の代わりにMosaic AI GatewayまたはDatabricksシークレットを使用します。

トークン使用状況の追跡

MLflow バージョン 3.4.0 を使用すると、MLflow は Strands エージェントのトークンの使用状況を自動的に追跡します。またはそれ以降。VPN 使用状況情報には、エージェントの実行中に消費された入力ウイルス、出力ウイルス、および合計消費ウイルスが含まれます。

Python
import mlflow

mlflow.strands.autolog()

from strands import Agent
from strands.models.openai import OpenAIModel
from strands_tools import calculator

model = OpenAIModel(
client_args={"api_key": os.environ.get("OPENAI_API_KEY")},
model_id="gpt-4o",
params={
"max_tokens": 2000,
"temperature": 0.7,
},
)

agent = Agent(model=model, tools=[calculator])

# Run the agent and retrieve trace information
with mlflow.start_span(name="strands_agent_run") as span:
response = agent("Calculate the sum of 15 and 27")
print(response)

# Token usage is automatically logged and visible in the MLflow UI
trace_info = mlflow.get_last_active_trace()
print(f"Trace ID: {trace_info.request_id}")

トークンの使用状況の詳細は MLflow トレース UI に表示されるため、エージェントのパフォーマンスとコストを監視して最適化できます。

自動トレースを無効にする

Strands Agents SDK の自動トレースは、 mlflow.strands.autolog(disable=True)またはmlflow.autolog(disable=True)を呼び出すことによってグローバルに無効にすることができます。