---
title: 壓縮概覽
url: https://platform.claude.com/docs/zh-TW/build-with-claude/compaction
description: 了解壓縮的作用、隨需壓縮與在 token 閾值時進行壓縮有何不同，以及哪個頁面涵蓋您的任務。
---

「Compaction」（壓縮）會將對話中較舊的輪次替換為 Claude 在伺服器上撰寫的摘要，因此您不需要自己編寫任何摘要程式碼。它讓長對話或代理任務保持在上下文視窗內，並讓作用中的上下文保持精簡，因為回應品質會隨著對話增長而下降。

如果您想跳過此概覽，請從[隨需壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand)開始，將壓縮加入您的應用程式。雖然它和[在 token 閾值時進行壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold)都處於 beta 階段，但隨需壓縮涵蓋了更常見的使用案例。若要依規則清除舊的工具結果或舊的思考區塊，而非對其進行摘要，請參閱[上下文編輯](https://platform.claude.com/docs/zh-TW/build-with-claude/context-editing)。

## 選擇如何壓縮

只要可以使用，就使用隨需壓縮。

|                                                                                                              | [隨需壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand)                                                                     | [在 token 閾值時壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold)                                                                                                                | 您自己的摘要器（[在用戶端壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/preserved-thinking#custom-compaction-on-the-client)） |
| ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| **由誰決定時機**                                                                                                   | 您，透過傳送請求                                                                                                                                                  | API，當輸入 token 達到您設定的觸發值時                                                                                                                                                                                      | 您                                                                                                                              |
| **您需撰寫的程式碼**                                                                                                 | 一個[壓縮迴圈](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#compact-in-a-loop)，用於請求摘要並將其替換進去                                   | 在您的一般請求上加入一個參數                                                                                                                                                                                                | 摘要呼叫、其提示，以及歷史記錄的改寫                                                                                                             |
| **之後您要傳回的內容**                                                                                                | 將傳回的區塊放在 `messages` 的最前面，取代它所摘要的訊息                                                                                                                        | 照常附加回應。API 會捨棄該區塊之前的內容                                                                                                                                                                                        | 您的摘要，作為您自己的一則訊息                                                                                                                |
| **最近的輪次逐字保留**                                                                                                | 是：[保留最近輪次的壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-keep-recent-turns)                                                      | 是，透過[壓縮後暫停](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#pausing-after-compaction)並重新插入它們                                                                                    | 是                                                                                                                              |
| **在背景執行**                                                                                                    | 是：[在背景壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-background)                                                                 | 否：它在達到閾值的請求中執行                                                                                                                                                                                                | 是，在您自己的程式碼中                                                                                                                    |
| **保留的輪次保有其思考內容**，適用於具備[保留思考](https://platform.claude.com/docs/zh-TW/build-with-claude/preserved-thinking)的模型 | 是，需符合[壓縮與保留思考](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-thinking-blocks)中的條件                                                   | 否：在 Claude Fable 5.1、Claude Opus 5.5、Claude Sonnet 5.5 和 Claude Haiku 5.5 上，請移除或捨棄您重新插入之輪次中的思考內容（請參閱[閾值壓縮範例](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#examples)）         | 否：該思考內容會[無法通過檢查](https://platform.claude.com/docs/zh-TW/build-with-claude/preserved-thinking#keep-tail-compaction)             |
| **Beta 標頭、參數與平台**                                                                                            | `compact-2026-09-04` 以及頂層的 `compaction` 參數。平台：請參閱[隨需壓縮相容性清單](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#compatibility) | 請參閱[閾值壓縮相容性清單](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#compatibility)與[基本用法](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#basic-usage) | 無。它在您的程式碼中執行                                                                                                                   |
| **適合選擇的情況**                                                                                                  | 您的應用程式需要控制壓縮發生的時機、無法在撰寫摘要時暫停，或必須保留最近的輪次及其思考內容                                                                                                             | 您希望 API 在一般請求中管理上下文                                                                                                                                                                                           | 您已在執行自己的摘要器，且它會以摘要取代整個歷史記錄                                                                                                     |

## 隨需壓縮選項

[隨需壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand)展示了一個在您的應用程式等待時摘要整個對話的迴圈。此清單中的前兩個頁面會改變該迴圈的執行方式，您可以將它們結合使用。第三個則適用於您送回思考區塊並執行上述任一操作的情況。

* [保留最近輪次的壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-keep-recent-turns)：當最後幾個輪次必須逐字送達 Claude 時。摘要僅涵蓋較舊的輪次。
* [在背景中進行壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-background)：當對話無法為摘要暫停時。請求在工作繼續進行時執行，當區塊送達時您再將其替換進去。
* [壓縮與保留思考](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-thinking-blocks)：如果您在具有保留思考的模型上送回思考區塊，並且保留最近的輪次或在背景壓縮。否則請跳過它。
* [撰寫您自己的摘要提示](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#write-your-own-summarization-prompt)：當預設摘要捨棄了後續輪次所需的內容時。您的提示會取代預設提示。
* [再次壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#compact-again)：當一個已經以壓縮區塊開頭的對話再次變長時。

在其他頁面上尋找這些主題：

* [請求摘要](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#request-a-summary)位於隨需壓縮頁面。
* [從摘要繼續](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#continue-from-the-summary)位於隨需壓縮頁面。讓您保留的輪次中的思考保持有效的條件位於[壓縮與保留思考](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-thinking-blocks)。
* 未回傳摘要時：請參閱隨需壓縮頁面上的[處理缺少摘要或錯誤的情況](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#when-no-summary-comes-back)。
* 與 API 其他部分的搭配方式：請參閱隨需壓縮頁面上的[限制以及與其他功能的互動](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#how-it-fits-with-the-rest-of-the-api)。
* 了解用量：對於隨需壓縮，請參閱[計算壓縮用量](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#understanding-usage)；對於閾值壓縮，請參閱[了解用量](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#understanding-usage)。
* 相容性：每種類型都有自己的 beta 標頭，因此每個頁面都有自己的清單。請參閱[隨需相容性清單](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-on-demand#compatibility)或[閾值相容性清單](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold#compatibility)。

閾值壓縮有自己的頁面：[在 token 閾值時進行壓縮](https://platform.claude.com/docs/zh-TW/build-with-claude/compaction-threshold)。
