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 中的串流拒絕,包括如何偵測和處理政策違規情況。

從 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:
            # 檢查訊息 delta 中的拒絕
            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}")