@@ -166,64 +166,98 @@ LLM_ROUTER_MINIMUM=1 python3 -m llm_router_api.rest_api
166166
167167---
168168
169- # # Provider Selection
169+ # # ⚖️ Load Balancing Strategies
170+
171+ The ` llm-router` supports various strategies for selecting the most suitable provider
172+ when multiple options exist for a given model. This ensures efficient
173+ and reliable routing of requests. The available strategies are:
174+
175+ # ## 1. `balanced` (Default)
176+
177+ * ** Description:** This is the default strategy. It aims to distribute requests
178+ evenly across available providers by keeping track of how many times each provider has
179+ been used for a specific model. It selects the provider that has been used the least.
180+ * ** When to use:** Ideal for scenarios where all providers are considered equal
181+ in terms of capacity and performance. It provides a simple and effective way to balance the load.
182+ * ** Implementation:** Implemented in ` llm_router_api.base.lb.balanced.LoadBalancedStrategy` .
183+
184+ ### 2. ` weighted`
185+
186+ * ** Description:** This strategy allows you to assign static weights to providers.
187+ Providers with higher weights are more likely to be selected. The selection is deterministic,
188+ ensuring that over time, the request distribution closely matches the configured weights.
189+ * ** When to use:** Useful when you have providers with different capacities or performance
190+ characteristics, and you want to prioritize certain providers without needing dynamic adjustments.
191+ * ** Implementation:** Implemented in ` llm_router_api.base.lb.weighted.WeightedStrategy` .
192+
193+ ### 3. ` dynamic_weighted`
194+
195+ * ** Description:** An extension of the ` weighted` strategy. It not only uses weights
196+ but also tracks the latency between successive selections of the same provider.
197+ This allows for more adaptive routing, as providers with consistently high latency
198+ might be de-prioritized over time. You can also dynamically update provider weights.
199+ * ** When to use:** Recommended for dynamic environments where provider performance
200+ can fluctuate. It offers more sophisticated load balancing by considering both
201+ configured weights and real-time performance metrics (latency).
202+ * ** Implementation:** Implemented in ` llm_router_api.base.lb.weighted.DynamicWeightedStrategy` .
203+
204+ ### 4. ` first_available`
205+
206+ * ** Description:** This strategy selects the very first provider that is available.
207+ It uses Redis to coordinate across multiple workers, ensuring that only one
208+ worker can use a specific provider at a time.
209+ * ** When to use:** Suitable for critical applications where you need the fastest
210+ possible response and want to ensure that a request is immediately handled by any available
211+ provider, without complex load distribution logic. It guarantees that a provider,
212+ once taken, is exclusive until released.
213+ * ** Implementation:** Implemented in ` llm_router_api.base.lb.first_available.FirstAvailableStrategy` .
214+
215+ ** When using the** ` first_available` load balancing strategy, a ** Redis server is required**
216+ for coordinating provider availability across multiple workers.
217+
218+ The connection details for Redis can be configured using environment variables:
219+
220+ ` ` ` shell
221+ LLM_ROUTER_BALANCE_STRATEGY=" first_available" \
222+ LLM_ROUTER_REDIS_HOST=" your.machine.redis.host" \
223+ LLM_ROUTER_REDIS_PORT=redis_port \
224+ ` ` `
170225
171- The LLM‑router supports ** multiple providers** for a single model. Provider selection is handled by
172- the ** ProviderChooser** class, which delegates the choice to a configurable ** strategy** implementation.
226+ ** Installing Redis on Ubuntu**
173227
174- # ## Chooser
228+ To install Redis on an Ubuntu system, follow these steps:
175229
176- ` ` ` python
177- from llm_router_api.base.lb.chooser import ProviderChooser
178- from llm_router_api.base.lb.strategy import LoadBalancedStrategy
230+ 1. ** Update package list:**
179231
180- # By default the chooser uses the LoadBalancedStrategy
181- provider_chooser = ProviderChooser(strategy=LoadBalancedStrategy())
232+ ` ` ` shell
233+ sudo apt update
182234` ` `
183235
184- ` ProviderChooser.get_provider(model_name, providers)` receives the model name
185- and the list of provider configurations (as defined in ` models-config.json` ) and
186- returns the chosen provider dictionary.
236+ 2. ** Install Redis server:**
187237
188- # ## Strategy Interface
189-
190- All strategies must implement ` ChooseProviderStrategyI` :
191-
192- ` ` ` python
193- class ChooseProviderStrategyI(ABC):
194- @abstractmethod
195- def choose(self, model_name: str, providers: List[Dict]) -> Dict:
196- " " " Select one provider configuration for the given model." " "
197- raise NotImplementedError
238+ ` ` ` shell
239+ sudo apt install redis-server
198240` ` `
199241
200- # ## Built‑in Strategy: LoadBalancedStrategy
242+ 3. ** Start and enable Redis service:**
243+ The Redis service should start automatically after installation.
244+ To ensure it' s running and starts on system boot, you can use the following commands:
201245
202- The default ` LoadBalancedStrategy` distributes requests evenly across providers
203- by keeping an in‑memory usage counter per model/provider pair.
204-
205- ` ` ` python
206- class LoadBalancedStrategy(ChooseProviderStrategyI):
207- def __init__(self) -> None:
208- self._usage_counters: Dict[str, Dict[str, int]] = defaultdict(
209- lambda: defaultdict(int)
210- )
211-
212- def choose(self, model_name: str, providers: List[Dict]) -> Dict:
213- # selects the provider with the smallest usage count
214- ...
246+ ``` shell
247+ sudo systemctl status redis-server
248+ sudo systemctl enable redis-server
215249```
216250
217- # ## Current Setting
218-
219- In ** ` engine.py` ** the Flask engine creates the chooser like this:
251+ 4. **Configure Redis (optional):**
252+ The default Redis configuration (`/etc/redis/redis.conf`) is usually sufficient
253+ to get started. If you need to adjust settings (e.g., address, port),
254+ edit this file. After making configuration changes, restart the Redis server:
220255
221- ` ` ` python
222- self._provider_chooser = ProviderChooser(strategy=LoadBalancedStrategy())
256+ ```shell
257+ sudo systemctl restart redis-server
223258```
224259
225- Therefore, unless overridden, the application uses the ** load‑balanced** provider
226- selection strategy out of the box.
260+ ---
227261
228262### Extending with Custom Strategies
229263
0 commit comments