用 Agno PerformanceEval 对比六大 Agent 框架实例化性能跨框架基准测试实战指南【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno本篇技术指南基于 Agno 仓库cookbook/09_evals/performance/comparison/目录下的跨框架基准套件展开讲解如何借助 Agno 内置的PerformanceEval评测 API在完全一致的条件下测量 AutoGen、CrewAI、LangGraph、OpenAI Agents SDK、PydanticAI 与 Smolagents 六个非 Agno 框架的 Agent 实例化耗时与内存峰值。读完本篇你将掌握这套基准的完整写法、每个框架实例化代码的差异点、底层测量实现原理以及如何在本地复跑并正确解读结果。一、这份比较基准在做什么cookbook/09_evals/performance/comparison/README.md开宗明义地定义了本目录的定位These benchmarks compare agent instantiation patterns across non-Agno frameworks.即这是一组针对“非 Agno 框架”的 Agent 实例化模式对比基准。与 Agno 官方 cookbook 中其他“评测某个模型/工具/记忆功能”的示例不同这里评测的对象是对象构造instantiation这一单一步骤横向比较的是六个主流 Agent 框架在“创建一个带工具调用的 Agent 对象”这一操作上的开销。目录下共包含 8 个文件autogen_instantiation.py— AutoGen assistant 实例化基准crewai_instantiation.py— CrewAI agent 实例化基准langgraph_instantiation.py— LangGraph react-agent 实例化基准openai_agents_instantiation.py— OpenAI Agents SDK 实例化基准pydantic_ai_instantiation.py— PydanticAI agent 实例化基准smolagents_instantiation.py— Smolagents tool-calling agent 实例化基准README.md— 本目录索引TEST_LOG.md— 各基准文件的测试执行状态记录。TEST_LOG.md为六个基准文件分别建立了条目当前状态均为PENDING“Tests not yet run. Run each file and update this log.”说明这是一套待执行的基准脚手架每个文件都已写好可独立运行的评测逻辑等待在实际环境中运行并回填结果。因此本篇的重心放在「如何用这套脚手架进行测量」与「测量背后的实现原理」上而不是引用任何未经实测的数字。与cookbook/performance/套件的关系需要区分两组易混淆的资源cookbook/09_evals/performance/comparison/本文主角面向09_evals 评测 cookbook 体系六条基准各自独立成文件直接实例化各框架的真实 Agent 对象含真实模型客户端配置并通过PerformanceEval的run(print_resultsTrue, print_summaryTrue)打印明细表与汇总表cookbook/performance/配套性能套件面向框架自身开销追踪全部使用进程内 mock 模型替换 provider测量结果写入baselines/并生成 HTML 报告其README.md中给出了已提交的参考测量结果见本文第七节。两者的测量对象不同前者测“真实构造 框架原生 API”后者测“mock 模型下的框架净开销”。阅读任何对比数据前先确认数据来自哪一套是避免误读的前提。二、统一的基准骨架同一把尺子量六个框架六个脚本虽然框架各异但结构完全同构都遵循「工具定义 → 实例化工厂 → 评测对象 → 运行入口」四段式模板# 1) 定义统一的基准工具 def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) # 2) 定义被测函数框架特定的 Agent 构造 def instantiate_agent(): return SomeFrameworkAgent(...) # 各框架写法不同 # 3) 创建评测对象 bench PerformanceEval(funcinstantiate_agent, num_iterations1000) # 4) 运行 if __name__ __main__: bench.run(print_resultsTrue, print_summaryTrue)这套同构设计的价值在于可对比性工具语义一致六个基准使用同一个get_weather输入域固定为Literal[nyc, sf]两个城市返回同样的两句话。唯一允许的差异是各框架声明工具的方式装饰器、子类、函数包装因为那正是框架原生 API 的一部分迭代次数一致全部使用num_iterations1000保证采样规模相同被测对象一致都只测「创建一个带有该工具的 Agent」这一动作不测运行、不测对话、不测推理入口一致均通过if __name__ __main__独立运行输出方式统一为print_resultsTrue, print_summaryTrue。这种“保留框架原生差异、固定测量维度”的做法使每个脚本可以独立阅读、独立运行、独立解释又能在汇总时进行横向比较。三、PerformanceEval源码级解析所有基准的核心都是 libs/agno/agno/eval/performance.py 中定义的PerformanceEval。它负责对一个任意Callable同时测量运行时间与峰值内存并输出完整的统计摘要。3.1 关键字段与默认值字段默认值作用func必填被测函数六个基准传入instantiate_agentmeasure_runtimeTrue是否测量耗时measure_memoryTrue是否测量内存nameNone评测名称用于落盘与入库标识warmup_runs10预热次数不计入最终统计num_iterations50正式测量迭代次数本套件覆盖为 1000memory_growth_trackingFalse开启后对比相邻迭代的内存快照top_n_memory_allocations5调试模式下追踪的内存增长热点数量file_path_to_save_resultsNone结果落盘路径支持{name}、{run_id}占位符debug_mode环境变量AGNO_DEBUG开启后输出逐次运行与内存快照对比日志dbNone传入数据库后把结果写入 Agno 平台telemetryTrue记录最小化遥测用于改进 Evals字段完整定义见 performance.py。3.2run()的七步执行管线run()方法源码按固定顺序执行预热先执行warmup_runs默认 10次无测量运行避免首次执行的懒加载、缓存预热等开销污染样本测耗时循环num_iterations次用Timer包裹self.func()_measure_time记录每次 elapsed测内存先通过_compute_tracemalloc_baseline()用空函数采样 3 次得到稳定的内存基线源码再逐次调用tracemalloc.start()→self.func()→ 读取get_traced_memory()的peak值减去基线后换算为 MiB_measure_memory。gc.collect()在每次测量前执行减少垃圾回收噪声汇总构造PerformanceResult(run_id, run_times, memory_usages)并挂到self.result落盘若设置了file_path_to_save_results调用store_result_in_file保存打印按参数输出「Individual Runs」明细表与「Performance Summary」汇总表均为 Rich 表格入库与遥测若设置了db以EvalType.PERFORMANCE类型调用log_eval_run写入评测记录最后发送最小化遥测。3.3PerformanceResult比均值更稳健的统计口径PerformanceResult源码对耗时与内存各计算一组统计量avg / min / max / std_dev / median / p95。其中两点值得注意p95 的算法使用statistics.quantiles(data, n100, methodinclusive)的第 95 分位保证小样本下分位数仍落在观测范围内源码排序后计算compute_stats()先对数据排序再计算各统计量确保中位数与分位数口径一致。选择 median 与 p95 而非均值是因为运行时分布常带长尾GC 停顿、系统调度均值容易被极值拉偏——这也是本节基准与配套套件方法论一致的原因。3.4 异步变体arun()PerformanceEval同时提供arun()源码以支持异步被测函数。它会先校验asyncio.iscoroutinefunction(self.func)若不是协程函数则抛出ValueError提示同步函数应使用run()。若传入AsyncBaseDbrun()也会主动报错并引导使用arun()。六个比较基准均为同步函数使用run()即可。四、六个框架的实例化基准逐一解读4.1 AutoGenautogen_instantiation.pyfrom typing import Literal from agno.eval.performance import PerformanceEval from autogen_agentchat.agents import AssistantAgent from autogen_ext.models.openai import OpenAIChatCompletionClient def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) tools [get_weather] def instantiate_agent(): return AssistantAgent( nameassistant, model_clientOpenAIChatCompletionClient( modelgpt-5.6-luna, model_info{ vision: False, function_calling: True, json_output: False, family: gpt-5.6-luna, structured_output: True, }, ), toolstools, ) autogen_instantiation PerformanceEval(funcinstantiate_agent, num_iterations1000) if __name__ __main__: autogen_instantiation.run(print_resultsTrue, print_summaryTrue)要点AutoGen 的构造拆成两步——OpenAIChatCompletionClient负责模型客户端含model_info能力声明供框架判断 function calling / structured output 支持AssistantAgent消费该客户端与工具列表。model字段为脚本中配置的模型标识可按实际部署环境替换。4.2 CrewAIcrewai_instantiation.pyfrom typing import Literal from agno.eval.performance import PerformanceEval from crewai.agent import Agent from crewai.tools import tool tool(Tool Name) def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) tools [get_weather] def instantiate_agent(): return Agent( llmgpt-5.6-luna, roleTest Agent, goalBe concise, reply with one sentence., toolstools, backstoryTest, ) crew_instantiation PerformanceEval(funcinstantiate_agent, num_iterations1000) if __name__ __main__: crew_instantiation.run(print_resultsTrue, print_summaryTrue)要点CrewAI 通过tool(Tool Name)装饰器把普通函数升级为工具Agent构造时需同时提供role、goal、backstory等角色要素llm直接以字符串形式指定模型。相比其他框架CrewAI 构造参数最多、语义层最厚这也是衡量其实例化开销时需要关注的框架特性。4.3 LangGraphlanggraph_instantiation.pyfrom typing import Literal from agno.eval.performance import PerformanceEval from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent tool def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) tools [get_weather] def instantiate_agent(): return create_react_agent(modelChatOpenAI(modelgpt-5.6-luna), toolstools) langgraph_instantiation PerformanceEval(funcinstantiate_agent, num_iterations1000) if __name__ __main__: langgraph_instantiation.run(print_resultsTrue, print_summaryTrue)要点LangGraph 使用langgraph.prebuilt的create_react_agent工厂函数构造 react-agent模型侧用ChatOpenAIlangchain-openai封装。注意被测动作是整张反应图react graph的创建这正是 LangGraph 实例化开销的主要来源。4.4 OpenAI Agents SDKopenai_agents_instantiation.pyfrom typing import Literal from agno.eval.performance import PerformanceEval try: from agents import Agent, function_tool except ImportError: raise ImportError( OpenAI agents not installed. Please install it using uv pip install openai-agents. ) def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) def instantiate_agent(): return Agent( nameHaiku agent, instructionsAlways respond in haiku form, modelo3-mini, tools[function_tool(get_weather)], ) openai_agents_instantiation PerformanceEval( funcinstantiate_agent, num_iterations1000 ) if __name__ __main__: openai_agents_instantiation.run(print_resultsTrue, print_summaryTrue)要点这是六个脚本中唯一带依赖缺失引导的用try/except ImportError包裹导入缺包时给出明确的安装指令uv pip install openai-agents。这一模式值得在其他基准中推广——基准的可复跑性依赖依赖安装提示。构造上通过function_tool(get_weather)包装工具Agent同时携带name、instructions、model。4.5 PydanticAIpydantic_ai_instantiation.pyfrom typing import Literal from agno.eval.performance import PerformanceEval from pydantic_ai import Agent def instantiate_agent(): agent Agent( openai:gpt-5.6-luna, system_promptBe concise, reply with one sentence. ) # Tool definition remains scoped to agent construction by design. agent.tool_plain def get_weather(city: Literal[nyc, sf]): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) return agent pydantic_instantiation PerformanceEval(funcinstantiate_agent, num_iterations1000) if __name__ __main__: pydantic_instantiation.run(print_resultsTrue, print_summaryTrue)要点PydanticAI 的工具注册方式与其他框架不同——工具通过agent.tool_plain绑定在 agent 实例上因此get_weather必须定义在instantiate_agent函数内部“Tool definition remains scoped to agent construction by design”随 Agent 构造一起完成。也就是说PydanticAI 这一基准测的构造开销天然包含了工具注册本身这是阅读对比结果时需要注意的口径差异。4.6 Smolagentssmolagents_instantiation.pyfrom agno.eval.performance import PerformanceEval from smolagents import InferenceClientModel, Tool, ToolCallingAgent class WeatherTool(Tool): name weather_tool description This is a tool that tells the weather inputs { city: { type: string, description: The city to look up, } } output_type string def forward(self, city: str): Use this to get weather information. if city nyc: return It might be cloudy in nyc elif city sf: return Its always sunny in sf else: raise AssertionError(Unknown city) def instantiate_agent(): return ToolCallingAgent( tools[WeatherTool()], modelInferenceClientModel(model_idmeta-llama/Llama-3.3-70B-Instruct), ) smolagents_instantiation PerformanceEval(funcinstantiate_agent, num_iterations1000) if __name__ __main__: smolagents_instantiation.run(print_resultsTrue, print_summaryTrue)要点Smolagents 要求工具以Tool子类形式声明通过类属性声明name、description、inputsJSON Schema 风格与output_type实际逻辑写在forward()中。ToolCallingAgent配合InferenceClientModel此处模型为meta-llama/Llama-3.3-70B-Instruct完成构造。WeatherTool()的实例化发生在instantiate_agent()内因此也计入被测开销。五、运行基准与解读结果5.1 运行方式六个脚本均为独立可执行文件无需统一入口。确保agno已安装PerformanceEval位于 libs/agno/agno/eval/performance.py并分别安装被测框架后直接运行python cookbook/09_evals/performance/comparison/autogen_instantiation.py python cookbook/09_evals/performance/comparison/crewai_instantiation.py python cookbook/09_evals/performance/comparison/langgraph_instantiation.py python cookbook/09_evals/performance/comparison/openai_agents_instantiation.py python cookbook/09_evals/performance/comparison/pydantic_ai_instantiation.py python cookbook/09_evals/performance/comparison/smolagents_instantiation.pyOpenAI Agents SDK 若未安装脚本会给出提示uv pip install openai-agents。运行会先执行 10 次预热不计入统计随后进行 1000 次耗时测量与 1000 次内存测量。由于默认show_spinnerTrue终端会渲染进度状态完成后先打印「Individual Runs」明细表每次运行的耗时/内存再打印「Performance Summary」汇总表MetricTime (seconds)Memory (MiB)Average / Minimum / Maximum / Std Dev / Median / 95th %ile逐项统计逐项统计汇总表的具体渲染逻辑见 print_summary。建议重点看 Median 与 95th %ile而非 Average——GC 停顿等长尾事件会抬高均值。5.2 调试与追踪设置AGNO_DEBUGtrue可开启debug_mode逐次打印耗时与内存含原始峰值与基线调整值并在开启memory_growth_tracking时输出 Top-N 内存增长来源按行号对比 tracemalloc 快照见_compare_memory_snapshots需要把结果归档时可设置file_path_to_save_results支持{name}、{run_id}占位符需要把结果写入 Agno 平台时传入db参数run()会以EvalType.PERFORMANCE类型记录见 run 的入库步骤。5.3 正确复跑的前提空闲机器CPU 争抢会直接扭曲微秒级耗时样本运行期间应避免其他负载进程隔离每个基准应在独立 Python 进程中运行脚本天然满足避免继承前一个框架的导入缓存与分配器状态同一环境绝对数值随机器与已装包数量变化横向对比应始终在同一个环境内进行。六、公平对比的方法论要点把这六条基准放在一起比较时cookbook/performance/README.md 中沉淀的方法论同样适用同工具、同语义统一get_weather、统一Literal[nyc, sf]输入域、统一 1000 次迭代——只允许框架原生 API 差异避免把“工具不同”混入结果预热必须排除PerformanceEval默认 10 次预热不计入统计避免懒加载、首次导入、缓存预热污染样本统计用分位数报告 median/p95 而非均值GC 停顿导致的右尾不会主导结论注意各框架的“单位”不同如 4.5 节所述PydanticAI 的工具注册内嵌在构造中CrewAI 的构造参数更重LangGraph 构造的是整张图。六条基准都忠实于框架的惯用构造方式因此比较的是“各框架惯用实例化路径”的整体开销而非剔除框架特性的同构操作正确性断言本套件的被测对象是构造若在构造后追加断言如工具可调用、schema 可生成能进一步防止“测了个错误路径”的情况——这正是配套套件cookbook/performance/在 run 类基准中强制 assert 的原因。七、配套套件的参考数据带边界说明cookbook/09_evals/performance/comparison/本身尚未回填实测结果TEST_LOG.md中六项均为 PENDING。如需量级参考可查阅配套套件 cookbook/performance/README.md 中已提交的参考测量——该表于 2026-08-22 在 Apple M4 Max、Python 3.12 环境测得四个框架同装于一个由perf_setup.sh创建的环境报告值为单次顺序运行的中位数MetricAgnoLangGraphPydanticAICrewAISingle-turn run (mocked model)65 us303 us (4.6x)1,580 us (24x)4,439 us (68x)Tool-call run (mocked model)327 us787 us (2.4x)2,394 us (7.3x)excluded5-turn conversation, in-memory1.0 ms3.5 ms (3.4x)8.0 ms (7.9x)19.0 ms (19x)25-turn conversation, in-memory12.2 ms22.3 ms (1.8x)39.2 ms (3.2x)92.9 ms (7.6x)25-turn conversation, durable (SQLite)42.2 ms36.5 ms (0.9x)excludedexcludedAgent construction (1 tool)4.7 us1,256 us (269x)9,546 us (2,046x)19,101 us (4,094x)Construction memory peak7.1 KiB146 KiB (21x)39 KiB (5.6x)24 KiB (3.3x)Cold import147 ms313 ms (2.1x)222 ms (1.5x)1,031 ms (7.0x)引用这张表时务必注意三点边界环境相关绝对数值随机器与已装包数量变化倍率multiplier的可迁移性远高于绝对值口径不同上表来自 mock 模型驱动的配套套件与本文六条“真实构造”基准的测量边界不同不能混为同一组数字版本相关表格注明 LangGraph 1.2.11、PydanticAI 2.31.1slim install、CrewAI 1.15.17升级框架版本后需重测。八、延伸阅读基准索引与定位comparison/README.md六个基准实现autogen_instantiation.py、crewai_instantiation.py、langgraph_instantiation.py、openai_agents_instantiation.py、pydantic_ai_instantiation.py、smolagents_instantiation.py执行状态记录TEST_LOG.mdPerformanceEval源码libs/agno/agno/eval/performance.py09_evals 性能评测目录总览cookbook/09_evals/performance/README.md配套性能套件mock 模型、基线、报告cookbook/performance/README.md如果需要进一步深入可沿着EvalType.PERFORMANCE的入库链路agno/db/schemas/evals.py与评测工具函数agno/eval/utils.py继续阅读理解评测结果如何从内存中的统计量沉淀为可审计的持久化记录。【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考