Skip to main content

agrag.agents.model.build_model_middleware

build_model_middleware(clients:list[LLMClientConfig], *, strategy:Literal['single', 'fallback', 'round_robin'] = 'single') -> list[Any]

Build agent middleware composing multiple clients per strategy.

The agent calls clients[0] as its primary model. With more than one client, the returned middleware teaches the agent loop to use the rest: "fallback" tries the other clients in order when the primary model call fails, and "round_robin" rotates across every client per model call.

Parameters:

  • clients (list[LLMClientConfig]) – The configured clients, in priority order.
  • strategy (Literal['single', 'fallback', 'round_robin']) – How to compose clients. "single" ignores all but the first client.

Returns:

  • list[Any] – Middleware for create_deep_agent/create_agent; empty when there
  • list[Any] – is nothing to compose.

Raises: