Loading...
  • 构建
  • 管理
  • 模型与定价
  • 客户端 SDK
  • API 参考
Search...
⌘K
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
  • 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
  • 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 API 中的流式拒绝处理,包括 API 响应格式、实现指南和最佳实践。

从 Claude 4 模型开始,Claude API 的流式响应在流式分类器介入处理潜在的政策违规时会返回 stop_reason: "refusal"。这项新的安全功能有助于在实时流式传输期间保持内容合规性。

要了解有关 Claude Sonnet 4.5 API 安全过滤器触发的拒绝的更多信息,请参阅了解 Sonnet 4.5 的 API 安全过滤器。

API 响应格式

当流式分类器检测到违反 Anthropic 政策的内容时,API 会返回此响应:

{
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello.."
    }
  ],
  "stop_reason": "refusal"
}

不包含额外的拒绝消息。您必须处理响应并提供适当的面向用户的消息。

拒绝后重置上下文

当您收到 stop_reason: refusal 时,您必须通过删除或更新被拒绝的轮次来重置对话上下文,然后再继续。尝试在不重置的情况下继续将导致持续拒绝。

即使响应被拒绝,响应中仍会提供使用指标用于计费目的。

您将被计费直到拒绝为止的输出令牌。

如果在使用 Claude Sonnet 4.5 或 Opus 4.1 时频繁遇到 refusal 停止原因,您可以尝试更新 API 调用以使用 Haiku 4.5 (claude-haiku-4-5-20251001),它具有不同的使用限制。了解更多关于理解 Sonnet 4.5 的 API 安全过滤器。

实现指南

以下是如何在您的应用程序中检测和处理流式拒绝的方法:

当前拒绝类型

API 目前以三种不同的方式处理拒绝:

拒绝类型响应格式发生时机
流式分类器拒绝stop_reason: refusal在流式传输期间当内容违反政策时
API 输入和版权验证400 错误代码当输入未通过验证检查时
模型生成的拒绝标准文本响应当模型本身决定拒绝时

未来的 API 版本将扩展 stop_reason: refusal 模式以统一所有类型的拒绝处理。

最佳实践

  • 监控拒绝:在您的错误处理中包含 stop_reason: refusal 检查
  • 自动重置:在检测到拒绝时实现自动上下文重置
  • 提供自定义消息:为拒绝发生时创建用户友好的消息以改善用户体验
  • 跟踪拒绝模式:监控拒绝频率以识别您的提示中的潜在问题

迁移说明

  • 未来的模型将扩展此模式到其他拒绝类型
  • 规划您的错误处理以适应拒绝响应的未来统一

Was this page helpful?

  • API 响应格式
client = anthropic.Anthropic()
messages = []


def reset_conversation():
    """拒绝后重置对话上下文"""
    global messages
    messages = []
    print("Conversation reset due to refusal")


try:
    with client.messages.stream(
        max_tokens=1024,
        messages=messages + [{"role": "user", "content": "Hello"}],
        model="claude-sonnet-4-6",
    ) as stream:
        for event in stream:
            # 检查消息增量中的拒绝
            if hasattr(event, "type") and event.type == "message_delta":
                if event.delta.stop_reason == "refusal":
                    reset_conversation()
                    break
except Exception as e:
    print(f"Error: {e}")