While these tips apply broadly to all Claude models, you can find prompting tips specific to extended thinking models here.

预填充功能仅适用于非扩展思考模式。目前不支持与扩展思考一起使用。

在使用 Claude 时,您有独特的能力通过预填充 Assistant 消息来引导其回复。这种强大的技术允许您指导 Claude 的行动,跳过前言,强制使用特定格式如 JSON 或 XML,甚至帮助 Claude 在角色扮演场景中保持角色一致性。

在某些 Claude 表现不如预期的情况下,几句预填充的句子可以极大地改善 Claude 的表现。一点预填充就能产生很大的效果!

如何预填充 Claude 的回复

要进行预填充,请在 Assistant 消息中包含所需的初始文本(Claude 的回复将从 Assistant 消息结束的地方继续):

import anthropic

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-opus-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is your favorite color?"},
        {"role": "assistant", "content": "As an AI assistant, I don't have a favorite color, But if I had to pick, it would be green because"}  # Prefill here
    ]
)

预填充内容不能以尾随空格结束。像 "As an AI assistant, I " 这样的预填充(末尾有空格)将导致错误。

示例

示例 1:控制输出格式并跳过前言

高级用户提示:预填充 { 强制 Claude 跳过前言并直接输出 JSON 对象。这样更简洁、更清晰,也更容易被程序解析而无需额外处理。

示例 2:在角色扮演场景中保持角色

角色扮演提示:预填充一个带方括号的 [角色名称] 可以提醒 Claude 保持角色,即使在更长更复杂的对话中也是如此。当与 system 参数中的角色提示结合使用时,这种方法特别强大。