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