FEATURED · 精选文章

LiteLLM LLM-Bench:跨模型基准测试响应质量、时延与成本的实战指南

发布时间 / 2026/9/6 23:05:55
来源 / 创域科博编辑部
栏目 / 资讯中心
LiteLLM LLM-Bench:跨模型基准测试响应质量、时延与成本的实战指南 LiteLLM LLM-Bench跨模型基准测试响应质量、时延与成本的实战指南【免费下载链接】litellmThe fastest, litest AI Gateway. Rust core with Python SDK. Call 100 LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]项目地址: https://gitcode.com/GitHub_Trending/li/litellmLiteLLM 仓库内置了一套名为 LLM-Bench 的基准测试工具位于 cookbook/benchmark用于在同一组问题下统一对比多个 LLM 的**响应内容、响应时间Response Time和调用成本Cost**三个维度。读完本文你将掌握如何配置并运行这套基准测试脚本、如何解读其表格化输出结果以及理解其底层成本计算依赖的 cost_calculator.py 与模型价格映射表 model_prices_and_context_window.json 的工作机制。一、LLM-Bench 是什么一次调用对比三个维度传统上对比多个模型需要分别对接各供应商 SDK、各自计时、各自查价。LiteLLM 的核心价值在于把 100 家 LLM API 统一成 OpenAI 风格的completion()接口因此 LLM-Bench 只需要一个 benchmark.py 脚本即可完成三类指标采集指标采集方式来源响应内容 Responsecompletion()返回的choices[0].message.content模型 API 实时返回响应时间 Response Time (seconds)请求前后time.time()差值本地计时成本 Cost ($)completion_cost(completion_responseresponse)LiteLLM 内置价格映射表 响应中的 token 用量脚本运行结束后会用tabulate库按问题分组输出一张网格表格grid 格式列宽固定为[15, 80, 15, 10]便于在终端中直接横向对比各模型表现。二、环境准备2.1 获取代码并进入基准测试目录git clone https://github.com/BerriAI/litellm cd litellm/cookbook/benchmark2.2 安装依赖pip install litellm click tqdm tabulate termcolor四个依赖各司其职与 benchmark.py 的 import 一一对应litellm提供统一的completion与completion_cost接口click提供命令行入口及--system-prompt选项tqdm逐模型显示进度条tabulate生成最终的网格结果表格termcolor为终端输出着色模型名浅蓝、耗时红色、成本绿色。三、配置基准测试模型、API Key 与问题集核心配置全部集中在 benchmark.py 文件顶部按当前仓库中的实际源码需要修改四处# 1. 定义要基准测试的模型列表 # 可任选 LiteLLM 支持的 LLM模型命名规则参见其官方 providers 文档 models [gpt-3.5-turbo, claude-2] # 2. 填入各供应商的 API Key os.environ[OPENAI_API_KEY] os.environ[ANTHROPIC_API_KEY] # 3. 基准测试的问题列表替换成你自己的问题 questions [When will BerriAI IPO?, When will LiteLLM hit $100M ARR?] # 4. 系统提示词也可通过命令行 --system-prompt 覆盖 system_prompt You are LiteLLMs helpful assistant 配置要点说明models 列表模型名使用 LiteLLM 统一命名如openrouter/meta-llama/llama-2-70b-chat、ollama/llama2、togethercomputer/llama-2-70b-chat列表越长对比面越广API Key按所选模型所属供应商填写对应环境变量。例如使用 OpenRouter、Together AI 时分别需要OPENROUTER_API_KEY、TOGETHERAI_API_KEYquestions脚本会对「每个问题 × 每个模型」各发起一次真实调用问题数量直接决定 API 调用总量与花费建议先小规模试跑--system-prompt 命令行选项脚本基于click封装了命令行入口支持python3 benchmark.py --system-prompt 你的系统提示词其默认值为You are a helpful assistant that can answer questions.。3.1 单次基准调用的实现细节从 benchmark.py 的源码可以看到每个「问题 × 模型」组合的核心逻辑为start_time time.time() response completion( modelmodel, max_tokens500, # 限制单次输出长度控制成本与耗时 messages[ {role: system, content: system_prompt}, {role: user, content: question}, ], ) end time.time() total_time end - start_time cost completion_cost(completion_responseresponse) raw_response response[choices][0][message][content]两处值得注意的实现细节max_tokens500硬编码限制每个模型每次最多生成 500 个 token既保证不同模型输出量可比也避免长输出拉高成本。若你的评测场景依赖长回复可修改该值但横向对比时注意各模型对max_tokens语义的一致性成本取自真实响应completion_cost直接解析响应对象里的usage字段prompt tokens / completion tokens与模型名而不是本地估算 token 数因此结果与供应商账单口径一致。四、成本计算的底层原理completion_cost定义在 litellm/cost_calculator.py其签名除必需的completion_response外还支持model、prompt、completion、custom_cost_per_token、service_tier、vertex_location等可选参数。其工作链路为提取用量从completion_response中读取模型名与 token 用量查价通过cost_per_token分发到各供应商的价格计算函数如openai_cost_per_token、anthropic_cost_per_token、bedrock_cost_per_token等价格数据源是仓库根目录的 model_prices_and_context_window.json。以当前仓库中的gpt-3.5-turbo条目为例{ input_cost_per_token: 5e-07, output_cost_per_token: 1.5e-06, max_input_tokens: 16385, max_output_tokens: 4096, mode: chat }该映射表当前收录了 3000 余个模型条目覆盖 Bedrock、Azure、OpenAI、Anthropic、VertexAI、vLLM 等主流供应商这正是 LLM-Bench 能对ollama/...、openrouter/...等任意前缀模型统一计价的原因 3.加权求和prompt_tokens × input_cost_per_token completion_tokens × output_cost_per_token返回美元浮点数对于未在映射表中的模型会抛出异常提示通过自定义定价custom_cost_per_token或提交 PR 补充价格条目。因此表中「Cost ($)」列的数值可以直接理解为「这一轮基准测试真实花掉的钱」$0.000122与$0.002084之间 17 倍的价差就是模型选型时成本维度的量化依据。五、运行 LLM-Bench 并解读输出python3 benchmark.py运行过程中每个模型都会显示绿色进度描述例如Running question: When will BerriAI IPO? for model: claude-2: 100%|████| 3/3 [00:1300:00, 4.41s/it]随后按问题分组打印结果表格下表摘自 readme.md 中记录的期望输出示例Benchmark Results for When will BerriAI IPO?: ------------------------------------------------------------------------------------------------------------------------------------------ | Model | Response | Response Time (seconds) | Cost ($) | | gpt-3.5-turbo | As an AI language model, I cannot provide up-to-date information or predict | 1.55 seconds | $0.000122 | | | future events. It is best to consult a reliable financial source or contact | | | | | BerriAI directly for information regarding their IPO plans. | | | ------------------------------------------------------------------------------------------------------------------------------------------ | togethercompute | Im not able to provide information about future IPO plans or dates for BerriAI | 8.52 seconds | $0.000531 | | r/llama-2-70b-c | or any other company. IPO (Initial Public Offering) plans and timelines are | | | | hat | typically kept private by companies until they are ready to make a public | | | | | announcement. ... | | | ------------------------------------------------------------------------------------------------------------------------------------------ | claude-2 | I do not have any information about when or if BerriAI will have an initial | 3.17 seconds | $0.002084 | | | public offering (IPO). ... | | | ------------------------------------------------------------------------------------------------------------------------------------------解读示例中的三个关键观察点成本与模型档位强相关同一问题下gpt-3.5-turbo$0.000122togethercomputer/llama-2-70b-chat$0.000531claude-2$0.002084时延并不与成本单调一致togethercomputer/llama-2-70b-chat耗时 8.52 秒反而高于 3.17 秒的claude-2。若你的业务对首响延迟敏感需要把 Response Time 列与 Cost 列联合评估而不是只看单价响应内容是人工评审区表格中 Response 列以 80 字符宽度截断展示完整的回答质量需要人工或自动评测判断。5.1 文档中记录的各模型成本快照readme.md 中还附有一张「模型 / 供应商 / 输入输出 token 单价」的基准测试成本快照表覆盖 Ollama 本地模型$0.0、Vertex AI 的 Bison 系列、Bedrock、OpenAI、Anthropic、Cohere、AI21 等供应商按单价从低到高排列ModelProviderCost per input output token ($)openrouter/mistralai/mistral-7b-instructopenrouter0.0ollama/llama2ollama0.0ollama/llama2:13bollama0.0ollama/llama2:70bollama0.0ollama/llama2-uncensoredollama0.0ollama/mistralollama0.0ollama/codellamaollama0.0ollama/orca-miniollama0.0ollama/vicunaollama0.0perplexity/codellama-34b-instructperplexity0.0perplexity/llama-2-13b-chatperplexity0.0perplexity/llama-2-70b-chatperplexity0.0perplexity/mistral-7b-instructperplexity0.0perplexity/replit-code-v1.5-3bperplexity0.0text-bisonvertex_ai-text-models0.00000025text-bison001vertex_ai-text-models0.00000025chat-bisonvertex_ai-chat-models0.00000025chat-bison001vertex_ai-chat-models0.00000025chat-bison-32kvertex_ai-chat-models0.00000025code-bisonvertex_ai-code-text-models0.00000025code-bison001vertex_ai-code-text-models0.00000025code-gecko001vertex_ai-chat-models0.00000025code-geckolatestvertex_ai-chat-models0.00000025codechat-bisonvertex_ai-code-chat-models0.00000025codechat-bison001vertex_ai-code-chat-models0.00000025codechat-bison-32kvertex_ai-code-chat-models0.00000025palm/chat-bisonpalm0.00000025palm/chat-bison-001palm0.00000025palm/text-bisonpalm0.00000025palm/text-bison-001palm0.00000025palm/text-bison-safety-offpalm0.00000025palm/text-bison-safety-recitation-offpalm0.00000025anyscale/meta-llama/Llama-2-7b-chat-hfanyscale0.0000003anyscale/mistralai/Mistral-7B-Instruct-v0.1anyscale0.0000003openrouter/meta-llama/llama-2-13b-chatopenrouter0.0000004openrouter/nousresearch/nous-hermes-llama2-13bopenrouter0.0000004deepinfra/meta-llama/Llama-2-7b-chat-hfdeepinfra0.0000004deepinfra/mistralai/Mistral-7B-Instruct-v0.1deepinfra0.0000004anyscale/meta-llama/Llama-2-13b-chat-hfanyscale0.0000005amazon.titan-text-lite-v1bedrock0.0000007deepinfra/meta-llama/Llama-2-13b-chat-hfdeepinfra0.0000007text-babbage-001text-completion-openai0.0000008text-ada-001text-completion-openai0.0000008babbage-002text-completion-openai0.0000008openrouter/google/palm-2-chat-bisonopenrouter0.000001openrouter/google/palm-2-codechat-bisonopenrouter0.000001openrouter/meta-llama/codellama-34b-instructopenrouter0.000001deepinfra/codellama/CodeLlama-34b-Instruct-hfdeepinfra0.0000012deepinfra/meta-llama/Llama-2-70b-chat-hfdeepinfra0.00000165deepinfra/jondurbin/airoboros-l2-70b-gpt4-1.4.1deepinfra0.00000165anyscale/meta-llama/Llama-2-70b-chat-hfanyscale0.000002anyscale/codellama/CodeLlama-34b-Instruct-hfanyscale0.000002gpt-3.5-turbo-1106openai0.000003openrouter/meta-llama/llama-2-70b-chatopenrouter0.000003amazon.titan-text-express-v1bedrock0.000003gpt-3.5-turboopenai0.0000035gpt-3.5-turbo-0301openai0.0000035gpt-3.5-turbo-0613openai0.0000035gpt-3.5-turbo-instructtext-completion-openai0.0000035openrouter/openai/gpt-3.5-turboopenrouter0.0000035cohere.command-text-v14bedrock0.0000035claude-instant-1anthropic0.00000714claude-instant-1.2anthropic0.00000714openrouter/anthropic/claude-instant-v1openrouter0.00000714anthropic.claude-instant-v1bedrock0.00000714openrouter/mancer/weaveropenrouter0.00001125j2-midai210.00002ai21.j2-mid-v1bedrock0.000025openrouter/jondurbin/airoboros-l2-70b-2.1openrouter0.00002775command-nightlycohere0.00003commandcohere0.00003command-lightcohere0.00003command-medium-betacohere0.00003command-xlarge-betacohere0.00003command-r-pluscohere0.000018j2-ultraai210.00003ai21.j2-ultra-v1bedrock0.0000376gpt-4-1106-previewopenai0.00004gpt-4-vision-previewopenai0.00004claude-2anthropic0.0000437openrouter/anthropic/claude-2openrouter0.0000437anthropic.claude-v1bedrock0.0000437anthropic.claude-v2bedrock0.0000437gpt-4openai0.00009gpt-4-0314openai0.00009gpt-4-0613openai0.00009openrouter/openai/gpt-4openrouter0.00009gpt-4-32kopenai0.00018gpt-4-32k-0314openai0.00018gpt-4-32k-0613openai0.00018需要说明这是文档撰写时点的一次性快照部分模型如claude-2、palm/*系列早已迭代且表中数值为「输入输出 token 的单价参考」而非某次真实调用的总花费当前仓库的最新价格以 model_prices_and_context_window.json 为准模型是否仍在售也应以各供应商文档为准。该表的价值在于演示 LLM-Bench 可横向拉通的供应商广度——Ollama 本地部署$0.0到 gpt-4-32k0.00018/token 级之间的价差可达数个数量级。六、延伸从基准测试到自动评测cookbook/benchmark 目录下还提供了一种子目录 eval_suites_mlflow_autoevals/auto_evals.py展示了如何把 LLM-Bench 的「响应内容」维度进一步自动化用 LiteLLM 发起completion调用后将返回内容传入 MLflow autoevals 的Factuality()评估器对比标准答案打分response litellm.completion( modelgpt-3.5-turbo, messages[{role: user, content: question}], ) evaluator Factuality() result evaluator( outputresponse.choices[0][message][content], expectedIndia, inputquestion, )由此可以推断LLM-Bench 的设计是「终端表格看时延与成本自动评测器看内容质量」的组合拳。七、实践建议与注意事项成本控制脚本会为每个「问题 × 模型」组合发起真实付费调用max_tokens500是唯一的输出上限护栏。首次运行建议只放 12 个模型、12 个问题试跑本地模型ollama/*单价为 0适合做流程验证公平性对比不同供应商时注意各模型上下文窗口与输出风格差异如 70B 模型天然更啰嗦可结合max_tokens与问题难度控制变量价格维护若基准测试中出现「模型不在价格映射表」的报错说明 model_prices_and_context_window.json 缺少该模型条目可参考 cost_calculator.py 中completion_cost的custom_cost_per_token参数临时注入自定义单价或直接向仓库提交价格 PR结果归档终端表格适合即时查看如需留存历史对比可将data列表改为追加写入 JSON/CSV仅作为个人脚本改造思路不影响仓库现有文件。小结LiteLLM 的 LLM-Bench 把「多模型选型」这件通常要写 N 套供应商 SDK 的活压缩成了改三行配置models、API Key、questions加一条运行命令python3 benchmark.py。其统一性建立在两层机制之上——completion()的多供应商适配层负责请求与计时completion_cost()加上 3500 余条模型价格映射负责计价。理解这套链路后你可以把它扩展为自己的日常模型评测流水线时延看time.time()差值、成本看completion_cost返回值、质量接 Factuality 等自动评测器。【免费下载链接】litellmThe fastest, litest AI Gateway. Rust core with Python SDK. Call 100 LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]项目地址: https://gitcode.com/GitHub_Trending/li/litellm创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED — 相关阅读

相关资讯

LATEST — 最新资讯

最新发布

TODAY — 本日精选

新闻

WEEKLY — 本周精选

新闻

MONTHLY — 本月精选

新闻