模型选型五问:开源与闭源、Token 成本、延迟、上下文窗口怎么权衡
选 LLM 看起来是单次决定,实则要随新模型和产品演化反复重做。本文把选型拆成五个问题:开源还是闭源、成本、延迟、性能、上下文窗口。开源模型需要自行托管或走 API 供应商(Hugging Face、Groq),闭源模型由模型厂商托管,按 token 计费,价格战愈演愈烈。延迟的测量要看 TTFT 和 TPOT;性能先参考公开榜单(Chatbot Arena、Open LLM Leaderboard),但榜单存在过拟合风险,真正的验证只能在应用内用自建 evals 进行。推理模型(如 o1)在编程和数学上更强,但要付出更高的 token 成本和更长响应时间。上下文窗口同时计入输入与输出 token,RAG 中的 chunking 等模式就是在有限的窗口里塞进更多信息。适合刚开始做模型选型的 AI 应用开发者。
Choosing the right model is crucial to the success of your AI-powered app. But it's not an easy call.
It's a tough, many-layered decision that you don't only make once. You'll have to make it over and over again as new models emerge and your app evolves.
I've split this decision down into several key questions you'll need to ask yourself when choosing a model.
选对模型,直接决定你的 AI 应用能否成功。但这并不是一个轻松的决定。
这是一个复杂、多层次的抉择,而且你不会只做一次。随着新模型不断出现、应用不断演进,你需要一次又一次地重新选择。
我把这个决策拆成了几个关键问题,你在选模型时不妨逐一自问。
There are two main types of models you'll need to choose from: open and closed.
Open Models
Open source models are models that are free to download and use. However, you'll need to host and run them yourself if you want to build an app with them.
Open models can be run on your own hardware, or on a cloud provider like AWS or Azure.
The Open LLM Leaderboard is a great place to look for open models.
Closed Models
Closed-source models are ones controlled by companies. You need to pay to use them, but they are hosted by the company, so you don't need to worry about running them yourself.
The most powerful models in the world are currently closed-source. But open models are improving all the time.
Chatbot Arena is a good place to compare closed-source against open-source models.
你需要从两类主要模型中做选择:开源与闭源。
开源模型
开源模型可以免费下载和使用,但如果你想用它来构建应用,就得自己托管和运行。
开源模型可以跑在自己的硬件上,也可以跑在 AWS 或 Azure 这样的云服务商上。
Open LLM Leaderboard 是寻找开源模型的好去处。
闭源模型
闭源模型则由公司控制。你需要付费使用,但模型由公司托管,不用操心自己运行的问题。
目前世界上最强大的模型是闭源的,不过开源模型也在不断进步。
Chatbot Arena 是对比闭源与开源模型的好地方。
Model Providers, API Providers, and Hosting Your Own Model
There are two types of companies who host models for you to use:
Model providers use closed-source models. You pay to use their models, but they're hosted by the company. These include OpenAI, Google, Anthropic, Deepseek, and many more.
API providers host open source models, and charge you a fee for using them. They include Hugging Face, Groq, and others.
You can also host your own model. This is the most flexible option, but also the most expensive. You'll need to pay for the hardware to run the model, and you'll be responsible for keeping it up and running.
The way you'll pay for your LLM usage changes depending on whether you're hosting your model or not.
模型提供商、API 提供商与自己托管模型
为你托管模型的公司分成两类:
模型提供商(model providers)使用闭源模型。你付费使用他们的模型,但模型由公司托管。OpenAI、Google、Anthropic、Deepseek 等都属于这一类。
API 提供商(API providers)托管开源模型,并按使用量收费。Hugging Face、Groq 等属于这一类。
你还可以选择自己托管模型。这是最灵活的方式,但也是最贵的。你需要为运行模型的硬件买单,并负责让模型持续正常运行。
你的 LLM 付费方式,取决于你是否自己托管模型。
Cost Per Token
Most model providers charge by token. The more tokens you use, the more you pay. The most common way this is expressed is "cost per 1m tokens".
You don't just get charged for input tokens (what you say to the model), but also for completion tokens (how the model replies).
This is a traditional 'pay per usage' model - just like serverless platforms charging you for compute time. Tokens are a decent metric for how much you're using the model.
There is a worldwide race-to-the-bottom on token prices, as companies compete for market share. Price comparison websites like Helicone's are useful for comparing prices.
按 token 计费
大多数模型提供商按 token 计费。token 用得越多,付得越多。最常见的计价方式是“每 100 万个 token 多少钱”。
你不仅要为输入 token(你向模型说的话)付费,还要为输出 token(模型回复的内容)付费。
这是一种传统的“按用量付费”模式,就像 serverless 平台按计算时间收费一样。token 是衡量模型用量的一个不错指标。
眼下各家公司正在 token 价格上展开全球性的“逐底竞争”,以争夺市场份额。Helicone 等价格对比网站很适合用来比价。
Hosting Open-Source Models
Hosting open-source models can be a more cost-effective option. Instead of paying per token, you now pay a fixed fee to host the model.
This also has the benefit of keeping you entirely in control of your own data. This can be important for data residency and privacy reasons.
However, models need to be hosted on powerful hardware, which can be expensive. You'll need to balance the cost of hosting the model with the cost of using a model provider.
My general suggestion is to start with third-party APIs. They give you the most flexibility and are reasonably cost-effective. Later, you can move to hosting your own models if you need to.
自托管开源模型
自托管开源模型可能是更具成本效益的选择。你不再按 token 付费,而是付一笔固定的托管费用。
这样做还能让你完全掌控自己的数据。在需要满足数据驻留和隐私要求时,这一点很重要。
不过,模型需要运行在性能强大的硬件上,这可能会很贵。你需要权衡自托管模型的成本与使用模型提供商的成本。
我的一般建议是:从第三方 API 开始。它们灵活性最高,成本也相对合理。之后如果确有需要,再迁移到自己托管模型。
Another important feature to consider is latency. Latency is the approximate time it takes for the model to respond to a query. Fast responses can be crucial for many use cases, and can make your application more useful to users.
Latency is affected by the size of the model - smaller models are faster to run, but often less accurate.
Latency is also affected by the hardware the model is running on. More powerful hardware can run models faster.
And finally, it's affected by any inference optimizations made to the model. This helps speed up the inference function of the model. They include quantization, distillation and parallelism - and are somewhat outside the bounds of this article.
另一个需要考虑的重要特性是延迟,也就是模型对查询作出响应的大致时间。响应快慢对很多用例都至关重要,也能让你的应用对用户更有用。
延迟受模型规模影响:模型越小,跑得越快,但准确性常常也越低。
延迟还受模型所运行硬件的影响。硬件越强,模型跑得越快。
最后,模型所做的推理优化也会影响延迟。推理优化能加速模型的推理过程,包括量化、蒸馏和并行等,具体内容超出了本文范围。
Measuring Latency
There are two main metrics to consider when measuring latency:
TTFT: Time to first token: how long it takes for the model to start generating a response
TPOT: Time per output token: how long it takes for the model to generate each token
衡量延迟
衡量延迟时,有两个主要指标值得关注:
TTFT(Time to First Token,首 token 时间):模型开始生成响应需要多长时间。
TPOT(Time Per Output Token,每个输出 token 的时间):模型生成每个 token 需要多长时间。
Cost and latency are important, but a model is useless if it cannot perform the task required.
As a general rule, smaller models will perform worse. A smaller model will have fewer parameters - and so a smaller space in which to store information.
Public Benchmarks
The performance of a model is an extremely slippery metric. It's extremely difficult to look at two models and say which one is better.
A common approach that model providers (and open-source projects) take is to use benchmarks. These are standardized tests that measure the performance of a model on a specific task. These benchmarks can grade the model at certain tasks, such as translation, summarization, question-answering, or coding.
Benchmarks are a decent early indicator for a model's performance. However, there is a constant danger of model providers overfitting their models to these benchmarks. This can either happen by the model accidentally consuming the benchmark data during pre-training, or organizational pressure to improve benchmark scores.
In some benchmarks, models are compared against each other - with humans grading which model produces the better output. Chatbot Arena is a good example of this - and worth checking out for an early indicator of model performance.
成本和延迟固然重要,但如果一个模型完成不了任务,那它就没用。
一般来说,较小的模型表现会更差。模型越小,参数越少——可以用来存储信息的空间也就越小。
公共基准测试
模型性能是一个非常难以衡量的指标。很难看着两个模型就说哪个更好。
模型提供商(以及开源项目)常用的做法是使用基准测试。基准测试是衡量模型在特定任务上表现的标准测试,可以对模型的翻译、摘要、问答或编码等能力打分。
基准测试是判断模型表现的一个不错的早期指标。不过,模型提供商始终有可能让模型在这些基准上过拟合——要么是模型在预训练时意外吃进了基准数据,要么是组织压力要求提高基准分数。
有些基准测试会把模型放在一起比较,由人工来评判哪个模型的输出更好。Chatbot Arena 就是一个很好的例子,值得看一看,作为模型表现的早期参考。
Specialized Models
Some models will perform better at certain tasks. This often depends on the model's training data - if it has been trained on large amounts of code examples, it will be better at coding tasks. The same is true for many disciplines: translation, classification, summarization, etc.
If you have a specialized task, it's worth looking for models which are specialized for that task. These models will usually outperform general-purpose models. They are also often smaller - so, faster and more efficient.
Reasoning Models
Some models have been specifically designed to pause before providing a response. These are the reasoning class of models, a trend initiated by OpenAI's o1.
These models often perform better at tasks requiring forward planning and critical thinking, like coding and math problems. They also output their planning process with reasoning tokens, which can be useful to stream to the user in real-time.
However, they are often more expensive than regular models, and take longer to respond. It's a performance/latency tradeoff.
专用模型
有些模型在特定任务上表现更好。这往往取决于模型的训练数据——如果模型用大量代码示例训练,它在编码任务上就会更擅长。翻译、分类、摘要等许多领域也是同理。
如果你的任务很专门,值得去找针对该任务优化的模型。这类模型通常比通用模型表现更好,而且往往更小——所以更快、更高效。
推理模型
有些模型被特意设计成在给出回答前先“思考”一阵。这就是推理(reasoning)类模型,由 OpenAI 的 o1 开创了这一潮流。
这类模型在需要提前规划和批判性思考的任务上通常表现更好,比如编码和数学问题。它们还会用推理 token 输出自己的规划过程,这可以实时流式呈现给用户。
不过,推理模型通常比普通模型更贵,响应也更慢。这是性能与延迟之间的一种取舍。
Evals
The only way a model can truly be evaluated is by testing it in the context of your application. This is why building evals for your system is so crucial.
Evals are a set of benchmarks you run on your own system. They let you see whether your system is improving or degrading over time. We'll cover them in more depth later.
Evals
真正评估一个模型的唯一方式,是在你自己的应用场景中测试它。这就是为什么为系统建立 evals 如此关键。
Evals 是一组在你自己的系统上运行的基准测试。它们能让你看到系统随着时间推移是变好了还是变差了。我们稍后会深入介绍。
- How Big A Context Window Do I Need?
The context window is the number of tokens the model can see at a time. The larger the context window, the more information the model can use to generate its next word.
This limit is counted in tokens, and counts both input and completion tokens. Passing too long an input to a model (or forcing it to generate too long a response) can cause an API error, or prevent it from generating a response.
The context window size is related to the mechanism the model uses to generate text - so is tied to the design of the model itself. Context window sizes are growing all the time. Currently, Gemini models have the largest context windows.
Since the context window is limited on all models, managing it is a constant battle for AI engineers. Patterns like chunking in RAG are designed to squeeze more information into the context window.
- 我需要多大的上下文窗口?
上下文窗口是模型一次能看到的 token 数量。上下文窗口越大,模型能用来生成下一个词的参考信息就越多。
这个限制按 token 计算,输入和输出 token 都算在内。给模型传入过长的输入(或逼它生成过长的回复),可能触发 API 错误,甚至让它无法生成响应。
上下文窗口大小与模型生成文本的机制相关,所以和模型本身的设计绑定在一起。上下文窗口一直在变大。目前 Gemini 模型的上下文窗口最大。
既然所有模型的上下文窗口都有限,管理窗口就成了 AI 工程师持续要打的仗。RAG 中的分块(chunking)等模式,就是用来把更多信息塞进上下文窗口的。
These five factors are important for choosing your model:
Open or Closed
Cost
Latency
Performance
Context Window
Leaderboards and benchmarks are a good place to start. However, the only way to truly assess a model's suitability is to test it in your application via experimentation with your own evals.
选择模型时,这五个因素都很重要:
开源还是闭源
成本
延迟
性能
上下文窗口
排行榜和基准测试是不错的起点。不过,要真正判断一个模型是否合适,唯一的方法是在你的应用里通过自己的 evals 做实验验证。