# 上下文窗口

上下文窗口是指语言模型在生成回答时一次性能够纳入考量的文本量，以token计量，其中也包括回答本身。它是模型的工作记忆，与模型训练时所使用的、规模大得多的语料库是两回事。<sup>[1](https://docs.anthropic.com/en/docs/build-with-claude/context-windows)</sup> 从2019年到2025年，可用窗口大小增长了大约三个数量级，从几千个token增至一百万甚至更多，但宣传中的大窗口并不保证模型能有效利用其全部容量。<sup>[2](https://os.platformstud.io/guild/articles/llm-context-window-size-the-new-moore-s-law-by-jeremy-burton)</sup><sup> • </sup><sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup>

| 关键事实 | 详情 |
|---|---|
| 单位 | 一个token大致相当于4个字符或四分之三个单词；100个token约等于75个单词；1,500个单词约等于2,048个token。<sup>[4](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them)</sup> |
| 限制结构 | 模型有最大token限制；有些模型规定的是输入加输出的合计上限，Anthropic表示其窗口的一部分预留给回答。<sup>[4](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them)</sup><sup> • </sup><sup>[5](https://support.anthropic.com/en/articles/11647753-understanding-usage-and-length-limits)</sup> |
| 增长 | GPT-3在2020年接受2,048个token；GPT-4接受8,192个，GPT-4 32K在2023年3月接受32,768个；2025至2026年的领先模型宣传约100万个token，自2019年以来增长约1,000倍。<sup>[2](https://os.platformstud.io/guild/articles/llm-context-window-size-the-new-moore-s-law-by-jeremy-burton)</sup><sup> • </sup><sup>[6](https://developers.openai.com/api/docs/models)</sup> |
| 成本驱动因素 | 对于标准的完全自注意力，注意力计算量随上下文长度呈O(n²)增长，KV缓存也随之增长：对于一个4.9 GB的模型，缓存内存在4K上下文时为0.5 GB，到128K时升至17.2 GB，是权重的3.5倍。<sup>[7](https://redis.io/blog/llm-context-windows/)</sup><sup> • </sup><sup>[8](https://gpufits.com/en/guides/long-context-vram-cost/)</sup> |
| 有效长度 | 可用的上下文长度——超过该长度后性能开始下降——可能远短于宣称的最大值。<sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup> |
| 位置效应 | 模型通常能很好地检索长文档开头和结尾的事实，但对中间部分的事实的得分往往低于60%，即“中间迷失”问题。<sup>[9](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)</sup> |
| 长上下文与RAG | 在32k上下文下，长上下文输入平均比检索增强生成高2.4%；在128k下，RAG反而高出3.68%；不存在通用的赢家。<sup>[10](https://ar5iv.labs.arxiv.org/html/2502.09977)</sup> |

## Token以及什么会计入窗口

语言模型不直接读取单词或字符，而是读取token，即由分词器生成的短文本片段。OpenAI的指南给出1个token约等于4个字符、1个token约等于¾个单词，因此100个token大约是75个单词，一个段落大约是100个token。<sup>[4](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them)</sup> 这一比例并不固定：同一段文本根据模型、其编码方式和语言的不同，会产生不同的token数。<sup>[11](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens)</sup> [Anthropic](https://www.edgepedia.org/zh/anthropic)当前的分词器（随Claude Opus 4.7推出）大约能把55.5万个单词、即250万个Unicode字符装入100万个token，而其早期分词器在同样的计数下约能装入75万个单词；20万个token大约相当于15万个单词。<sup>[12](https://docs.anthropic.com/en/about-claude/models)</sup>

窗口涵盖请求中的所有内容：系统指令、对话历史、检索到的文档以及生成的回答。每个模型都有输入加输出的合计上限，在Anthropic的付费方案中，窗口的一部分始终预留给回答；向其100万token模型发出的单次请求最多可生成12.8万个输出token。<sup>[4](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them)</sup><sup> • </sup><sup>[5](https://support.anthropic.com/en/articles/11647753-understanding-usage-and-length-limits)</sup><sup> • </sup><sup>[1](https://docs.anthropic.com/en/docs/build-with-claude/context-windows)</sup> 推理token——某些模型在回答前产生的内部步骤——不作为回答文本可见，但会计入输出用量并按输出token计费。<sup>[11](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens)</sup>

## 长上下文为何昂贵

上下文长度面临三个重要的计算约束：自注意力的O(n²)复杂度、KV缓存内存增长以及GPU内存带宽。<sup>[7](https://redis.io/blog/llm-context-windows/)</sup> 在自注意力中，每个token都要关注其他所有token，因此上下文翻倍会使注意力计算量大约变为四倍。KV缓存——解码时必须查阅的、存储过往token的中间状态——随上下文增长，并与模型权重争夺内存。<sup>[7](https://redis.io/blog/llm-context-windows/)</sup>

在一个权重为4.9 GB的模型上实测，KV缓存在4K上下文时为0.5 GB，32K时为4.3 GB，64K时为8.6 GB，128K时为17.2 GB，此时其大小是权重的3.5倍。<sup>[8](https://gpufits.com/en/guides/long-context-vram-cost/)</sup> 吞吐量也因同样的原因下降：生成过程中，每产生一个token都要从显存中重新读取全部权重以及整个KV缓存，因此速度从缓存可忽略时的约每秒154个token降至32K时的每秒约82个token、128K时的每秒约34个token。<sup>[8](https://gpufits.com/en/guides/long-context-vram-cost/)</sup> 延迟也随上下文变化：首token延迟在4K token时约为0.5至2秒，32K时为2至5秒，128K时为8至15秒，100万token时为30至90秒。<sup>[9](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)</sup>

定价同样随token数增长。对于单次12.8万token的查询，一项研究记录到GPT-4o为0.32美元，o1-preview为1.92美元，[Claude 3.5 Sonnet](https://www.edgepedia.org/zh/claude-3-5-sonnet)为0.384美元，[Gemini 1.5 Pro](https://www.edgepedia.org/zh/gemini-1-5-pro)为0.16美元；一次200万token的Gemini 1.5 Pro查询花费5美元。<sup>[13](https://arxiv.org/pdf/2411.03538)</sup> 每百万token价格更低并不一定意味着总成本更低，因为不同模型对文本的分词方式不同，生成的输出或推理token量也不同。<sup>[11](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens)</sup>

## 扩展上下文的技术

**位置编码方法。** ALiBi（带线性偏置的注意力）用与键和查询之间距离成正比的、线性递减的注意力分数惩罚来取代位置嵌入，从而使在短序列上训练的模型能够处理更长的序列。一个在1,024 token序列上训练、采用ALiBi的13亿参数语言模型，其困惑度（衡量模型预测文本能力的统计指标）与在2,048 token上训练的正弦模型相当，同时训练速度快11%、内存占用少11%；该方法不需要额外的运行时成本或参数，内存仅增加0–0.7%。<sup>[14](https://arxiv.org/pdf/2108.12409v1)</sup> LongRoPE将预训练模型扩展到2,048k（约200万）个token，在不超过256k的训练长度下仅需最多1,000步微调，利用了位置插值中的非均匀性；其论文指出，由于微调成本、长文本稀缺以及新位置上的数值不稳定，扩展后的上下文窗口通常仅限于约12.8万个token。<sup>[15](https://proceedings.mlr.press/v235/ding24i.html)</sup>

**持续预训练与稀疏注意力。** UltraLong配方利用持续预训练来扩展窗口，再加上高效的指令微调以保留短上下文能力，将基于[Llama](https://www.edgepedia.org/zh/llama-model-family)-3.1-Instruct的模型从128K扩展到1M、2M和4M token。<sup>[16](https://aclanthology.org/2026.findings-acl.640/)</sup> MiniMax的M3模型通过MSA达到100万token窗口，这是一种稀疏注意力架构，通过在完全注意力之前增加预过滤阶段来避免复杂度爆炸。<sup>[17](https://www.minimax.io/blog/minimax-m3)</sup>

**缓存、摘要与检索。** 厂商也在模型之外管理上下文。当对话接近窗口上限时，Claude会自动对较早的消息进行摘要，其Projects功能使用检索增强生成，只将相关内容加载到窗口中。<sup>[5](https://support.anthropic.com/en/articles/11647753-understanding-usage-and-length-limits)</sup> Gemini的上下文缓存让开发者可以存储上传的文件并按小时付费，Gemini Flash的缓存输入/输出成本大约为标准输入/输出成本的四分之一。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup>

## 上下文窗口的增长

GPT-3在2020年支持2,048个token。GPT-4支持8,192个token，GPT-4 32K支持32,768个token，二者均于2023年3月发布；从2019年到2025年，上下文窗口大小增长了约1,000倍。<sup>[2](https://os.platformstud.io/guild/articles/llm-context-window-size-the-new-moore-s-law-by-jeremy-burton)</sup> Google的文档称早期生成模型一次只能处理8,000个token，较新的模型接受32,000或128,000个，而Gemini是首个能够接受100万个token的模型。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup> Gemini 1.5 Pro于2024年2月推出，标准窗口为12.8万个token，非公开预览时最多可达100万个token，高于Gemini 1.0的32,000个；Google称100万窗口是当时所有大规模基础模型中最长的，并报告研究测试成功达到1,000万个token。<sup>[19](https://blog.google/innovation-and-ai/products/google-gemini-next-generation-model-february-2024/)</sup><sup> • </sup><sup>[20](https://blog.google/innovation-and-ai/products/long-context-window-ai-models/)</sup>

开放权重模型也遵循同样的曲线：第一代Llama有2k token，而Mixtral和DBRX达到32k，Llama 3.1达到128k。<sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup> 研究系统走得更远：已演示了多达400万token的稳定语言建模，尽管在上下文中间识别相关信息的困难依然存在。<sup>[21](https://proceedings.neurips.cc/paper_files/paper/2024/file/6ffdbbe354893979367f93e2121e37dd-Paper-Conference.pdf)</sup>

## 模型如何比较

截至2025至2026年，当前的[OpenAI API](https://www.edgepedia.org/zh/openai-api)模型列出的上下文窗口为105万个token，最大输出为128K个token。<sup>[6](https://developers.openai.com/api/docs/models)</sup> 若干当前Claude模型拥有100万token的上下文窗口，每次请求最多可输出12.8万个token。<sup>[1](https://docs.anthropic.com/en/docs/build-with-claude/context-windows)</sup> 2025年7月一项对宣传窗口的调查列出：GPT-4.1和[Gemini 2](https://www.edgepedia.org/zh/gemini-2).5 Pro为1,048,576个token，[Claude Sonnet 4](https://www.edgepedia.org/zh/claude-sonnet-4)为200,000个，Grok 4约为1,000,000个，Llama 4 Scout在某些配置下超过1,000万个token。<sup>[9](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)</sup>

定价结构各不相同。Anthropic将其完整的100万token窗口以标准价格全面开放给[Claude Opus 4](https://www.edgepedia.org/zh/claude-opus-4).6和Sonnet 4.6，Opus 4.6为每百万token 5/25美元，Sonnet 4.6为3/15美元，不设长上下文加价：90万token的请求与9千token的请求按相同的每token费率计费。<sup>[22](https://claude.com/resources/articles/1m-context-ga)</sup> GPT-4.1的标价为每百万token 2.00/8.00美元，并有50%的提示缓存折扣；Claude Sonnet 4为3.00/15.00美元，提示缓存折扣为90%。<sup>[9](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)</sup>

## 有效上下文与失败模式

**宣传值与可用值。** RULER基准发现，有效上下文长度——超过该长度后模型性能开始下降的可用长度——可能远短于宣称的最大值。<sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup> Giraffe研究发现，所有上下文长度外推方法都会在任务准确率上出现退化，即使在输出仍然连贯、困惑度合理的长度上也是如此；即使模型只关注窗口最后512个token，也能获得合理的困惑度分数，因此仅凭困惑度不足以衡量长上下文性能。<sup>[23](https://ar5iv.labs.arxiv.org/html/2308.10882)</sup> 在RAG设置中，大多数受评估模型随着上下文长度增长，性能先升后降，只有少数最先进的模型在64k token以上保持稳定的准确率；大多数开源模型尽管宣传有长上下文，实际只能处理大约16k至32k token的有效RAG任务。<sup>[13](https://arxiv.org/pdf/2411.03538)</sup>

**大海捞针测试。** 标准测试将事实嵌入长文档中，要求模型将其检索出来。Gemini的文档报告单针准确率约为99%，但指出多针情况下性能差异很大；以99%的性能检索100条信息可能需要100次独立请求，每次都要支付全部输入token的成本。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup> Claude Opus 4.6在100万上下文下的MRCR v2上得分为78.3%，被称为该上下文长度下前沿模型中的最高分。<sup>[22](https://claude.com/resources/articles/1m-context-ga)</sup> [Epoch AI](https://www.edgepedia.org/zh/epoch-ai)通过Fiction.liveBench（衡量叙事理解）和2针设置下的MRCR（衡量上下文相关信息的检索）来追踪长上下文能力；它发现模型随时间推移能更有效地利用更长的输入，但仍有提升空间，因为许多长上下文任务比这些基准更难，而且有些相关内容即使是最长的窗口也装不下。<sup>[24](https://epoch.ai/data-insights/context-windows)</sup>

**中间迷失。** 模型难以保留和利用长文本中间部分的信息，且随着上下文增长性能下降。<sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup> 针测试显示，对文档开头和结尾的事实准确率近乎完美，但对中间部分的事实往往低于60%。<sup>[9](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)</sup> LaRA基准证实，当答案位于上下文中心附近时，长上下文LLM会失去准确率，而RAG性能与答案位置没有明显相关性。<sup>[10](https://ar5iv.labs.arxiv.org/html/2502.09977)</sup> 由此得出实用建议：将查询放在长提示的末尾通常能提高性能。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup> Ms-PoE等即插即用的位置编码方法直接针对这一问题，无需微调即可改善对上下文中间信息的处理，在Zero-SCROLLS上平均准确率提升最高达3.8。<sup>[21](https://proceedings.neurips.cc/paper_files/paper/2024/file/6ffdbbe354893979367f93e2121e37dd-Paper-Conference.pdf)</sup> Anthropic将这一更广泛的现象命名为上下文腐化：随着token数增长，准确率和召回率下降。<sup>[1](https://docs.anthropic.com/en/docs/build-with-claude/context-windows)</sup>

**推理失败。** 长上下文语言模型在多匹配检索和基于逻辑的检索等基本任务上表现不佳，这些任务需要由特定提示引导的足够长的推理过程；仅仅扩展上下文窗口大小无法解决所有类型的长上下文任务，而长推理虽然准确，却耗时且低效。<sup>[25](https://aclanthology.org/2025.findings-emnlp.301.pdf)</sup>

## 上下文窗口与检索

超长上下文是否会取代检索增强生成是一个活跃的比较课题，实测答案取决于长度和任务。在LaRA基准中，在32k上下文长度下，长上下文输入在各模型上平均准确率比RAG高2.4%，但在128k下趋势反转，RAG以3.68%的优势超过长上下文。长上下文擅长推理和比较任务，而RAG在单位置任务上与之相当，并在识别幻觉方面具有显著优势；在128k下，表现最好的三个专有模型（GPT-4o、Gemini-1.5-Pro和Claude-3.5-Sonnet）都使用了长上下文输入。该研究得出结论：不存在通用的赢家，因为性能取决于具体模型和上下文长度。<sup>[10](https://ar5iv.labs.arxiv.org/html/2502.09977)</sup>

在实践中，二者相辅相成。销售100万token窗口的Anthropic仍在其Projects功能中使用RAG，只将相关内容加载到窗口中，并对较早的消息进行摘要，而不是无限期保留完整历史。<sup>[5](https://support.anthropic.com/en/articles/11647753-understanding-usage-and-length-limits)</sup> 成本进一步强化了这种分工：一次准确率99%的单针查询每次仍要支付全部输入token的成本，因此在大规模语料库上反复检索，通过RAG可能比重新发送整个上下文更便宜。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup>

## 近期发展

2023年初，领先模型提供8k至32k token，GPT-4 Turbo和GPT-4o后来达到128k。<sup>[2](https://os.platformstud.io/guild/articles/llm-context-window-size-the-new-moore-s-law-by-jeremy-burton)</sup><sup> • </sup><sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup> 到2024年2月，Gemini 1.5 Pro在预览中提供100万token，研究测试达到1,000万。<sup>[19](https://blog.google/innovation-and-ai/products/google-gemini-next-generation-model-february-2024/)</sup><sup> • </sup><sup>[20](https://blog.google/innovation-and-ai/products/long-context-window-ai-models/)</sup> 伴随增长出现了两个经济变化。在一项2024年研究之前的一年里，每百万输入token的价格从GPT-4的30美元降至GPT-4o的2.5美元，Anthropic后来完全取消了长上下文溢价，按标准每token费率对100万token请求计费。<sup>[13](https://arxiv.org/pdf/2411.03538)</sup><sup> • </sup><sup>[22](https://claude.com/resources/articles/1m-context-ga)</sup> 提示缓存存储重复使用的上下文，对缓存的Gemini Flash输入和输出收费约为标准收费的四分之一，改变了重复长提示的经济性。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup> 在研究方面，持续预训练方法现在将开放权重模型扩展到200万和400万token，位置插值方法通过适度微调达到约200万token。<sup>[16](https://aclanthology.org/2026.findings-acl.640/)</sup><sup> • </sup><sup>[15](https://proceedings.mlr.press/v235/ding24i.html)</sup>

Google报告在整个窗口上单针检索准确率最高约99%，而独立基准测试发现有效上下文往往远短于宣传的最大值。两者可以同时成立，因为单针检索比出现退化的多针和推理任务更容易。<sup>[18](https://ai.google.dev/gemini-api/docs/long-context)</sup><sup> • </sup><sup>[3](https://www.databricks.com/blog/long-context-rag-performance-llms)</sup>

## References

1. [Context windows — Anthropic Docs](https://docs.anthropic.com/en/docs/build-with-claude/context-windows)
2. [LLM Context Window Size: The New Moore's Law? (Jeremy Burton, Platform Studio)](https://os.platformstud.io/guild/articles/llm-context-window-size-the-new-moore-s-law-by-jeremy-burton)
3. [Long Context RAG Performance of LLMs, Databricks engineering blog](https://www.databricks.com/blog/long-context-rag-performance-llms)
4. [What are tokens and how to count them? OpenAI Help Center](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them)
5. [Understanding usage and length limits, Claude Help Center](https://support.anthropic.com/en/articles/11647753-understanding-usage-and-length-limits)
6. [Models, OpenAI API documentation](https://developers.openai.com/api/docs/models)
7. [Context window limits come from how transformers work, Redis](https://redis.io/blog/llm-context-windows/)
8. [The VRAM Cost of Long Context (and How to Right-Size It), gpufits](https://gpufits.com/en/guides/long-context-vram-cost/)
9. [Long-Context Prompting: Million-Token Windows, Williams Spurlock blog](https://williamspurlock.com/blog/long-context-prompting-million-token-windows/)
10. [LaRA: Benchmarking Retrieval-Augmented Generation and Long-Context LLMs](https://ar5iv.labs.arxiv.org/html/2502.09977)
11. [Understanding and counting tokens, OpenAI Help Center](https://help.openai.com/en/articles/4936856-understanding-and-counting-tokens)
12. [Models overview — Anthropic Docs](https://docs.anthropic.com/en/about-claude/models)
13. [RAG vs. Long Context: Assessing LLM performance with long-context RAG at scale (arXiv)](https://arxiv.org/pdf/2411.03538)
14. [Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation (Press et al., arXiv)](https://arxiv.org/pdf/2108.12409v1)
15. [LongRoPE: Extending LLM Context Window Beyond 2 Million Tokens (ICML 2024)](https://proceedings.mlr.press/v235/ding24i.html)
16. [From 128K to 4M: Efficient Training of Ultra-Long Context Large Language Models (ACL 2026 Findings)](https://aclanthology.org/2026.findings-acl.640/)
17. [MiniMax M3: Frontier Coding, 1M Context, Native Multimodality — All in One Model](https://www.minimax.io/blog/minimax-m3)
18. [Long context, Gemini API documentation](https://ai.google.dev/gemini-api/docs/long-context)
19. [Introducing Gemini 1.5, Google's next-generation AI model](https://blog.google/innovation-and-ai/products/google-gemini-next-generation-model-february-2024/)
20. [What is a long context window? Google DeepMind engineers explain](https://blog.google/innovation-and-ai/products/long-context-window-ai-models/)
21. [Found in the Middle: How Language Models Use Long Contexts Better via Plug-and-Play Positional Encoding (NeurIPS 2024)](https://proceedings.neurips.cc/paper_files/paper/2024/file/6ffdbbe354893979367f93e2121e37dd-Paper-Conference.pdf)
22. [1M context is now generally available for Opus 4.6 and Sonnet 4.6, Anthropic](https://claude.com/resources/articles/1m-context-ga)
23. [Giraffe: Adventures in Expanding Context Lengths in LLMs (arXiv)](https://ar5iv.labs.arxiv.org/html/2308.10882)
24. [LLMs now accept longer inputs, and the best models can use them more effectively, Epoch AI](https://epoch.ai/data-insights/context-windows)
25. [Long-context Language Models Fail in Basic Retrieval Tasks Without Sufficient Reasoning Steps (EMNLP 2025 Findings)](https://aclanthology.org/2025.findings-emnlp.301.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 11, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Large language model architecture and scaling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/context-window

English original: https://www.edgepedia.org/context-window
