Skip to content

Routing

Retry, fallback, and target resolution — the policy layer that adapters are forbidden from containing. Behavior is described in routing.

anyinfer.Route dataclass

Route(
    targets: tuple[Target, ...],
    retry: Retry = Retry(),
    health_gate: bool = True,
    health_ttl_s: float = 30.0,
    context_window_targets: tuple[Target, ...] = (),
    content_policy_targets: tuple[Target, ...] = (),
)

An ordered fallback chain and the policy applied to it.

Beyond the general chain, two failure classes get their own chains because the right next target differs by why the first one failed: a prompt that overflowed one model's context needs a larger model, not another same-sized one, and a content-policy refusal needs a differently-governed provider, not a retry.

Attributes:

Name Type Description
targets tuple[Target, ...]

Targets to try in order.

retry Retry

Retry policy applied per target.

health_gate bool

Skip targets whose health probe recently failed.

health_ttl_s float

How long a health failure suppresses a target.

context_window_targets tuple[Target, ...]

Chain used after a ContextLengthError. Empty means "use targets".

content_policy_targets tuple[Target, ...]

Chain used after a content-filter refusal. Empty means "use targets".

of classmethod

of(*targets: Target, retry: Retry | None = None) -> Route

Build a route from positional targets.

specialized_chain_for

specialized_chain_for(
    error: ProviderError,
) -> tuple[Target, ...]

The fallback chain that fits this failure, or () for the general one.

coerce classmethod

coerce(value: Route | Target | Sequence[Target]) -> Route

Accept a route, a single target string, or a sequence of targets.

anyinfer.Retry dataclass

Retry(
    max_attempts: int = 2,
    backoff_base_s: float = 0.5,
    backoff_max_s: float = 30.0,
    retry_on: Callable[[ProviderError], bool] | None = None,
)

Per-target retry policy.

Attributes:

Name Type Description
max_attempts int

Total attempts per target, including the first.

backoff_base_s float

Base for exponential backoff.

backoff_max_s float

Ceiling for any single delay.

retry_on Callable[[ProviderError], bool] | None

Overrides the default predicate. The default declines deterministic failures (auth, context length) and otherwise follows error.retryable.

should_retry

should_retry(error: ProviderError) -> bool

Whether error is worth retrying under this policy.

anyinfer.ResolvedTarget dataclass

ResolvedTarget(
    provider_id: str,
    model: str,
    via_alias: str | None = None,
)

A target after alias and provider-alias resolution.

Attributes:

Name Type Description
provider_id str

Normalized id of the provider that will serve the request.

model str

The model identifier, verbatim as the provider expects it.

via_alias str | None

The catalog alias this target was resolved from, if any.

__str__

__str__() -> str

Render as the canonical provider:model spelling.