What hybrid means
A hybrid setup answers on your own box first and sends only the overflow to the cloud. The app keeps one socket, the OpenAI-compatible API from Chapter 7. A router sits behind that socket. For each request, the router picks the engine.
Chapter 13 built the box. This chapter answers two questions. What happens when the box is full? What happens when the question is too hard for the local model?
The routing rules
Four rules decide where a request goes.
- Fallback. When every slot is busy, the gate sends the request to the cloud dock. Step 2 above shows the queue clear while the median stays near 6 s.
- Privacy. A private request never crosses to the dock, even when the box is busy. It waits in the queue. Step 3 shows the price: private requests wait longer.
- Cost and latency. Cloud providers differ in price, first-token delay and speed. Step 5 swaps the provider. The routes stay the same, and only the meter rate changes.
- Budget. Cloud spend has a monthly limit. When the day's share is spent, the dock shuts and requests wait for the box again (Step 4).
The prices in the figure are illustrative. Check the current prices before you plan a real budget.
One socket, one config
One small file turns these rules into settings. LiteLLM is one router that reads such a file. It serves the same OpenAI-compatible socket and maps each alias to an engine.
model_list:
- model_name: private # local only: no fallback is listed for it
litellm_params: {model: ollama/qwen3:32b, api_base: "http://localhost:11434"}
- model_name: fast # local first
litellm_params: {model: ollama/qwen3:32b, api_base: "http://localhost:11434"}
- model_name: strong # the cloud dock
litellm_params: {model: provider/cloud-model, api_key: os.environ/CLOUD_API_KEY}
router_settings:
timeout: 20 # a full box counts as a failed call
fallbacks: [{fast: [strong]}]
litellm_settings: {max_budget: 150, budget_duration: 30d}
The app names only an alias: private, fast or strong. A fallback fires when the local call fails or times out, so a short timeout turns a full box into a fallback. Other routers do the same job: OpenRouter, Ollama's cloud models, or a small Python shim.
Explain it
- "Why not send everything to the cloud?" Private data would leave your box, and the bill grows with every request. The local box answers most requests at no cost per request.
- "What changes in the app when the router changes?" Nothing. The app talks to one socket and names an alias.
- "Why do private requests wait longer?" They cannot use the dock, so they queue for the same few slots.
Exercises
- Set 16 users and find the lowest budget that keeps the dock open all day.
- Add a second cloud alias to the YAML that falls back from
strong. - Write a Python shim that sends prompts with a
[private]tag to the local socket only.