# 错误：处理流式数据块时超出上下文长度 — 返回部分响应

- **ID:** `llm/context-window-exceeded-with-chunked-streaming`
- **领域:** llm
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在流式处理期间，累积的输入和输出令牌超过了模型的上下文窗口，导致API在流中间截断响应，而没有明确的错误提示。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.12.0 | active | — | — |
| anthropic==0.25.0 | active | — | — |
| langchain==0.1.12 | active | — | — |
| gpt-4-turbo-2024-04-09 | active | — | — |
| claude-3-opus-20240229 | active | — | — |

## 解决方案

1. ```
   在流式处理前，使用tiktoken计算总令牌数（例如：`import tiktoken; enc = tiktoken.encoding_for_model('gpt-4'); tokens = enc.encode(prompt); if len(tokens) > 120000: truncate prompt`）。截断输入以为输出留出空间。
   ```
2. ```
   通过降低max_tokens来减少输出长度，并实现一个循环，在截断时从最后一个完整句子恢复生成。
   ```

## 无效尝试

- **** — Increasing max_tokens in the request doesn't help because the total (input + output) exceeds the model's limit, and max_tokens only caps output. (85% 失败率)
- **** — Retrying the same request with no changes will reproduce the error since the context is still too large. (95% 失败率)
- **** — Switching to a different streaming library (e.g., from openai to httpx) doesn't solve the underlying token limit issue. (90% 失败率)
