# `OpenrouterSdk.Middleware`
[🔗](https://github.com/zmzlois/openrouter_sdk/blob/v0.1.0/lib/openrouter_sdk/middleware.ex#L1)

extension hook for upstream consumers.

this package intentionally ships zero retry / backoff / rotation
policy. instead, consumers compose their own by implementing this
behaviour and registering it in `:openrouter_sdk, :middleware`:

    config :openrouter_sdk,
      middleware: [
        {MyApp.Retry, max: 3, base: 200},
        {MyApp.Rotate, models: ["openai/gpt-4o", "anthropic/claude-sonnet-4-6"]}
      ]

the `next` callback runs the rest of the pipeline (eventually
reaching the finch call). a middleware that wants to retry simply
re-invokes `next.(request)`; a rotator can swap fields on the
request before passing it on.

# `next`

```elixir
@type next() :: (request() -&gt; response())
```

# `request`

```elixir
@type request() :: OpenrouterSdk.Client.Request.t()
```

# `response`

```elixir
@type response() :: {:ok, term()} | {:error, OpenrouterSdk.Error.t()}
```

# `call`

```elixir
@callback call(request(), next(), opts :: keyword()) :: response()
```

# `build`

```elixir
@spec build([module() | {module(), keyword()}], next()) :: next()
```

fold a list of middleware over the terminal `runner` function,
producing a single function that runs the whole chain.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
