Skip to main content

Weighted, sticky load balancing across lmux providers, with failover

Project description

lmux-load-balancer

Weighted, sticky load balancing across lmux providers, with failover.

LoadBalancerProvider is a meta-provider: it holds no SDK of its own and distributes each chat request across a group of other providers already registered in a Registry. Selection is weighted and deterministic per routing key, so one conversation pins to a single endpoint (keeping its provider-side prompt cache warm) while different conversations spread across endpoints.

Install

uv add lmux-load-balancer

Usage

Register the underlying providers, then register a LoadBalancerProvider under its own prefix with one or more endpoint groups. A group maps a logical model name (the part after this provider's prefix) to "prefix/model" endpoints and their weights:

from lmux import Registry, UserMessage
from lmux_anthropic import AnthropicProvider
from lmux_aws_bedrock import BedrockProvider
from lmux_load_balancer import LoadBalancerProvider, LoadBalancerParams, LoadBalancerMetadata

registry = Registry()
registry.register("anthropic", AnthropicProvider())
registry.register("bedrock", BedrockProvider())

registry.register(
    "balanced",
    LoadBalancerProvider(
        registry,
        groups={
            "sonnet": {
                "bedrock/anthropic.claude-sonnet-4": 0.7,
                "anthropic/claude-sonnet-4-20250514": 0.3,
            },
        },
    ),
)

# Distributed by weight, independently per call:
response = registry.chat("balanced/sonnet", [UserMessage(content="Hello")])

# Sticky: the same key always tries the same endpoint first, preserving cache locality:
response = registry.chat(
    "balanced/sonnet",
    [UserMessage(content="Hello")],
    provider_params=LoadBalancerParams(sticky_key="conversation-123"),
)

meta = response.provider_metadata
if isinstance(meta, LoadBalancerMetadata):
    print(meta.primary, meta.served, meta.attempted)

Weights are honored in aggregate across many distinct keys, not per call: a sticky conversation's whole traffic lands on the endpoint its key hashes to. A weight of 0 disables an endpoint. The load balancer delegates to each child by model string and does not forward its own provider_params, so each child uses the default params it was registered with.

The served endpoint is reported on provider_metadata as a LoadBalancerMetadata (primary selected, served actual, attempted in order) on the non-streaming response and on the first streaming chunk.

Failover

On a retryable error (rate limit, timeout, or a provider/server error) the request falls through to the next endpoint in a deterministic, per-key order. Non-retryable errors (auth, invalid request, not found, unsupported feature) propagate immediately, and when every candidate is exhausted the underlying error is re-raised unchanged.

LoadBalancerParams.failover selects the behavior:

Value Behavior
"always" (default) Fall through to the next endpoint on a retryable error.
"never" Try only the selected endpoint; never fall through.
"unless_sticky" Fall through only for keyless calls; a sticky call stays pinned to its endpoint.

For streaming, failover applies only before the first chunk is produced; once output has started it cannot switch endpoints.

Because "never" and "unless_sticky" can leave a sticky conversation pinned to a down endpoint, keep a direct-provider entry as a last resort where availability matters more than cache locality.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lmux_load_balancer-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lmux_load_balancer-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file lmux_load_balancer-0.1.0.tar.gz.

File metadata

  • Download URL: lmux_load_balancer-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lmux_load_balancer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 409565ac286489fac57a503a00af04b3e00d01e81d85da1e7a09308447d7b39a
MD5 bf0ec0341874d43ff9f700caa4a85795
BLAKE2b-256 d3bd26b829e33f0c56dd5beb1ba1ffc9c212cecfe7a6c321161c3508216d231d

See more details on using hashes here.

Provenance

The following attestation bundles were made for lmux_load_balancer-0.1.0.tar.gz:

Publisher: publish.yml on cluebbehusen/lmux

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lmux_load_balancer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lmux_load_balancer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be51b9d2035e456fd5d0eb5c29a5cb075feeb66a1d50957a10a8fae6285149b7
MD5 350000b3af94b12b35f848c1a9bc9305
BLAKE2b-256 84338e99292d12d9079972c9f80aa33c40ecc91f8b8fe8027e4c955b1cb8ef52

See more details on using hashes here.

Provenance

The following attestation bundles were made for lmux_load_balancer-0.1.0-py3-none-any.whl:

Publisher: publish.yml on cluebbehusen/lmux

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page