Skip to content

[Bug] 工具输出未截断导致 Context Token 溢出,Session 无法继续 #4049

@cfh1113

Description

@cfh1113

问题描述

当工具(如 run_shell_command)输出大量数据时,这些数据直接进入对话上下文,可能导致 token 超出模型限制,导致整个 Session 无法继续执行。

复现步骤

  1. 用户发送一个查询请求,AI 执行并返回数据(积累上下文)
  2. 用户发送一个复杂任务请求(如批量创建订单)
  3. AI 创建脚本并运行,脚本输出大量 JSON 数据
  4. 工具结果直接放入上下文
  5. 总 token 超出模型限制(如 GLM-5 的 202,745)
  6. API 返回错误:400 InternalError.Algo.InvalidParameter: Range of input length should be [1, 202745]
  7. AI 无法响应任何内容,Session 卡死

错误日志

2026-05-11T03:48:33.951Z [ERROR] [OPENAI_ERROR] 
OpenAI API Error: 400 InternalError.Algo.InvalidParameter: 
Range of input length should be [1, 202745]

预期行为

工具层应该:

  1. 截断工具输出 - 限制输出长度(如 10KB),超出部分显示摘要
  2. 上下文监控 - 实时显示 token 使用情况
  3. 预警机制 - 当 context 超过 80% 时发出警告
  4. 智能摘要 - 大数据输出自动写入临时文件,只返回摘要信息

实际行为

工具输出直接进入上下文,没有任何截断或摘要处理,导致 token 溢出。

影响范围

  • Session 完全无法继续
  • 用户没有任何提示
  • AI 无法响应任何内容
  • 需要重新开新 session

建议改进

  1. run_shell_command 添加输出长度限制参数
  2. 添加实时 context token 计数显示(如 [180k/202k]
  3. 大数据输出自动写入临时文件,返回摘要
  4. 提供 /compact 命令压缩历史对话
  5. 当接近 token 限制时,提示用户开启新 session

补充信息

  • Qwen Code 版本:0.15.6
  • 使用的模型:GLM-5 / Kimi-K2.5
  • 问题类型:工具层上下文管理缺失

Metadata

Metadata

Assignees

Labels

category/toolsTool integration and executionpriority/P2Medium - Moderately impactful, noticeable problemscope/session-managementSession state and persistencescope/token-managementToken handling and limitstype/bugSomething isn't working as expected

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions