• Messages
  • Managed Agents
  • Admin
Search...
⌘K
CLI, SDKs, and libraries
Overview
ant CLI
QuickstartAuthentication optionsUsing the CLIScripting and automation
Client SDKs
MiddlewarePythonTypeScriptC#GoJavaPHPRuby
Libraries and integrations
Apple Foundation ModelsOpenAI SDK compatibility
Log in
Middleware
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
CLI, SDKs, and libraries/Client SDKs

SDK middleware

Intercept and modify requests and responses in the Anthropic SDKs.

The Anthropic SDKs provide a middleware (or interceptor) hook that lets you run code before a request is sent and after the response is received. Use middleware for cross-cutting concerns such as logging, custom retries, request annotation, and refusal fallback handling.

Each middleware can inspect or replace the request before calling next(), and the response after next() returns.

Registering middleware

Each middleware is a function that receives the outgoing request and a next callable. Call next to forward the request to the rest of the chain (or directly to the SDK core if this is the last middleware), and return its response. Anything before the next call runs on the way out; anything after runs on the way back.

Middleware ordering

When you register multiple middleware, they apply in the order given: the first middleware's "before" code runs first, and its "after" code runs last. Middleware registered on the client runs before middleware passed as a per-request option.

In the Go SDK, repeated option.WithMiddleware calls concatenate (client first, then method). In the other SDKs, pass an array; later entries wrap inner.

Replacing the HTTP client

Each SDK also accepts a custom HTTP client (for proxy configuration, custom TLS, or connection pooling). Only one HTTP client is used per SDK client; setting it replaces the default. The custom HTTP client receives requests after all middleware has run.

Built-in middleware

The SDKs ship a refusal-fallback middleware that automatically retries requests Claude Fable 5 declines on a fallback model. See Detect and retry on a fallback model for setup and per-language examples.

Was this page helpful?

  • Registering middleware
  • Middleware ordering
  • Replacing the HTTP client
  • Built-in middleware