In short.
This guide explains Anthropic SDK request and response hooks for Claude developers, with installation prerequisites and minimum SDK versions left unspecified. It provides examples in Python, TypeScript, C#, Go, Java, PHP and Ruby, explains handler ordering, and introduces refusal fallback. Managed Agents middleware sees client calls to the agent API but cannot see model calls inside the agent loop. Its availability description also mixes Available and Beta labels. No examples were executed, and runtime behavior was not assessed.
Researched and written by an AI model and checked by a second AI model. How it works
Coverage includes the complete selected documentation page at https://platform.claude.com/docs/en/cli-sdks-libraries/middleware.md, including its metadata, language examples, diagram, middleware ordering, transport notes, fallback overview and availability notices. All linked pages are excluded. No SDK source or separate library files were supplied, and no explicit licence notice appears in the captured page. Nothing was executed; runtime behavior, compatibility, performance and billing remain untested.
The overview
identity
SDK middleware is an Anthropic documentation guide explaining how middleware, also called interceptors, runs around requests made through its SDKs. The selected source is the complete page at [https://platform.claude.com/docs/en/cli-sdks-libraries/middleware.md].
The page teaches an SDK feature through registration examples, ordering rules, transport configuration and API availability notes.
assessment
This is a useful focused reference for developers who want shared logging, request annotation or retry behavior around Claude SDK calls. It explains where a handler runs and how to forward a request, which are essential details when several handlers must work together.
It provides less help with initial installation or production configuration. Minimum SDK versions, complete authentication setup and detailed fallback configuration are not established by this page.
users
The guide suits application developers using the Anthropic SDK in Python, TypeScript, C#, Go, Java, PHP or Ruby. Beginners can use the logging examples to understand code that runs before and after a request; experienced developers get explicit ordering and transport boundaries.
Developers working with Managed Agents need to account for a narrower observation scope: middleware sees their SDK client's calls to the Managed Agents API, while model calls inside the agent loop remain outside that hook.
capabilities
A middleware handler can inspect or replace an outgoing request before forwarding it, then inspect or replace the response returned by the next handler. The page identifies logging, custom retries and request annotation as uses.
The SDK also supplies refusal-fallback middleware that retries a request on a fallback model when Claude returns stop_reason equal to refusal. The page does not establish that this built-in feature handles other failure types, and delegates setup and eligible-model details to linked documentation.
Using it
requirements
Using the examples requires the relevant Anthropic SDK, a configured client and familiarity with that language's request and response handling. Each handler receives a request and a way to invoke the next handler, then returns the resulting response.
Minimum SDK and runtime versions, installation commands, credentials and backend-specific configuration are unspecified. Availability must also be checked for the intended API: the page describes Messages API support as available across supported platforms, but gives mixed availability labels for Managed Agents.
mechanism
Handlers wrap one another. With handlers A and B registered in that order, the outgoing request passes through A, then B, then the SDK core. The response returns through B and then A. Code before the forwarding call runs on the outgoing path; code after it runs on the return path.
Client-level middleware runs before middleware supplied as a per-request option. A custom HTTP client receives outgoing requests after the middleware chain; each SDK client uses only one HTTP client, and supplying one replaces the default.
setup
Define a handler that performs any pre-request work, forwards the request and returns the response after any post-request work. Register it through the SDK client's middleware or interceptor configuration.
The examples use middleware=[logging_middleware] in Python, a middleware array in TypeScript and Ruby, Handlers in C#, option.WithMiddleware in Go, addInterceptor in Java, and a middleware entry within requestOptions in PHP. Multiple handlers are registered in the intended outgoing order. Complete imports and installation instructions are not supplied for every example.
example
The Python example defines logging_middleware(request, call_next). It prints the HTTP method and URL, calls call_next(request), prints the response status code and returns the response. Registering that function on the client demonstrates logging around the forwarded request.
The Go example additionally records elapsed time and returns an error immediately if forwarding fails. These are documented examples; no code was executed and no resulting logs, timings or responses were observed.
Assessment & evidence
limitations
Managed Agents middleware cannot observe model calls made inside the agent loop. The same section labels availability as Available while its platform table says Beta; the page does not resolve that discrepancy.
The guide does not specify streaming-response behavior, retry limits, fallback configuration or minimum SDK versions. PHP interactions with retries, errors and request signing are referred to another page. Working behavior across SDK releases and backends was not tested.
reasoning
The documented hook is a practical fit when the same request or response handling should apply across multiple SDK calls. Its explicit nesting order helps developers reason about which handler observes a request first and a returning response last.
Fit depends on the required observation point. SDK request logging matches the documented scope, while inspecting a Managed Agent's internal model calls requires capabilities this page explicitly excludes. Refusal fallback also needs its separate configuration reference before implementation.
alternatives
The page identifies a custom HTTP client as another configuration option for proxy settings, custom TLS or connection pooling. That option replaces the default transport and receives requests after middleware processing.
It provides no comparison with third-party middleware libraries, observability products or other SDKs. Their suitability, compatibility and costs are not assessed.
evidence
The evidence consists of the complete selected Anthropic documentation page at [https://platform.claude.com/docs/en/cli-sdks-libraries/middleware.md]. It contains seven language examples, a request-flow diagram, middleware ordering rules, a custom HTTP client description, a refusal-fallback overview and API availability notes.
Linked pages, SDK implementations, package manifests and licence files were outside the supplied scope. Capabilities described here are documentation claims; no examples or SDK calls were executed.
Access & freshness
costs
Costs are unknown from this page. It supplies no SDK fees, model prices, request charges or billing rules for fallback attempts.
Refusal fallback sends another request, but the page does not quantify its cost or latency implications. No performance measurements or operating-cost estimates are provided.
rights
The supplied page contains code examples but no explicit licence or redistribution permission for the documentation or snippets. Reuse and redistribution rights are therefore unknown from this evidence.
SDK package licences and service terms are not included in the selected page and were not assessed.
freshness
The page was captured on 25 November 2026 at 20:29:12.733 UTC. The supplied content gives no publication date, last-updated date or minimum SDK release for the feature.
The metadata labels the listed platforms stable, while the body gives Managed Agents both Available and Beta labels. Those statements do not establish unambiguous current support for every SDK and API combination.
actions
Confirm that the installed SDK release exposes the registration interface shown for the chosen language. Start with the method, URL and status logging pattern, register handlers in the required order, and verify successful and failed request behavior in the intended environment.
Confirm Managed Agents availability before depending on it. For refusal fallback, consult the linked setup reference at [https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#client-side-fallback]. For PHP retry, error and signing interactions, consult [https://platform.claude.com/docs/en/cli-sdks-libraries/sdks/php#middleware]. Those linked pages were not assessed here.
