 一鍵追蹤 Agno Agent 全鏈路)
MLflow Agno 集成指南用 mlflow.agno.autolog() 一鍵追蹤 Agno Agent 全鏈路【免費(fèi)下載鏈接】mlflowThe open source AI engineering platform for agents, LLMs, and ML models. MLflow enables teams of all sizes to debug, evaluate, monitor, and optimize production-quality AI applications while controlling costs and managing access to models and data.項(xiàng)目地址: https://gitcode.com/GitHub_Trending/ml/mlflowMLflow 為 Agno一個(gè)用于編排 LLM、推理步驟、工具與記憶的 Agent 框架提供了原生自動(dòng)追蹤能力只需調(diào)用mlflow.agno.autolog()即可自動(dòng)捕獲 Agent 調(diào)用產(chǎn)生的 Trace 并記錄到當(dāng)前激活的 MLflow Experiment。本文以 API 參考文檔 mlflow.agno.rst 為核心結(jié)合源碼實(shí)現(xiàn)、官方集成文檔與測(cè)試用例講解該 API 的參數(shù)語義、自動(dòng)追蹤范圍、底層雙引擎V1 補(bǔ)丁式 / V2 OpenTelemetry原理以及單 Agent、多 Agent 協(xié)作場(chǎng)景下的實(shí)戰(zhàn)用法。一、API 一覽mlflow.agno.autolog 簽名與參數(shù)語義mlflow.agno模塊的公開入口只有一個(gè)函數(shù)autolog()定義于 mlflow/agno/init.py完整的參數(shù)語義如下參數(shù)類型默認(rèn)值作用log_tracesboolTrue是否記錄 Agno Agent 的 Trace設(shè)為False時(shí)只保留 autolog 集成注冊(cè)不產(chǎn)生追蹤數(shù)據(jù)disableboolFalse設(shè)為True時(shí)關(guān)閉 Agno autologging并反注冊(cè)已安裝的插樁silentboolFalse設(shè)為True時(shí)抑制 MLflow 的所有事件日志與警告函數(shù)體上還顯式標(biāo)注了autolog.integration_name agno見 mlflow/agno/init.py這是mlflow.autolog()全局入口識(shí)別該集成所需的關(guān)鍵標(biāo)記——因此用戶既可以直接調(diào)用mlflow.agno.autolog()也可以通過mlflow.autolog()統(tǒng)一開啟。值得注意的是源碼中_autolog()使用了autologging_integration(FLAVOR_NAME)裝飾器承載共享邏輯而真正的清理邏輯被刻意放在裝飾器包裹的入口之外。注釋說明mlflow/agno/init.py這樣設(shè)計(jì)的原因帶注解的包裝函數(shù)在disableTrue時(shí)不會(huì)執(zhí)行若把反注冊(cè)邏輯放進(jìn)裝飾函數(shù)內(nèi)將無法完成關(guān)閉時(shí)的清理。這是一個(gè)理解該 API 內(nèi)部實(shí)現(xiàn)時(shí)很關(guān)鍵的細(xì)節(jié)。二、快速開始三行代碼啟用 Agno 自動(dòng)追蹤官方集成文檔 agno.mdx 給出的啟用方式極為簡(jiǎn)潔import mlflow mlflow.agno.autolog()建議版本組合文檔示例環(huán)境pip install mlflow3.3 agno anthropic yfinance。若使用 Agno V2 2.0.0還需要額外的 OpenTelemetry 相關(guān)依賴源碼在缺少依賴時(shí)會(huì)拋出MlflowException并給出安裝提示見 mlflow/agno/autolog_v2.pypip install opentelemetry-exporter-otlp openinference-instrumentation-agno啟用后一個(gè)最簡(jiǎn)單的追蹤示例來自官方集成文檔from agno.agent import Agent from agno.models.anthropic import Claude from agno.tools.yfinance import YFinanceTools agent Agent( modelClaude(idclaude-sonnet-4-20250514), tools[YFinanceTools(stock_priceTrue)], instructionsUse tables to display data. Dont include any other text., markdownTrue, ) agent.print_response(What is the stock price of Apple?, streamFalse)自動(dòng)追蹤會(huì)捕獲每次 Agentic 調(diào)用的以下信息Prompt 與完成響應(yīng)completion responses各調(diào)用延遲Agent 元數(shù)據(jù)如函數(shù)名Token 用量與成本緩存命中情況調(diào)用過程中拋出的任何異常三、自動(dòng)追蹤覆蓋范圍Agent、Team、工具調(diào)用、記憶存儲(chǔ)與模型從mlflow.agno.autolog()的 V1 補(bǔ)丁清單mlflow/agno/init.py可以看出自動(dòng)追蹤覆蓋的類與方法class_map { agno.agent.Agent: [run, arun], agno.team.Team: [run, arun], agno.tools.function.FunctionCall: [execute, aexecute], }此外模塊還會(huì)動(dòng)態(tài)發(fā)現(xiàn)兩類對(duì)象并自動(dòng)注冊(cè)補(bǔ)丁存儲(chǔ)后端Storage通過discover_storage_backends()導(dǎo)入agno.storage下全部子模塊遞歸收集所有Storage子類并為其補(bǔ)丁create / read / upsert / drop / upgrade_schema方法見 mlflow/agno/utils.py模型子類Model通過find_model_subclasses()導(dǎo)入agno.models下全部子模塊遞歸收集所有Model子類并按 MRO 深度排序更具體的類先被打補(bǔ)丁為其補(bǔ)丁invoke / ainvoke方法見 mlflow/agno/utils.py。打補(bǔ)丁時(shí)源碼會(huì)先判斷原方法是否為協(xié)程函數(shù)inspect.iscoroutinefunction同步方法套用patched_class_call、異步方法套用patched_async_class_callmlflow/agno/init.py并通過safe_patch保證補(bǔ)丁可安全疊加與卸載。相應(yīng)地生成的 Span 類型由被調(diào)用實(shí)例決定見 mlflow/agno/autolog_v1.py實(shí)例類型Span 類型說明Agent/TeamAGENTAgent 或團(tuán)隊(duì)執(zhí)行入口run/arunFunctionCallTOOL工具函數(shù)調(diào)用execute/aexecuteStorageMEMORY記憶存儲(chǔ)讀寫create/read/upsert等ModelLLM底層模型調(diào)用invoke/ainvoke其他UNKNOWN兜底從源碼結(jié)構(gòu)可以推斷Agent.run調(diào)用會(huì)層層觸發(fā)其內(nèi)部的Model.invoke與FunctionCall.execute因此一次運(yùn)行通常會(huì)展開為一條包含 AGENT → LLM → TOOL 多層子 Span 的完整 Trace這與測(cè)試test_run_simple_autolog中斷言一次Agent.run產(chǎn)生 2 個(gè) SpanAgent.runClaude.invoke的行為完全一致見 tests/agno/test_agno_tracing.py。四、Span 內(nèi)容細(xì)節(jié)輸入輸出、Agent 屬性與 Token 用量對(duì)于Agent/Team的調(diào)用_set_span_inputs_attributes會(huì)將實(shí)例的__dict__寫入 Span 屬性其中tools會(huì)通過model_dumps(exclude_noneTrue)序列化為結(jié)構(gòu)化 JSON見 mlflow/agno/autolog_v1.py并把run方法中非None的入?yún)懭?Span inputs——因?yàn)锳gent.run有大量可選參數(shù)過濾掉None可以避免噪聲mlflow/agno/autolog_v1.py。Span 命名遵循以下規(guī)則mlflow/agno/autolog_v1.py工具調(diào)用優(yōu)先使用FunctionCall上的function_name/name/tool_name屬性取不到時(shí)回退到底層函數(shù)的name/__name__仍無則命名為AgnoToolCall其他實(shí)例統(tǒng)一命名為{ClassName}.{method_name}例如Agent.run、Claude.invoke。輸出側(cè)RunResponse/TeamRunResponse會(huì)被轉(zhuǎn)換為to_dict()寫入 Span outputs并從result.metrics或session_metrics中聚合input_tokens、output_tokens、total_tokens以SpanAttributeKey.CHAT_USAGE屬性掛到 Span 上mlflow/agno/autolog_v1.py。這就是 MLflow 能夠展示 Token 用量與成本趨勢(shì)的數(shù)據(jù)來源。五、底層雙引擎V1 補(bǔ)丁式追蹤與 V2 OpenTelemetry 插樁mlflow.agno.autolog()會(huì)根據(jù)已安裝的 Agno 版本自動(dòng)選擇實(shí)現(xiàn)路徑mlflow/agno/init.py5.1 Agno V1 2.0.0基于 safe_patch 的補(bǔ)丁式追蹤V1 路徑實(shí)現(xiàn)在 mlflow/agno/autolog_v1.py。核心邏輯是with mlflow.start_span(namespan_name, span_typespan_type) as span: raw_inputs construct_full_inputs(original, self, *args, **kwargs) _set_span_inputs_attributes(span, self, raw_inputs) result original(self, *args, **kwargs) _set_span_outputs(span, result) return result同步與異步版本patched_class_call/patched_async_class_call結(jié)構(gòu)相同區(qū)別僅在await原始方法。異常在with塊內(nèi)拋出時(shí)MLflow 會(huì)自動(dòng)將 Span/Trace 標(biāo)記為ERROR狀態(tài)測(cè)試test_run_failure_tracing驗(yàn)證了失敗場(chǎng)景下SpanStatusCode.ERROR與錯(cuò)誤描述ModelProviderError: bang的落盤tests/agno/test_agno_tracing.py。5.2 Agno V2 2.0.0OpenInference 插樁 MLflow 上下文橋接V2 路徑實(shí)現(xiàn)在 mlflow/agno/autolog_v2.py。Agno V2 自身通過openinference.instrumentation.agno.AgnoInstrumentor導(dǎo)出 OpenTelemetry SpanMLflow 的做法是自定義_MlflowTracerProvider把get_tracer()替換為委托給 MLflow 的_get_tracer()mlflow/agno/autolog_v2.py用_MlflowContextBridgingTracer包裝返回的 Tracer其start_span/start_as_current_span通過_bridge_parent_context把 Agno 的 OpenInference Span 掛到當(dāng)前激活的 MLflow Span 之下mlflow/agno/autolog_v2.py。_bridge_parent_context處理了一個(gè)易踩坑的場(chǎng)景OpenInference 在頂層 Agno Team 上會(huì)傳入一個(gè)包裹INVALID_SPAN的 context 以強(qiáng)制創(chuàng)建根 Span。該函數(shù)會(huì)識(shí)別這種無效父級(jí)并改橋接到 MLflow 的當(dāng)前上下文從而保證 Agno 產(chǎn)生的 Span 與手工創(chuàng)建的mlflow.start_span()合并為同一條 Trace而不是各自獨(dú)立的 Trace。測(cè)試test_v2_spans_nest_under_manual_mlflow_span與test_v2_invalid_span_context_still_nests_under_manual_mlflow_span對(duì)此做了專門驗(yàn)證tests/agno/test_agno_tracing.py。5.3 關(guān)閉時(shí)的對(duì)稱清理disableTrue或log_tracesFalse時(shí)V2 路徑會(huì)調(diào)用_uninstrument_otel()反注冊(cè) AgnoInstrumentormlflow/agno/autolog_v2.pyV1 路徑則借助safe_patch的卸載能力移除全部補(bǔ)丁。因此該 API 支持在運(yùn)行期安全地反復(fù)開啟/關(guān)閉測(cè)試test_run_simple_autolog末尾即驗(yàn)證了autolog(disableTrue)后再次運(yùn)行 Agent 不再產(chǎn)生 Tracetests/agno/test_agno_tracing.py。六、多 AgentAgents to Agents協(xié)作追蹤官方集成文檔專門介紹了對(duì) Agno非流式端點(diǎn)的多 Agent 協(xié)作追蹤MLflow 會(huì)自動(dòng)記錄 Agent 之間的每一次 handoff交接、交互消息以及所用工具/函數(shù)的細(xì)節(jié)輸入、輸出、耗時(shí)便于排查問題、度量性能與復(fù)現(xiàn)結(jié)果。一個(gè)完整的 Team 協(xié)作示例來自 agno.mdximport 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( nameWeb Search Agent, roleHandle web search requests and general research, modelOpenAIChat(idgpt-4.1), tools[DuckDuckGoTools()], instructionsAlways include sources, add_datetime_to_instructionsTrue, ) finance_agent Agent( nameFinance Agent, roleHandle financial data requests and market analysis, modelOpenAIChat(idgpt-4.1), tools[ YFinanceTools( stock_priceTrue, stock_fundamentalsTrue, analyst_recommendationsTrue, company_infoTrue, ) ], 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_instructionsTrue, ) reasoning_finance_team Team( nameReasoning Finance Team, modecoordinate, modelClaude(idclaude-sonnet-4-20250514), members[web_agent, finance_agent], tools[ReasoningTools(add_instructionsTrue)], 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, ], markdownTrue, show_members_responsesTrue, enable_agentic_contextTrue, add_datetime_to_instructionsTrue, success_criteriaThe 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_reasoningTrue, )Team與成員Agent均在追蹤范圍內(nèi)V1 下Team.run/arun被打補(bǔ)丁V2 下 OpenInference 原生覆蓋因此上述一次print_response會(huì)生成覆蓋 Team 編排、成員 Agent、工具調(diào)用與模型調(diào)用的完整嵌套 Trace。七、Token 用量與成本追蹤MLflow 會(huì)自動(dòng)為 Agno 記錄每次 LLM 調(diào)用的 Token 用量與成本每個(gè) Trace/Span 上都會(huì)記錄 Token 用量input_tokens/output_tokens/total_tokens見 mlflow/agno/autolog_v1.py內(nèi)置 Dashboard 會(huì)展示聚合后的成本與耗時(shí)趨勢(shì)如需以編程方式讀取這些數(shù)據(jù)官方文檔指向 Token Usage and Cost Tracking 主題對(duì)應(yīng) docs/docs/genai/tracing/ 下的專題文檔。測(cè)試中test_run_simple_autolog斷言了traces[0].info.token_usage與 LLM 返回的Usage(input_tokens5, output_tokens7, total_tokens12)完全一致tests/agno/test_agno_tracing.py可作為該行為的驗(yàn)證依據(jù)。八、關(guān)閉自動(dòng)追蹤與其他 MLflow 集成一致可通過兩種方式全局關(guān)閉 Agno 自動(dòng)追蹤見 agno.mdxmlflow.agno.autolog(disableTrue) # 或 mlflow.autolog(disableTrue)源碼層面disableTrue時(shí)會(huì)同時(shí)執(zhí)行_autolog的清理與V2 下_uninstrument_otel()的反注冊(cè)確保后續(xù) Agno 調(diào)用不再產(chǎn)生任何 Trace。九、補(bǔ)充說明與適用前提版本前提V1 補(bǔ)丁式追蹤適用于 Agno 2.0.0Agno 2.0.0自動(dòng)切換為 OpenTelemetry/OpenInference 插樁并需要opentelemetry-exporter-otlp與openinference-instrumentation-agno兩個(gè)額外包。流式支持V1 路徑下源碼注釋明確標(biāo)注# TODO: Support streamingmlflow/agno/init.py因此 V1 的模型子類目前僅對(duì)invoke/ainvoke打補(bǔ)丁多 Agent 追蹤場(chǎng)景官方文檔也說明針對(duì)的是非流式端點(diǎn)。錯(cuò)誤處理差異Agno 2.3.14起模型錯(cuò)誤會(huì)被 Agno 內(nèi)部捕獲并以錯(cuò)誤狀態(tài)返回而非拋出異常這會(huì)影響失敗 Span 的生成方式見 tests/agno/test_agno_tracing.py 與test_v2_failure_creates_spans。上下文橋接在 Agno V2 下若外層已有手工創(chuàng)建的mlflow.start_span()Agno 自動(dòng)產(chǎn)生的 Span 會(huì)嵌套其下合并為單條 Trace無論 MLflow 的 tracer provider 處于 isolated 還是 unified 模式MLFLOW_USE_DEFAULT_TRACER_PROVIDER該行為均被測(cè)試覆蓋tests/agno/test_agno_tracing.py。相關(guān)代碼與文檔索引API 參考docs/api_reference/source/python_api/mlflow.agno.rst集成指南docs/docs/genai/tracing/integrations/listing/agno.mdx入口實(shí)現(xiàn)mlflow/agno/init.pyV1 補(bǔ)丁邏輯mlflow/agno/autolog_v1.pyV2 OpenTelemetry 插樁mlflow/agno/autolog_v2.py存儲(chǔ)/模型動(dòng)態(tài)發(fā)現(xiàn)mlflow/agno/utils.py測(cè)試用例tests/agno/test_agno_tracing.py【免費(fèi)下載鏈接】mlflowThe open source AI engineering platform for agents, LLMs, and ML models. MLflow enables teams of all sizes to debug, evaluate, monitor, and optimize production-quality AI applications while controlling costs and managing access to models and data.項(xiàng)目地址: https://gitcode.com/GitHub_Trending/ml/mlflow創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考