Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K
    快速开始
    Claude 简介快速入门
    模型与定价
    模型概览选择模型Claude 4.5 新功能迁移到 Claude 4.5模型弃用定价
    使用 Claude 构建
    功能概览使用 Messages API上下文窗口提示词最佳实践
    能力
    提示词缓存上下文编辑扩展思考工作量流式消息批量处理引用多语言支持Token 计数嵌入视觉PDF 支持Files API搜索结果结构化输出
    工具
    概览如何实现工具使用细粒度工具流式传输Bash 工具代码执行工具程序化工具调用计算机使用工具文本编辑器工具Web 获取工具Web 搜索工具内存工具工具搜索工具
    Agent Skills
    概览快速入门最佳实践在 API 中使用 Skills
    Agent SDK
    概览快速入门TypeScript SDKTypeScript V2(预览版)Python SDK迁移指南
    API 中的 MCP
    MCP 连接器远程 MCP 服务器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示词工程
    概览提示词生成器使用提示词模板提示词改进器清晰直接使用示例(多轮提示)让 Claude 思考(CoT)使用 XML 标签给 Claude 一个角色(系统提示词)预填充 Claude 的响应链接复杂提示词长上下文提示扩展思考提示
    测试与评估
    定义成功标准开发测试用例使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性缓解越狱流式拒绝减少提示词泄露保持 Claude 的角色
    管理和监控
    Admin API 概览使用和成本 APIClaude Code Analytics API
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    提示词工程

    预填充 Claude 的响应以获得更好的输出控制

    通过预填充 Assistant 消息来指导 Claude 的响应,跳过前言,强制特定格式,并在角色扮演场景中保持角色一致性。

    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-sonnet-4-5",
        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 对象。这更简洁、更简明,程序更容易解析,无需额外处理。 为了保证输出符合特定架构的 JSON,请考虑使用 Structured Outputs 而不是预填充。结构化输出确保 Claude 的响应始终与您定义的 JSON 架构相匹配,非常适合需要严格格式合规性的生产应用程序。

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

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


    使用消息

    查看更多预填充和其他 Messages API 模式的示例。

    提示库

    从为各种任务和用例精选的提示中获得灵感。

    GitHub 提示教程

    一个充满示例的教程,涵盖我们文档中的提示工程概念。

    Google Sheets 提示教程

    通过交互式电子表格提供的我们提示工程教程的轻量级版本。

    • 如何预填充 Claude 的响应