压缩与保留思考
在具有保留思考功能的模型上,压缩后(或在您自己的摘要或截断之后)保留的轮次中的思考何时保持有效,以及如何进行检查。
除非您会将思考块发送回具有"preserved thinking"(保留思考)的模型,并在压缩块之后(或在您自己的摘要或截断之后)保留一些轮次,否则可以跳过本页。"Kept turns"(保留的轮次)是指紧随该块之后的轮次。它们要么是您未纳入压缩请求的近期轮次(如保留最近轮次的压缩中所述),要么是在 API 编写摘要期间到达的轮次(如后台压缩中所述)。
具有保留思考的模型会将较早的思考块与生成它们的对话进行核对。摘要会替换该对话的一部分,但当摘要由 API 编写时,"prefix check"(前缀检查)会接受这种替换,因此保留的轮次中的思考可以保持有效。
保留的思考保持有效的条件
只要以下所有条件都成立,保留的轮次中的思考块就会保持有效:
- 压缩请求在具有保留思考的模型上运行。 此条件涵盖自思考块生成以来的每一次压缩请求,而不仅仅是最近的一次。满足此条件的一种方法是将每个压缩请求都发送到该对话所使用的模型。
- 保留的轮次紧跟在被摘要的消息之后,并且您原样发送它们。 请完全按照历史记录中的样子发送每条保留的消息,并且不要再次发送被摘要的消息。不要在最后一条被摘要的消息与第一条保留的消息之间跳过或添加任何消息。第一条保留的消息的角色还必须与最后一条被摘要的消息不同,并且不能是对话中途的
role: "system"消息。否则,API 会将其合并到最后一条被摘要的消息中。确保第一条保留的消息正确的一种方法是,对您已发送过的某个请求的messages进行完全一致的压缩。这样,保留的轮次就会从 Claude 对该请求的回复开始。 system和tools不发生变化。 它们在压缩请求中与生成保留的思考的请求中保持一致,并且在后续请求中也保持不变。例外情况是标记为defer_loading: true且尚未被任何内容引用过的工具。要安全地更改它们,请参阅更改系统提示或工具。
如果某个条件不成立,API 仍会在后续请求中接受压缩块。失败的是保留的思考,发生在第一个发送它的后续请求上。压缩请求也会对其携带的思考块运行前缀检查,因此更改了 system 或 tools,或编辑了某条消息,也可能导致该压缩请求失败。关于届时会发生什么,请参阅设置 prefix_mismatch_behavior。
再次压缩而不破坏较早的思考
您可以再次压缩并保留一些轮次:新块涵盖旧摘要以及压缩请求中其后的每条消息,而您未纳入该请求的任何轮次都是新块保留的轮次。
要使某个轮次的思考在两次压缩后仍保持有效,这两次压缩都必须在具有保留思考的模型上运行。
在思考块生成之前进行的压缩不会对其产生影响。在某个块就位之后生成的思考会绑定到该块,并且在满足条件的后续压缩中保持有效。
更改系统提示或工具
后续请求可以使用另一个 system、tools 或支持按需压缩的模型,API 仍会接受该块。此类更改可能会使保留的思考失效。
要在不使任何保留的思考失效的情况下更改 system 或 tools,请先压缩整个对话,使其不保留任何轮次。然后在下一个请求中进行更改。
要在不改动 system 或 tools 的情况下添加指令或更改可用工具,请将更改追加到 messages 中,如更改工具或指令而不会导致前缀检查失败中所述。
被摘要的轮次可能包含对话中途的系统消息,即 messages 中的 role: "system" 条目。它们会与其余内容一起被摘要,因此其文本指令和按消息设置 effort 级别不再生效(请参阅限制以及与其他功能的交互)。其工具更改可以沿用:
- 在压缩请求上发送
inline-tools-2026-09-15beta 标头。返回的块随后会在tool_changes字段中记录这些更改。 - 原样发送回该块。API 随后会在包含该块的每个请求上应用所记录的更改。如果缺少该字段,API 会接受该块,但不会应用任何更改,因此请再次声明这些更改。
- 在包含该块的每个请求上都发送相同的标头。
您可以再次声明指令、effort 级别或工具更改。请将其放入一条 role: "system" 消息中,紧跟在下一个请求中新的 user 消息之后。这样,请求的 messages 会按以下顺序排列:
- 压缩块。
- 保留的轮次(如有)。
- 您的
user消息。 role: "system"消息。
在后续请求中,保持这些条目不变,并在该系统消息之后添加新的轮次。不要将系统消息放在保留的轮次之前或之间,放在那里时,它会使其后的每个保留的思考块失效。
当您的代码编写摘要时
无论是否使用按需压缩,本节都适用于所有运行前缀检查的场景。
如果由您的代码编写摘要,那么您在摘要之后保留的轮次中的思考将无法通过前缀检查,因为您的摘要替换了该思考之前的消息。要保留该思考,请让 API 编写摘要。
以下轮次也是如此:在令牌阈值处通过压缩后暂停而保留的轮次、在您的代码于后台编写摘要期间 Claude 生成的轮次,以及截断之后的轮次。
移除这些轮次的 thinking 和 redacted_thinking 块并保留其其他块,或者在包含它们的每个请求上发送 "drop_block"。无论采用哪种方式,Claude 读取这些轮次时都不会带有该思考,并且输出令牌可能会增加。
如果您只将摘要和下一条指令作为一条用户消息发送,则不会发送任何较早的思考,之后的思考也是有效的。
检查保留的思考是否有效
压缩响应不会告诉您保留的思考是否有效,而替换后的第一个请求会。要在测试中进行检查:
- 在开启思考的情况下进行一段简短的对话。使用会运行前缀检查的模型,并在每个步骤中都使用该模型,因为无法读取思考块的模型会直接丢弃它而不报错。
- 压缩较早的轮次,并至少保留一个包含思考块的轮次。
- 发送下一个请求,依次包含该块、保留的轮次和一条新的
user消息,并将thinking.block_binding.prefix_mismatch_behavior设置为"error"。 - 读取结果。如果返回 200 响应且其
input_transformations数组为空,则表示所有思考块都通过了前缀检查,且没有块被丢弃。如果返回 400 错误并提示 “the block is bound to a different conversation”,则表示有思考块未通过前缀检查。错误消息以第一个未通过前缀检查的块的路径开头,如错误:The block is bound to a different conversation所示。
除了 compact-2026-09-04 beta 标头之外,prefix_mismatch_behavior 设置还需要 thinking-binding-controls-2026-08-01 beta 标头。
以下程序运行这四个步骤。它会打印保留的轮次中包含的思考块数量以及 input_transformations 中的条目数量;没有条目表示保留的思考有效:
此工作流需要的逻辑超出了单个 shell 命令的能力范围。请使用 Anthropic SDK 来运行它。
此工作流需要的逻辑超出了单个 shell 命令的能力范围。请使用 Anthropic SDK 来运行它。
from anthropic.types.beta import BetaMessageParam, BetaThinkingConfigParam
client = anthropic.Anthropic()
# Claude Fable 5.1 是首个会将回传的思考内容与对话进行核对的模型。
MODEL = "claude-fable-5-1"
BETAS = ["compact-2026-09-04", "thinking-binding-controls-2026-08-01"]
SYSTEM = "You help plan a recipe app's release. Keep answers short."
# 设置为 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
THINKING: BetaThinkingConfigParam = {
"type": "adaptive",
"block_binding": {"prefix_mismatch_behavior": "error"},
}
# 1. 在开启思考的情况下进行一段简短对话。
history: list[BetaMessageParam] = [
{"role": "user", "content": "What are the main entities in the app's data model?"}
]
first = client.beta.messages.create(
model=MODEL,
max_tokens=8192,
system=SYSTEM,
betas=BETAS,
thinking=THINKING,
messages=history,
)
history += [
{"role": "assistant", "content": first.content},
{
"role": "user",
"content": "Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?",
},
]
second = client.beta.messages.create(
model=MODEL,
max_tokens=8192,
system=SYSTEM,
betas=BETAS,
thinking=THINKING,
messages=history,
)
history.append({"role": "assistant", "content": second.content})
thinking_blocks = sum(block.type == "thinking" for block in second.content)
print(f"Thinking blocks in the kept turn: {thinking_blocks}")
# 2. 总结第一轮。第二轮不包含在请求中。
summary = client.beta.messages.create(
model=MODEL,
max_tokens=4096,
system=SYSTEM,
betas=BETAS,
thinking=THINKING,
messages=history[:2],
compaction={"type": "summarize"},
)
if summary.stop_reason != "compaction":
raise SystemExit(f"No summary: {summary.stop_reason}")
# 3. 将该块放在保留的轮次之前,然后提出下一个问题。
history = [
{"role": "assistant", "content": summary.content},
*history[2:],
{"role": "user", "content": "Which day should the release go out?"},
]
third = client.beta.messages.create(
model=MODEL,
max_tokens=8192,
system=SYSTEM,
betas=BETAS,
thinking=THINKING,
messages=history,
)
# 4. 返回 200 且没有被丢弃的块,说明保留的思考保持有效。
print(f"Dropped thinking blocks: {len(third.input_transformations or [])}")
const client = new Anthropic();
// Claude Fable 5.1 是首个会将回传的思考与对话进行比对检查的模型。
const model: Anthropic.Model = "claude-fable-5-1";
const betas: Anthropic.Beta.AnthropicBeta[] = [
"compact-2026-09-04",
"thinking-binding-controls-2026-08-01"
];
const systemPrompt = "You help plan a recipe app's release. Keep answers short.";
// 设为 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
const thinking: Anthropic.Beta.Messages.BetaThinkingConfigParam = {
type: "adaptive",
block_binding: { prefix_mismatch_behavior: "error" }
};
// 1. 在开启思考的情况下进行一段简短对话。
let history: Anthropic.Beta.Messages.BetaMessageParam[] = [
{ role: "user", content: "What are the main entities in the app's data model?" }
];
const first = await client.beta.messages.create({
model,
max_tokens: 8192,
system: systemPrompt,
betas,
thinking,
messages: history
});
history.push(
{ role: "assistant", content: first.content },
{
role: "user",
content:
"Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?"
}
);
const second = await client.beta.messages.create({
model,
max_tokens: 8192,
system: systemPrompt,
betas,
thinking,
messages: history
});
history.push({ role: "assistant", content: second.content });
const thinkingBlocks = second.content.filter((block) => block.type === "thinking").length;
console.log(`Thinking blocks in the kept turn: ${thinkingBlocks}`);
// 2. 总结第一轮。第二轮不包含在请求中。
const summary = await client.beta.messages.create({
model,
max_tokens: 4096,
system: systemPrompt,
betas,
thinking,
messages: history.slice(0, 2),
compaction: { type: "summarize" }
});
if (summary.stop_reason !== "compaction") {
throw new Error(`No summary: ${summary.stop_reason}`);
}
// 3. 将该块放在保留的轮次之前,然后提出下一个问题。
history = [
{ role: "assistant", content: summary.content },
...history.slice(2),
{ role: "user", content: "Which day should the release go out?" }
];
const third = await client.beta.messages.create({
model,
max_tokens: 8192,
system: systemPrompt,
betas,
thinking,
messages: history
});
// 4. 返回 200 且没有被丢弃的块,说明保留的思考保持有效。
console.log(`Dropped thinking blocks: ${third.input_transformations?.length ?? 0}`);
using Anthropic.Models.Beta;
using Anthropic.Models.Beta.Messages;
using Model = Anthropic.Models.Messages.Model;
AnthropicClient client = new();
// Claude Fable 5.1 是首个会根据对话检查回传思考的模型。
const Model ModelId = Model.ClaudeFable5_1;
AnthropicBeta[] betas = [AnthropicBeta.Compact2026_09_04, AnthropicBeta.ThinkingBindingControls2026_08_01];
const string SystemPrompt = "You help plan a recipe app's release. Keep answers short.";
// 设置为 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
BetaThinkingConfigAdaptive thinking = new()
{
BlockBinding = new() { PrefixMismatchBehavior = BetaThinkingPrefixMismatchBehavior.Error },
};
// 1. 在启用思考的情况下进行一段简短对话。
List<BetaMessageParam> history =
[
new BetaUserMessageParam { Content = "What are the main entities in the app's data model?" },
];
var first = await client.Beta.Messages.Create(new MessageCreateParams
{
Model = ModelId,
MaxTokens = 8192,
System = SystemPrompt,
Betas = [.. betas],
Thinking = thinking,
Messages = history,
});
history.AddRange(
[
new BetaAssistantMessageParam
{
Content = first.Content.Select(block => new BetaContentBlockParam(block.Json)).ToList(),
},
new BetaUserMessageParam
{
Content = "Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?",
},
]);
var second = await client.Beta.Messages.Create(new MessageCreateParams
{
Model = ModelId,
MaxTokens = 8192,
System = SystemPrompt,
Betas = [.. betas],
Thinking = thinking,
Messages = history,
});
history.Add(new BetaAssistantMessageParam
{
Content = second.Content.Select(block => new BetaContentBlockParam(block.Json)).ToList(),
});
var thinkingBlocks = second.Content.Count(block => block.TryPickThinking(out _));
Console.WriteLine($"Thinking blocks in the kept turn: {thinkingBlocks}");
// 2. 总结第一轮对话。第二轮不放入请求中。
var summary = await client.Beta.Messages.Create(new MessageCreateParams
{
Model = ModelId,
MaxTokens = 4096,
System = SystemPrompt,
Betas = [.. betas],
Thinking = thinking,
Messages = history[..2],
Compaction = new BetaCompactionConfig(), // type defaults to "summarize"
});
if (summary.StopReason != BetaStopReason.Compaction)
{
throw new InvalidOperationException($"No summary: {summary.StopReason?.Raw()}");
}
// 3. 将该块放在保留的轮次之前,然后提出下一个问题。
history =
[
new BetaAssistantMessageParam
{
Content = summary.Content.Select(block => new BetaContentBlockParam(block.Json)).ToList(),
},
.. history[2..],
new BetaUserMessageParam { Content = "Which day should the release go out?" },
];
var third = await client.Beta.Messages.Create(new MessageCreateParams
{
Model = ModelId,
MaxTokens = 8192,
System = SystemPrompt,
Betas = [.. betas],
Thinking = thinking,
Messages = history,
});
// 4. 返回 200 且没有块被丢弃,说明保留的思考保持有效。
Console.WriteLine($"Dropped thinking blocks: {third.InputTransformations?.Count ?? 0}");
ctx := context.Background()
client := anthropic.NewClient()
// Claude Fable 5.1 是首个会将回传的思考与对话进行比对检查的模型。
const model = anthropic.ModelClaudeFable5_1
betas := []anthropic.AnthropicBeta{
anthropic.AnthropicBetaCompact2026_09_04,
anthropic.AnthropicBetaThinkingBindingControls2026_08_01,
}
system := []anthropic.BetaTextBlockParam{{Text: "You help plan a recipe app's release. Keep answers short."}}
// 设置为 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
thinking := anthropic.BetaThinkingConfigParamUnion{
OfAdaptive: &anthropic.BetaThinkingConfigAdaptiveParam{
BlockBinding: anthropic.BetaThinkingBlockBindingParam{
PrefixMismatchBehavior: anthropic.BetaThinkingPrefixMismatchBehaviorError,
},
},
}
// 1. 开启思考,进行一段简短的对话。
history := []anthropic.BetaMessageParam{
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("What are the main entities in the app's data model?")),
}
first, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: model,
MaxTokens: 8192,
System: system,
Betas: betas,
Thinking: thinking,
Messages: history,
})
if err != nil {
log.Fatal(err)
}
history = append(history,
first.ToParam(),
anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?")),
)
second, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: model,
MaxTokens: 8192,
System: system,
Betas: betas,
Thinking: thinking,
Messages: history,
})
if err != nil {
log.Fatal(err)
}
history = append(history, second.ToParam())
thinkingBlocks := 0
for _, block := range second.Content {
if _, ok := block.AsAny().(anthropic.BetaThinkingBlock); ok {
thinkingBlocks++
}
}
fmt.Printf("Thinking blocks in the kept turn: %d\n", thinkingBlocks)
// 2. 对第一轮进行摘要。第二轮不包含在请求中。
summary, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: model,
MaxTokens: 4096,
System: system,
Betas: betas,
Thinking: thinking,
Messages: history[:2],
Compaction: anthropic.BetaCompactionConfigUnionParam{
OfSummarize: &anthropic.BetaSummarizeCompactionParam{},
},
})
if err != nil {
log.Fatal(err)
}
if summary.StopReason != anthropic.BetaStopReasonCompaction {
log.Fatalf("No summary: %s", summary.StopReason)
}
// 3. 将该块放在保留的轮次之前,然后提出下一个问题。
history = slices.Concat(
[]anthropic.BetaMessageParam{summary.ToParam()},
history[2:],
[]anthropic.BetaMessageParam{anthropic.NewBetaUserMessage(anthropic.NewBetaTextBlock("Which day should the release go out?"))},
)
third, err := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{
Model: model,
MaxTokens: 8192,
System: system,
Betas: betas,
Thinking: thinking,
Messages: history,
})
if err != nil {
log.Fatal(err)
}
// 4. 返回 200 且没有块被丢弃,说明保留的思考保持有效。
fmt.Printf("Dropped thinking blocks: %d\n", len(third.InputTransformations))
import com.anthropic.models.beta.AnthropicBeta;
import com.anthropic.models.beta.messages.BetaCompactionConfig;
import com.anthropic.models.beta.messages.BetaContentBlock;
import com.anthropic.models.beta.messages.BetaMessageParam;
import com.anthropic.models.beta.messages.BetaStopReason;
import com.anthropic.models.beta.messages.BetaThinkingBlockBinding;
import com.anthropic.models.beta.messages.BetaThinkingConfigAdaptive;
import com.anthropic.models.beta.messages.BetaThinkingPrefixMismatchBehavior;
import com.anthropic.models.beta.messages.MessageCreateParams;
// Claude Fable 5.1 是首个会将回传的思考与对话进行比对检查的模型。
static final Model MODEL = Model.CLAUDE_FABLE_5_1;
static final List<AnthropicBeta> BETAS = List.of(
AnthropicBeta.COMPACT_2026_09_04,
AnthropicBeta.THINKING_BINDING_CONTROLS_2026_08_01
);
static final String SYSTEM = "You help plan a recipe app's release. Keep answers short.";
// 设为 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
static final BetaThinkingConfigAdaptive THINKING = BetaThinkingConfigAdaptive.builder()
.blockBinding(BetaThinkingBlockBinding.builder()
.prefixMismatchBehavior(BetaThinkingPrefixMismatchBehavior.ERROR)
.build())
.build();
void main() {
var client = AnthropicOkHttpClient.fromEnv();
// 1. 在启用思考的情况下进行一段简短对话。
var history = new ArrayList<BetaMessageParam>();
history.add(BetaMessageParam.ofUser("What are the main entities in the app's data model?"));
var firstParams = MessageCreateParams.builder()
.model(MODEL)
.maxTokens(8192)
.system(SYSTEM)
.betas(BETAS)
.thinking(THINKING)
.messages(history)
.build();
var first = client.beta().messages().create(firstParams);
history.add(first.toParam());
history.add(BetaMessageParam.ofUser(
"Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?"));
var secondParams = MessageCreateParams.builder()
.model(MODEL)
.maxTokens(8192)
.system(SYSTEM)
.betas(BETAS)
.thinking(THINKING)
.messages(history)
.build();
var second = client.beta().messages().create(secondParams);
history.add(second.toParam());
long thinkingBlocks = second.content().stream().filter(BetaContentBlock::isThinking).count();
IO.println("Thinking blocks in the kept turn: " + thinkingBlocks);
// 2. 总结第一轮。第二轮不包含在请求中。
var firstTurn = history.subList(0, 2);
var summaryParams = MessageCreateParams.builder()
.model(MODEL)
.maxTokens(4096)
.system(SYSTEM)
.betas(BETAS)
.thinking(THINKING)
.messages(firstTurn)
.compaction(BetaCompactionConfig.builder().build()) // type defaults to "summarize"
.build();
var summary = client.beta().messages().create(summaryParams);
if (!summary.stopReason().map(BetaStopReason.COMPACTION::equals).orElse(false)) {
throw new IllegalStateException("No summary: " + summary.stopReason().orElseThrow());
}
// 3. 将该块放在保留的轮次之前,然后提出下一个问题。
firstTurn.clear();
history.addFirst(summary.toParam());
history.add(BetaMessageParam.ofUser("Which day should the release go out?"));
var thirdParams = MessageCreateParams.builder()
.model(MODEL)
.maxTokens(8192)
.system(SYSTEM)
.betas(BETAS)
.thinking(THINKING)
.messages(history)
.build();
var third = client.beta().messages().create(thirdParams);
// 4. 返回 200 且没有被丢弃的块,说明保留的思考保持有效。
IO.println("Dropped thinking blocks: " + third.inputTransformations().map(List::size).orElse(0));
}
use Anthropic\Beta\AnthropicBeta;
use Anthropic\Beta\Messages\BetaCompactionConfig;
use Anthropic\Beta\Messages\BetaMessageParam;
use Anthropic\Beta\Messages\BetaMessageParam\Role;
use Anthropic\Beta\Messages\BetaStopReason;
use Anthropic\Beta\Messages\BetaThinkingBlock;
use Anthropic\Beta\Messages\BetaThinkingBlockBinding;
use Anthropic\Beta\Messages\BetaThinkingConfigAdaptive;
use Anthropic\Beta\Messages\BetaThinkingPrefixMismatchBehavior;
$client = new Client();
// Claude Fable 5.1 是首个根据对话检查发回的思考内容的模型。
const MODEL = Model::CLAUDE_FABLE_5_1;
const BETAS = [AnthropicBeta::COMPACT_2026_09_04, AnthropicBeta::THINKING_BINDING_CONTROLS_2026_08_01];
const SYSTEM = "You help plan a recipe app's release. Keep answers short.";
// 使用 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
$thinking = BetaThinkingConfigAdaptive::with(
blockBinding: BetaThinkingBlockBinding::with(
prefixMismatchBehavior: BetaThinkingPrefixMismatchBehavior::ERROR,
),
);
// 1. 在开启思考的情况下进行一段简短对话。
$history = [
BetaMessageParam::with(role: Role::USER, content: "What are the main entities in the app's data model?"),
];
$first = $client->beta->messages->create(
model: MODEL,
maxTokens: 8192,
system: SYSTEM,
betas: BETAS,
thinking: $thinking,
messages: $history,
);
$history[] = BetaMessageParam::with(role: Role::ASSISTANT, content: $first->content);
$history[] = BetaMessageParam::with(
role: Role::USER,
content: 'Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?',
);
$second = $client->beta->messages->create(
model: MODEL,
maxTokens: 8192,
system: SYSTEM,
betas: BETAS,
thinking: $thinking,
messages: $history,
);
$history[] = BetaMessageParam::with(role: Role::ASSISTANT, content: $second->content);
$thinkingBlocks = count(array_filter($second->content, fn ($block) => $block instanceof BetaThinkingBlock));
printf("Thinking blocks in the kept turn: %d\n", $thinkingBlocks);
// 2. 总结第一个轮次。第二个轮次不包含在请求中。
$summary = $client->beta->messages->create(
model: MODEL,
maxTokens: 4096,
system: SYSTEM,
betas: BETAS,
thinking: $thinking,
messages: array_slice($history, 0, 2),
compaction: new BetaCompactionConfig(), // type defaults to 'summarize'
);
if ($summary->stopReason !== BetaStopReason::COMPACTION->value) {
throw new \RuntimeException("No summary: {$summary->stopReason}");
}
// 3. 将该块放在保留的轮次之前,然后提出下一个问题。
$history = [
BetaMessageParam::with(role: Role::ASSISTANT, content: $summary->content),
...array_slice($history, 2),
BetaMessageParam::with(role: Role::USER, content: 'Which day should the release go out?'),
];
$third = $client->beta->messages->create(
model: MODEL,
maxTokens: 8192,
system: SYSTEM,
betas: BETAS,
thinking: $thinking,
messages: $history,
);
// 4. 返回 200 且没有被丢弃的块,说明保留的思考仍然有效。
printf("Dropped thinking blocks: %d\n", count($third->inputTransformations ?? []));
client = Anthropic::Client.new
# Claude Fable 5.1 是首个会根据对话检查发回的思考内容的模型。
MODEL = Anthropic::Model::CLAUDE_FABLE_5_1
BETAS = [
Anthropic::AnthropicBeta::COMPACT_2026_09_04,
Anthropic::AnthropicBeta::THINKING_BINDING_CONTROLS_2026_08_01
]
SYSTEM = "You help plan a recipe app's release. Keep answers short."
# 使用 "error" 时,未通过前缀检查的思考块会导致请求失败并返回 400。
THINKING = Anthropic::Beta::BetaThinkingConfigAdaptive.new(
block_binding: Anthropic::Beta::BetaThinkingBlockBinding.new(
prefix_mismatch_behavior: Anthropic::Beta::BetaThinkingPrefixMismatchBehavior::ERROR
)
)
# 1. 在开启思考的情况下进行一段简短对话。
history = [{ role: "user", content: "What are the main entities in the app's data model?" }]
first = client.beta.messages.create(
model: MODEL,
max_tokens: 8192,
system_: SYSTEM,
betas: BETAS,
thinking: THINKING,
messages: history
)
history += [
{ role: "assistant", content: first.content },
{
role: "user",
content: "Testing starts on Tuesday, March 3, 2026, takes 10 weekdays, and pauses on March 9 and March 16. On which date does it end?"
}
]
second = client.beta.messages.create(
model: MODEL,
max_tokens: 8192,
system_: SYSTEM,
betas: BETAS,
thinking: THINKING,
messages: history
)
history << { role: "assistant", content: second.content }
thinking_blocks = second.content.count { it.is_a?(Anthropic::Beta::BetaThinkingBlock) }
puts "Thinking blocks in the kept turn: #{thinking_blocks}"
# 2. 总结第一个轮次。第二个轮次不包含在请求中。
summary = client.beta.messages.create(
model: MODEL,
max_tokens: 4096,
system_: SYSTEM,
betas: BETAS,
thinking: THINKING,
messages: history.first(2),
compaction: { type: "summarize" }
)
abort "No summary: #{summary.stop_reason}" unless summary.stop_reason == :compaction
# 3. 将该块放在保留的轮次之前,然后提出下一个问题。
history = [
{ role: "assistant", content: summary.content },
*history.drop(2),
{ role: "user", content: "Which day should the release go out?" }
]
third = client.beta.messages.create(
model: MODEL,
max_tokens: 8192,
system_: SYSTEM,
betas: BETAS,
thinking: THINKING,
messages: history
)
# 4. 返回 200 且没有被丢弃的块,说明保留的思考仍然有效。
puts "Dropped thinking blocks: #{(third.input_transformations || []).length}"
Thinking blocks in the kept turn: 1
Dropped thinking blocks: 0
在生产环境中,当某个条件不成立时,"drop_block" 可确保请求依然成功,并且响应会在 input_transformations 中列出每个被丢弃的块,并附带 reason: "prefix_binding_mismatch"。如果某个条目的 path 位于保留的轮次中,则表示该轮次的思考未满足某个条件。关于如何针对这些条目设置告警,请参阅监控生产环境,并找出无错误丢失的推理。在 Claude Sonnet 5.5 和 Claude Haiku 5.5 上,block_binding 仅适用于 thinking: {"type": "adaptive"}。在这些模型上使用任何其他思考设置时,请确保所有条件均成立,或从保留的轮次中移除 thinking 和 redacted_thinking 块。
可用性
压缩与保留思考在部分模型上处于 beta 阶段。
API 支持
Messages API
- 可用性:可用
所需的 beta 请求头
Beta 请求头让您可以在实验性功能和新模型能力成为标准 API 的一部分之前访问它们。
compact-2026-09-04