Learning resourcesReviewed · Anthropic

Handle streaming refusals

Handle streaming refusals is an Anthropic guide for developers building Claude applications, with separate handling needed for refusals that arrive as ordinary text or API errors. It explains structured refusal signals, conversation resets and fallback options, with SDK examples in seven languages. Product-specific event formats and incomplete application setup require additional integration work.

Researched 25 Nov 2026

In short.

Handle streaming refusals is an Anthropic guide for developers handling Claude refusals during streamed responses, but its examples need adaptation before they can manage a complete application's recovery. It explains how to detect refusals in successful API responses, handle nullable explanatory fields and reset conversation context. Messages API and Managed Agents use different event shapes. Fallback implementation, exact billing rules and minimum SDK versions are not established here. No code was executed.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete supplied page at [https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals.md], including API differences, nullable details, reset guidance, SDK examples, fallback exceptions, migration notes and availability notices. Linked pages, SDK packages, pricing documentation and service behavior are outside the supplied evidence. No code, API calls, fallback requests or application journeys were executed.

The overview

Identity

Handle streaming refusals is a Claude Platform documentation page about detecting and responding to safety refusals while output is delivered in pieces. Its public documentation address is [https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals].

The supplied source was captured from [https://platform.claude.com/docs/en/test-and-evaluate/strengthen-guardrails/handle-streaming-refusals.md]. The resource is an implementation guide, with response examples and SDK snippets rather than a standalone application or installable library.

Assessment

The page is useful for integrations that currently treat successful HTTP responses as successful answers. It explains that a classifier refusal can arrive with HTTP 200, so monitoring only request errors can miss it. It also notes that refused Message Batch requests can appear as succeeded results.

Its strongest contribution is the event-handling contract: which fields to inspect, where they arrive and which descriptive fields may be missing. Production recovery needs further work because the snippets do not implement fallback, complete conversation management or user-facing recovery.

Users

The guide fits developers building Claude chat interfaces, streaming backends or agent applications. Beginners get a concrete refusal response and short examples; experienced developers get details relevant to event parsing, monitoring and migration.

Managed Agents users should read the product-specific availability section carefully. Its refusal detection rules differ from those shown in the Messages API examples, so the same handler cannot simply be assumed to cover both products.

Capabilities

The page explains refusal detection through stop_reason, structured details through stop_details, context reset, custom user messages and refusal-frequency monitoring. It supplies Python, TypeScript, C#, Go, Java, PHP and Ruby examples.

It distinguishes three refusal paths: streaming classifier refusals with a structured stop reason, input or copyright validation failures with HTTP 400 errors, and model-generated refusals expressed as ordinary response text. The streaming handler addresses the first path; the other two need separate handling.

It also describes recovery options involving another Claude model, including server-side fallback, SDK middleware and manual retries. Their implementations are referenced through links rather than demonstrated on this page.

Using it

Requirements

An application needs access to its streaming events and conversation state to apply the examples. The guide recommends an Anthropic SDK for this logic and provides no equivalent single-command cURL or CLI implementation. Installation instructions, authentication setup and minimum SDK versions are not supplied.

The text describes streaming refusal signals for Claude 4 and later. The Messages API availability table lists the feature as always on across supported platforms. Managed Agents is listed as always on but in beta on Claude API and Claude Platform on AWS. These product-specific statements matter more than assuming every platform has identical behavior.

Mechanism

For the Messages API, the page says a streaming classifier refusal ends the response with stop_reason set to refusal. That value and stop_details arrive on a message_delta event. The details object includes a type, category and human-readable explanation, but category and explanation can be null. Detection should therefore use the refusal signal rather than requiring a populated explanation.

For Managed Agents, a refused turn ends and the session becomes idle. The page directs developers to inspect stop_details.type or stop_reason.type on session.status_idle for refusal. It also says a refused turn can have stop_reason.type set to end_turn and can have null stop_details; it does not explain detection if both conditions occur together.

The page advises removing or rephrasing the triggering turn, or clearing the history, before continuing after a refusal. It warns that continuing with the unchanged context results in further refusals.

Setup

For a Messages API integration, the demonstrated pattern is to create an SDK client, start a streaming request and inspect message_delta events for stop_reason equal to refusal. The sample requests use claude-opus-5-5 and max_tokens of 1024; the page does not establish those values as mandatory.

Connect the refusal handler to the conversation history actually used in subsequent requests, and supply a user message when category or explanation is null. Several snippets clear a history variable while constructing request messages separately, so their reset functions need integration with the application's real state.

Fallback requires additional implementation. The page points to separate documentation for server-side configuration, middleware and manual retry handling.

Example

The Python example begins with an empty messages list and sends a user message containing Hello. While reading the stream, it checks each message_delta event. If the event reports refusal, it clears the stored messages, prints a reset notice and breaks out of the loop.

This illustrates a control-flow branch, not a demonstrated refusal: the supplied example has no recorded response showing that Hello triggers one. It also does not show retaining successful turns, displaying a refusal explanation or issuing a fallback request. Nothing was executed.

Assessment & evidence

Limitations

A stop_reason handler does not detect every refusal. Input-validation errors and refusals written as normal model text follow different paths. The Managed Agents null-field cases also leave an unresolved detection question.

The sample refusal response contains a text block alongside the refusal status. Applications should therefore decide how to present partial content when a refusal arrives; the examples do not implement that user experience.

Clearing the entire history discards conversation context. The page offers removing or rephrasing the triggering turn as alternatives, but it does not demonstrate selective history repair. The Java snippet also resets history without an explicit stream-iteration break, so the examples do not all show the same stopping behavior.

No detection accuracy, latency measurements, fallback success rates or executed compatibility results are supplied.

Design rationale

The documented response shapes support treating refusal as an application outcome alongside transport success and failure. A successful HTTP status, or a succeeded batch result, does not establish that the requested answer was delivered.

Nullable category and explanation fields make them suitable for optional user messaging rather than the primary detection condition. The product-specific event formats also support keeping Messages API and Managed Agents parsing explicit.

Recovery should account for the refusal type. The page identifies reasoning_extraction as an exception that requires changing the prompt instead of retrying on another model; a universal cross-model retry rule would miss that exception.

Alternatives

Within this page, recovery options include removing or rephrasing the triggering turn, clearing the conversation and using another Claude model through server-side fallback, SDK middleware or a manual retry. The page does not establish comparative reliability or cost for these options.

For broader coverage, it points to Refusals and fallback [https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback], Stop reasons and fallback [https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons] and Streaming messages [https://platform.claude.com/docs/en/build-with-claude/streaming]. Their contents were not supplied, so their implementation details are not assessed.

Evidence

The evidence is the complete supplied documentation page, including its front matter, response example, seven SDK snippets, migration notes and availability tables. It supports an account of documented behavior and visible example-code limitations.

No execution logs, independent measurements or actual refusal-and-recovery traces are included. Documented behavior has not been verified by running the examples.

Access & freshness

Costs

Exact costs are unknown. The page says usage metrics remain available on refused responses, while billing depends on the refusal's timing and category. It provides no prices or complete billing rules, so a refusal cannot be assumed to be free.

For manual fallback, the page advises passing the refusal's fallback-credit token to avoid paying prompt-cache cost twice. It states that Claude Haiku 5.5 refusals carry no fallback credit and that this model has no server-side fallback. The total cost of a retry is not established by this page.

Rights

The supplied page does not state a documentation license, a code-example license or redistribution permission. Rights to redistribute its text or snippets are unknown from this evidence.

Freshness

The supplied page was captured on 25 November 2026 at 04:20:22.730 UTC. It contains no explicit publication date, last-updated date or minimum SDK release.

Model identifiers, beta availability, fallback support and credit exceptions describe the captured documentation. Their continued applicability is unknown; confirm them against the documentation for the product and SDK used before implementation.

Suggested actions

For a Messages API application, add a refusal branch to the streaming-event handler and record refusals separately from HTTP errors. Retain usage information and provide a readable default message when structured explanations are null.

Connect context reset to the history actually sent on the next request. Decide how partial output should appear and keep separate handling for validation failures and ordinary-text refusals.

Before adding fallback, establish the applicable billing and credit rules, account for the Haiku 5.5 exceptions and route reasoning_extraction refusals toward prompt changes. For Managed Agents, resolve the documented null-detail and end_turn cases before relying on the proposed checks as complete detection.

Evidence for this report

selected-page.txtselected · checked 2026-11-25