Module: Anthropic::Middleware

Included in:
BetaRefusalFallbackMiddleware
Defined in:
lib/anthropic/middleware.rb,
sig/anthropic/middleware.rbs

Overview

HTTP around-middleware.

A middleware is any object that responds to #call(request, nxt) and returns an APIResponse. nxt is itself a #call(request)-able that invokes the rest of the chain and, ultimately, a single HTTP attempt. The chain runs per attempt, inside the SDK's retry loop — the same placement as Go, TypeScript, Java, and Python.

Register middleware at the client level via Anthropic::Client.new(middleware: [...]), or per request via request_options: {middleware: [...]}. Request-level entries run innermost (below client-level entries), so client-level middleware still wraps every request a per-call middleware fabricates or retries.

On provider clients (Bedrock/Vertex/AWS), a provider middleware is appended below all user entries on every dispatch: it rewrites the canonical request into the provider's wire shape and applies provider auth (SigV4/OAuth), so user middleware always sees the canonical Anthropic request and every re-issued or retried leg is re-signed.

Examples:

log = ->(req, nxt) { res = nxt.call(req); LOGGER.info(res.status); res }
Anthropic::Client.new(middleware: [log])

Constant Summary collapse

Anthropic =

Returns:

  • (:APIRequest,)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_chain(list, terminal) ⇒ #call

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compose list into a single callable. list[0] is outermost.

Parameters:

  • list (Array<#call>)
  • terminal (#call)

    the innermost callable — the actual HTTP attempt

Returns:



428
429
430
431
432
# File 'lib/anthropic/middleware.rb', line 428

def build_chain(list, terminal)
  list.reverse.reduce(terminal) do |inner, mw|
    ->(req) { mw.call(req, inner) }
  end
end

Instance Method Details

#call(request, nxt) ⇒ Anthropic::APIResponse

Optional mixin that names the middleware contract for users who prefer a class to a lambda. include Anthropic::Middleware and implement #call(request, nxt). The SDK treats any #call(req, nxt) object as middleware (lambdas, procs, Methods included), so including this is for discovery and type-checking — under Sorbet the method is abstract, so a missing or mis-typed #call is a static error — not a runtime requirement.

Examples:

class AddTeamHeader
  include Anthropic::Middleware

  def initialize(team) = @team = team

  def call(request, nxt)
    nxt.call(request.with(headers: request.headers.merge("x-team" => @team)))
  end
end

Parameters:

Returns:

Raises:

  • (NotImplementedError)


416
417
418
# File 'lib/anthropic/middleware.rb', line 416

def call(request, nxt)
  raise NotImplementedError, "#{self.class} must implement #{Anthropic::Middleware}#call(request, nxt)"
end