Class BetaRefusalFallbackInterceptor
-
- All Implemented Interfaces:
-
com.anthropic.core.http.Interceptor
public final class BetaRefusalFallbackInterceptor implements Interceptor
An Interceptor that retries refused beta
/v1/messagesrequests down a fallback chain.Only requests made through the beta API surface (
client.beta().messages()) are handled; non-betaclient.messages()requests pass through untouched.When a non-streaming response comes back with
stop_reason: "refusal", the request is retried with each entry of the fallback chain applied as a patch over the original params (a field the entry sets overrides the original value, a field it sets tonullis removed from the retried request, and a field it omits keeps the original value; entries never see each other's overrides) — passing along the refusal'sfallback_credit_token, which refunds the retry's cache-miss cost — until a model accepts or the chain is exhausted. A message served by a fallback retry mirrors the server-side stitched envelope: onefallbackcontent block per model boundary (from: the model that refused, as the caller spelled it;to: the next entry's model) is prepended to the serving hop's content. Abetween_toolsthinking config is sent to a fallback asdisabledunless the entry setsthinkingitself.When a streaming response ends in
stop_reason: "refusal", a second request is issued to the fallback model — carrying the refused model's partial output as a trailing assistant prefill when the refusal grants one (fallback_has_prefill_claim), plus the refusal'sfallback_credit_token— and the fallback's events are spliced onto the still-open stream, so the client sees one continuous message in the server-sidefallbackswire shape: afallbackcontent block at each model boundary, monotonic block indices, and per-hopusage.iterationson the finalmessage_delta. Onlymodelandspeedare honored from each entry on this path (abetween_toolsthinking config is sent asdisabledwhatever the entry sets). The other per-entry overrides are ignored, because the credit token is tied to the refused request's body. Aspeedvalue replaces the request's, and anullspeedomits it.The fallback-credit beta the credit tokens require is sent by default on every request the interceptor handles — the original request included, since refusals only carry a
fallback_credit_tokenwhen the beta is enabled; the Builder.betas option controls this.In both modes a fallback that itself refuses with a fresh credit token continues down the chain. A streaming fallback whose prefill the server rejects (HTTP 400) is retried once without it; a streaming fallback whose request fails outright is skipped — its token was never redeemed, so it carries to the next entry. A streaming refusal with no
fallback_credit_tokento retry is surfaced to the client and reported once per interceptor with a warning on System.err; one that exhausts the chain is surfaced silently — its terminalmessage_deltaalready reports every hop.A
speed: "fast"request that falls back to an entry with nospeed, and is rejected with a 429 or 529, is retried once on that entry withoutspeed; if that streaming fallback is then retried without its prefill, that retry omitsspeedtoo. Setspeedtofaston the entry to keep it fast-only.To keep later requests on the model that accepted, pass a BetaFallbackState via the RequestOptions.Builder.fallbackState request option; requests sharing that state start directly at the pinned fallback. Reuse one state across whatever scope the pin should apply to — typically a conversation.
Example:
AnthropicClient client = AnthropicOkHttpClient.builder() .fromEnv() .addInterceptor(BetaRefusalFallbackInterceptor.builder() .addFallback(Model.CLAUDE_OPUS_4_8) .build()) .build(); BetaFallbackState fallbackState = BetaFallbackState.create(); BetaMessage message = client.beta().messages().create( params, RequestOptions.builder().fallbackState(fallbackState).build());
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classBetaRefusalFallbackInterceptor.BuilderA builder for BetaRefusalFallbackInterceptor.
-
Method Summary
Modifier and Type Method Description final BetaRefusalFallbackInterceptor.BuildertoBuilder()HttpClientintercept(HttpClient httpClient)final static BetaRefusalFallbackInterceptor.Builderbuilder()Returns a mutable builder for constructing an instance of BetaRefusalFallbackInterceptor. -
-
Method Detail
-
toBuilder
final BetaRefusalFallbackInterceptor.Builder toBuilder()
-
intercept
HttpClient intercept(HttpClient httpClient)
-
builder
final static BetaRefusalFallbackInterceptor.Builder builder()
Returns a mutable builder for constructing an instance of BetaRefusalFallbackInterceptor.
-
-
-
-