概述
主要做法包括丟棄舊回合、摘要早期內容、將整個歷史壓縮成簡明摘要以及清除舊工具輸出。這很重要,因為語言模型在呼叫之間沒有記憶,除非應用程式重新傳送。當歷史超出視窗時,必須裁剪某些內容,裁剪的內容決定助手是否保持連貫。
深入探討
Most chat APIs are stateless. Every request resends the conversation so far, and the model reads it from scratch. Tokens, cost and latency therefore grow as the session gets longer, until the history hits the context limit and the request fails or gets truncated. Quality can drop even before that point, because relevant details are buried among old material. The simplest strategy is truncation, usually as a sliding window: keep the system prompt and the most recent turns, and drop the rest. It is cheap and predictable, but it forgets early facts, such as constraints the user stated at the start. A common refinement is to pin important messages so they are never dropped. Rolling summarization replaces older turns with a model-written summary that is updated as the conversation grows. It keeps the gist in far fewer tokens. The cost is that summaries lose detail and can introduce errors, and those errors compound when a summary is itself summarized again. Compaction is a more deliberate version used in agent tools. When token usage crosses a threshold, the system asks the model to write a structured handoff covering the goal, progress, decisions, open problems and key file names. It then starts a fresh context with that handoff plus the most recent turns. Claude Code, for example, has a compact command and can also compact automatically near the limit. Tool-result clearing targets the biggest source of bloat in agent sessions: large outputs from searches, file reads and commands that were useful once. Swapping them for short placeholders frees space and still records that the call happened. External memory stores facts in a file or database and retrieves them when relevant, so they survive any trimming. A common misconception is that the model remembers earlier parts of a chat on its own. Any continuity comes from what the application chooses to resend or retrieve.
現實世界的實施
聊天機器人保留系統提示加上最近 20 條訊息,並丟棄更舊的內容。這樣做成本低,但會忘記用戶在第一條訊息中提供的姓名和預算。
每 10 回合,較小的模型會將對話較早部分摘要成一段文字,並將其保留在上下文頂部。這稱為滾動摘要。
當編碼代理接近限制時,它會寫一份交接文件:目標、變更的檔案、未解決的錯誤和已做的決策。然後它會以該交接文件和最新檔案為內容開始一個全新的上下文。
代理會將幾回合前的搜尋結果替換為簡短佔位符,例如「[網路搜尋輸出已清除:12 個結果]」。一旦代理對原始結果採取行動,原始結果通常不再需要。
上下文壓縮與對話歷史管理的未來
供應商已開始在其平台中構建歷史管理功能。功能包括伺服器端對話狀態、自動壓縮和清除舊工具結果的上下文編輯。這減少了樣板工作,但開發者仍需決定哪些資訊絕不能遺忘。研究仍在進行,尋找能更可靠使用長上下文的模型,以及能跨會話存取和檢索事實的記憶系統。壓縮可能會成為代理框架的預設行為,但細節與空間之間的權衡不會消失。沒有任何方法能免費保留所有內容,因此測試遺失的資訊將繼續是工作的一部分。
付諸實踐
為什麼這很重要
- 成本與預算. 多年來,架構決策決定著效能和營運成本。
- 更明確的決策. 技術教育幫助團隊選擇正確的堆疊,而不僅僅是最新的堆疊。
- 品質管控. 更好的工程選擇可以減少生產中的可靠性事故。
留意
- 優化一項基準測試可以隱藏更廣泛的系統弱點。
- 基礎設施和維護成本常常被低估。
- 隨著系統變得更加複雜,安全性和可觀察性差距可能會擴大。
後續步驟
- 在實施之前定義延遲、品質和成本目標。
- 在實際負載和資料條件下進行基準測試。
- 儀器監控錯誤、漂移和使用者影響。
- 在擴展之前準備回滾和事件回應路徑。
不斷探索
常見問題
什麼是上下文壓縮與對話歷史管理?
對話歷史管理是保持長聊天或代理會話在模型上下文限制內的一組技術。主要做法包括丟棄舊回合、摘要早期內容、將整個歷史壓縮成簡明摘要以及清除舊工具輸出。這很重要,因為語言模型在呼叫之間沒有記憶,除非應用程式重新傳送。當歷史超出視窗時,必須裁剪某些內容,裁剪的內容決定助手是否保持連貫。
為什麼長時間的聊天會話變得越來越昂貴?
無狀態 API 處理每次呼叫的整個歷史記錄。隨著歷史記錄的增長,每個請求的代幣、成本和延遲也會隨之增長。
滑動視窗截斷的主要弱點是什麼?
只保留最近的轉彎既便宜又可預測,但任何掉出窗外的東西都會消失,除非它被固定住。
滾動匯總特有哪些風險?
每輪總結都是有損的。一個摘要中的錯誤會影響下一個摘要,因此在長時間的會話中錯誤可能會不斷累積。
壓縮通常在代理工具中產生什麼?
壓縮將會議壓縮為經過深思熟慮的簡報,並用它和最近的轉變開始一個新的背景。代理可以在沒有完整歷史記錄的情況下繼續。
為什麼在代理會話中清除舊工具結果如此有效?
工具輸出可能非常大,通常只能使用一次。將它們替換為佔位符可以釋放大量空間並保留呼叫發生的記錄。