【Bug已解决】Claude context window exceeded / Conversation too long — Claude 上下文窗口超限解决方案 【Bug已解决】Claude: context window exceeded / Conversation too long — Claude 上下文窗口超限解决方案1. 问题描述Claude 对话过程中上下文窗口超限无法继续对话# 上下文超限 $ claude --continue Error: context_window_exceeded Conversation has grown beyond the 200K token limit. # 或长对话中断 $ claude 分析代码 修改这个 再修改那个 ...100 轮后 Error: Context limit reached. Please start a new conversation. # 或文件对话超限 $ claude 分析 $(cat large_file.py) 然后修复 Error: Combined input exceeds 200000 tokens. # 或系统提示对话超限 $ claude --system-prompt $(cat large_prompt.txt) --continue Error: Total context exceeds maximum.这个问题在以下场景中特别常见长对话累积--continue大文件分析系统提示过长多文件同时分析代码生成累积历史记录过大2. 原因分析原因分类表原因分类具体表现占比长对话累积--continue约 40%大文件cat large约 25%系统提示长--system-prompt约 15%多文件同时分析约 10%代码生成累积约 5%历史记录session 大约 5%3. 解决方案方案一开新会话最推荐# 步骤 1不用 --continue开新会话 claude # 新会话 # 步骤 2保存之前的讨论 claude 将之前的讨论总结保存到 docs/summary.md # 步骤 3新会话引用总结 claude 参考 docs/summary.md 继续 # 步骤 4验证 claude --print hello方案二清除会话历史# 步骤 1清除当前会话 claude /clear # 步骤 2或清除所有会话 rm -rf ~/.claude/sessions/* # 步骤 3重新开始 claude task # 步骤 4验证 claude --print hello方案三分块处理大文件# 步骤 1使用 head/tail 分块 claude 分析 $(head -n 200 src/large_file.js) # 步骤 2使用 grep 过滤 claude 分析 $(grep function src/large_file.js | head -20) # 步骤 3使用 sed 提取 claude 分析 $(sed -n 1,200p src/large_file.js) # 步骤 4分步分析 claude 第一步: 列出 src/large_file.js 的函数 claude 第二步: 分析第一个函数 # 新会话方案四精简系统提示# 步骤 1检查系统提示大小 wc -c system-prompt.txt # 步骤 2精简到 4000 tokens # 只保留核心指令 # 步骤 3使用简短提示 claude --system-prompt 代码助手 task # 步骤 4或移除系统提示 claude task # 不使用 --system-prompt方案五使用更大窗口的模型# 步骤 1使用 200K 窗口 claude --model claude-sonnet-4-20250514 task # 步骤 2或 Opus claude --model claude-opus-4-20250514 task # 步骤 3减少 --max-turns 避免累积 claude --print task --max-turns 5 # 步骤 4验证 claude --model claude-sonnet-4-20250514 --print hello方案六使用文件引用# 步骤 1不用 $(cat) 粘贴 # 错误: claude 分析 $(cat large_file.js) # 正确: claude 分析 src/large_file.js 的前 200 行 # 步骤 2让 Claude 自己读取 claude 读取 src/index.js 第 1-50 行并分析 # 步骤 3分步引用 claude 分析 src/auth.js claude 分析 src/api.js # 新会话 # 步骤 4验证 claude --print 读取 src/index.js --max-turns 54. 各方案对比总结方案适用场景推荐指数难度方案一新会话长对话⭐⭐⭐⭐⭐低方案二清除历史累积⭐⭐⭐⭐⭐低方案三分块大文件⭐⭐⭐⭐⭐低方案四精简提示系统提示⭐⭐⭐⭐⭐低方案五大窗口超大⭐⭐⭐⭐低方案六文件引用大文件⭐⭐⭐⭐⭐低5. 常见问题 FAQ5.1 上下文窗口多大Sonnet/Opus/Haiku: 200K tokens5.2 200K tokens 多少约 150,000 英文单词约 5,000-8,000 行代码。5.3 --continue 为什么容易超限每次 --continue 加载之前的全部对话历史历史越长累积 Token 越多。5.4 如何开新会话claude # 不使用 --continue5.5 如何清除会话claude /clear # 清除当前 rm -rf ~/.claude/sessions/* # 清除所有5.6 如何分块处理使用head -n 200、grep、sed -n 1,200p提取文件的一部分。5.7 系统提示占多少通常 500-5000 tokens。过长会挤占对话空间。5.8 如何保存讨论claude 将讨论总结保存到 docs/summary.md5.9 $(cat) 有什么问题将整个文件作为命令行参数可能导致上下文超限。5.10 排查清单速查表□ 1. claude 开新会话 □ 2. claude /clear 清除历史 □ 3. 保存总结到文件 □ 4. head -n 200 分块 □ 5. grep 过滤大文件 □ 6. 精简系统提示 4000 tokens □ 7. --model sonnet 200K 窗口 □ 8. --max-turns 5 减少累积 □ 9. 不用 $(cat) 粘贴大文件 □ 10. claude 读取文件 让 Claude 自己读6. 总结根本原因上下文超限最常见原因是长对话累积40%和大文件分析25%最佳实践开新会话不 --continue将讨论总结保存到文件分块处理使用head -n 200、grep、sed提取文件部分精简提示系统提示保持在 4000 tokens 以内最佳实践建议让 Claude 自己读取文件claude 读取 src/index.js不用$(cat)粘贴故障排查流程图flowchart TD A[上下文超限] -- B{是 --continue?} B --|是| C[开新会话] B --|否| D{是大文件?} C -- E[claude 新会话] E -- F[保存总结到文件] F -- G[claude 验证] D --|是| H[分块处理] D --|否| I{系统提示长?} H -- j[head -n 200 分块] j -- K[或 grep 过滤] K -- G I --|是| L[精简提示 4000 tokens] I --|否| M[使用大窗口模型] L -- G M -- N[--model sonnet 200K] N -- G G -- O{成功?} O --|是| P[✅ 问题解决] O --|否| Q[清除会话历史] Q -- R[claude /clear] R -- S[或 rm sessions/*] S -- G O --|否| T[不用 $(cat)] T -- U[claude 读取文件] U -- V[让 Claude 自己读] V -- G G -- W{成功?} W --|是| P W --|否| X[分步分析] X -- Y[每步新会话] Y -- P P -- Z[长期: 新会话 分块 文件引用] Z -- AA[✅ 长期方案]