アグノのトレース
Agno は、エージェントアプリケーションを構築するためのオープンソースフレームワークです。ツールとポリシーをタスク指向のエージェントに構成することに重点を置いています。
MLflow Tracing は Agno と統合して、エージェント ワークフローとツール構成の包括的なトレースをキャプチャします。mlflow.agno.autolog
で有効にします。この統合により、以下に対するエンドツーエンドの可視性が提供されます。
- プロンプトと完了応答
- 待ち時間
- 関数名など、さまざまなエージェントに関するメタデータ
- トークンの使用状況とコスト
- キャッシュヒット
- 発生した場合の例外
前提 条件
Agnoで MLflow Tracing を使用するには、 MLflow と関連するAgnoパッケージをインストールする必要があります。
- Development
- Production
開発環境の場合は、Databricks エクストラと Agno を含む完全な MLflow パッケージをインストールします。
pip install --upgrade "mlflow[databricks]>=3.1" agno openai
完全な mlflow[databricks]
パッケージには、Databricks でのローカル開発と実験のためのすべての機能が含まれています。
本番運用デプロイの場合は、 mlflow-tracing
と Agno をインストールします。
pip install --upgrade mlflow-tracing agno openai
mlflow-tracing
パッケージは本番運用用に最適化されています。
最適なトレース エクスペリエンスを得るには、MLflow 3 をお勧めします。
例を実行する前に、環境を構成する必要があります。
Databricks ノートブック以外のユーザーの場合 : Databricks 環境変数を設定します。
export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
export DATABRICKS_TOKEN="your-personal-access-token"
Databricks ノートブック内のユーザーの場合 : これらの資格情報は自動的に設定されます。
基本的な例
import mlflow
mlflow.agno.autolog()
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.tools.yfinance import YFinanceTools
agent = Agent(
model=Claude(id="claude-sonnet-4-20250514"),
tools=[YFinanceTools(stock_price=True)],
instructions="Use tables to display data. Don't include any other text.",
markdown=True,
)
agent.print_response("What is the stock price of Apple?", stream=False)
通常どおり、Agno エージェントを実行します。トレースはエクスペリメントUIに表示されます。
マルチエージェントの例
import mlflow
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.models.openai import OpenAIChat
from agno.team.team import Team
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.reasoning import ReasoningTools
from agno.tools.yfinance import YFinanceTools
# Enable auto tracing for Agno
mlflow.agno.autolog()
web_agent = Agent(
name="Web Search Agent",
role="Handle web search requests and general research",
model=OpenAIChat(id="gpt-4.1"),
tools=[DuckDuckGoTools()],
instructions="Always include sources",
add_datetime_to_instructions=True,
)
finance_agent = Agent(
name="Finance Agent",
role="Handle financial data requests and market analysis",
model=OpenAIChat(id="gpt-4.1"),
tools=[
YFinanceTools(
stock_price=True,
stock_fundamentals=True,
analyst_recommendations=True,
company_info=True,
)
],
instructions=[
"Use tables to display stock prices, fundamentals (P/E, Market Cap), and recommendations.",
"Clearly state the company name and ticker symbol.",
"Focus on delivering actionable financial insights.",
],
add_datetime_to_instructions=True,
)
reasoning_finance_team = Team(
name="Reasoning Finance Team",
mode="coordinate",
model=Claude(id="claude-sonnet-4-20250514"),
members=[web_agent, finance_agent],
tools=[ReasoningTools(add_instructions=True)],
instructions=[
"Collaborate to provide comprehensive financial and investment insights",
"Consider both fundamental analysis and market sentiment",
"Use tables and charts to display data clearly and professionally",
"Present findings in a structured, easy-to-follow format",
"Only output the final consolidated analysis, not individual agent responses",
],
markdown=True,
show_members_responses=True,
enable_agentic_context=True,
add_datetime_to_instructions=True,
success_criteria="The team has provided a complete financial analysis with data, visualizations, risk assessment, and actionable investment recommendations supported by quantitative analysis and market research.",
)
reasoning_finance_team.print_response(
"""Compare the tech sector giants (AAPL, GOOGL, MSFT) performance:
1. Get financial data for all three companies
2. Analyze recent news affecting the tech sector
3. Calculate comparative metrics and correlations
4. Recommend portfolio allocation weights""",
show_full_reasoning=True,
)
トークン使用状況の追跡
MLflow は、 mlflow.chat.tokenUsage
属性への各エージェント呼び出しのトークン使用状況をログに記録します。トレース全体のトークン使用量の合計は、トレース情報オブジェクトの token_usage
フィールドで確認できます。
# Get the trace object just created
last_trace_id = mlflow.get_last_active_trace_id()
trace = mlflow.get_trace(trace_id=last_trace_id)
# Print the token usage
total_usage = trace.info.token_usage
print("== Total token usage: ==")
print(f" Input tokens: {total_usage['input_tokens']}")
print(f" Output tokens: {total_usage['output_tokens']}")
print(f" Total tokens: {total_usage['total_tokens']}")
# Print the token usage for each LLM call
print("\n== Detailed usage for each LLM call: ==")
for span in trace.data.spans:
if usage := span.get_attribute("mlflow.chat.tokenUsage"):
print(f"{span.name}:")
print(f" Input tokens: {usage['input_tokens']}")
print(f" Output tokens: {usage['output_tokens']}")
print(f" Total tokens: {usage['total_tokens']}")
自動トレースを無効にする
mlflow.agno.autolog(disable=True)
で無効にするか、mlflow.autolog(disable=True)
でグローバルに無効にします。