langchain-failover
Tiny, dependency-light multi-model orchestration for LangChain chat models — two strategies for running more than one model behind one interface:
- Failover (
FailoverChatModel) — for resilience. Serve from a primary, transparently fall back to a secondary on connection errors, switch back the moment the primary recovers. Tool-calling keeps working across the failover. - Tier-split (
TieredChatAgent) — for cost/latency. Run the tool-gathering loop on a cheap/local model, then compose the final answer on a frontier model. The long generation moves off the contended box.
They compose: either tier of a TieredChatAgent can itself be a
FailoverChatModel. Depends only on langchain-core.
See it in use: aisoc: One LLM, Eight Hats — a multi-agent AI SOC that fails a local LLM over to a backup and offloads final-answer synthesis to a frontier model.
Failover — for resilience
from langchain_openai import ChatOpenAI
from langchain_failover import FailoverChatModel
primary = ChatOpenAI(base_url="http://gpu-box:8001/v1", api_key="x", model="local")
backup = ChatOpenAI(base_url="http://cpu-box:8002/v1", api_key="x", model="local")
llm = FailoverChatModel(primary=primary, secondary=backup)
llm.invoke("Summarise this incident…") # served by primary
# …primary host dies…
llm.invoke("And the next one?") # transparently served by backup
# …primary comes back…
llm.invoke("One more") # back on primary, logged as recovered
Tier-split — for cost/latency
A tool-calling agent spends almost all of its tokens and wall-clock on the loop
(decide a call, read the result, decide the next) — cheap reasoning. Writing the
final answer is the part that wants a stronger model. They don't have to be the
same model. TieredChatAgent runs the gathering loop on a cheap/local gatherer
and composes the answer on a frontier composer:
from langchain_failover import TieredChatAgent
agent = TieredChatAgent(
gatherer=local_llm, # cheap/local — drives the tool loop (tools are bound for you)
composer=frontier_llm, # frontier — writes the final answer from gathered data
tools=[search, lookup_host, get_timeline],
)
agent.invoke("What changed in the incident overnight?").content
The gatherer is told to gather, then stop — it doesn't write the prose answer.
A structural guard (is_premature_marker) catches the model trying to answer
before calling any tool and nudges it to gather first, so the composer never
writes an answer from zero data. On a contended local GPU this routinely turns a
multi-minute final turn into a couple of seconds, because the long generation
moves off the busy box. Running your own loop? synthesize_answer(composer, query, messages) is the compose step on its own.
Install
pip install langchain-failover # core
pip install "langchain-failover[openai]" # + langchain-openai for create_failover_llm
Why not RunnableWithFallbacks / .with_fallbacks()?
LangChain ships per-invocation fallbacks, and they're great for what they do. This package exists for the cases they don't cover well:
- Stateful recovery.
FailoverChatModelremembers which leg it's on and logs the transition both ways (activeproperty tells you)..with_fallbacks()is stateless — every call re-tries the (possibly still-dead) primary first. - Tool-calling survives failover.
bind_toolsis overridden to bind on both legs and return anotherFailoverChatModel. With strict langchain-core (>=1.4, whereBaseChatModel.bind_toolsraises by default) naïve wrappers break at bind time; agents using this one keep working. - Connection-aware, not blanket. It only fails over on connection/network
errors (walking the exception's
__cause__/__context__chain, so a socket error wrapped three layers deep still counts). AValueErrorfrom a bad prompt propagates instead of being silently retried on a second endpoint. - Mid-stream safety. During
stream(), it only fails over if the primary dies before the first token — so you never get duplicated, half-streamed output.
Local-model convenience
If you run local OpenAI-compatible servers (vLLM, mlx-lm, Ollama, LM Studio) and
don't want to hardcode model names, create_failover_llm auto-discovers the served
model id from each endpoint's /models:
from langchain_failover import create_failover_llm
llm = create_failover_llm(
primary_url="http://localhost:8001/v1",
secondary_url="http://localhost:8002/v1",
)
Bonus helper
extract_token_metrics(response.response_metadata) normalises token counts and
timings across OpenAI-compatible and Ollama metadata shapes into a single
{input_tokens, output_tokens, prompt_time, generation_time} dict.
License
MIT
Metadata
Release files for langchain-failover 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_failover-0.2.2.tar.gz | 20.1 kB | With provenance | Details for langchain_failover-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_failover-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for langchain_failover-0.2.2-py3-none-any.whl |
Total release size: 34.9 kB
Release files / langchain_failover-0.2.2.tar.gz
| Download URL | langchain_failover-0.2.2.tar.gz |
|---|---|
| Size | 20.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f379799895ea46dabbfd9f8458613c5991b566797643856bece985c45cd785b5
|
|
BLAKE2b-256 checksum How to use checksums |
14dcbe253c42171addbc0a6b5ee302e41590c3cb0e7e74a6a9d4144c807f4555
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency logRelease files / langchain_failover-0.2.2-py3-none-any.whl
| Download URL | langchain_failover-0.2.2-py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
156411946f793c4c5471d79955933fa56a28d9ef90f50d261feb805dd0e7d40e
|
|
BLAKE2b-256 checksum How to use checksums |
306ec952b15d57f4654379445c85786f9203085ceb73e0debe542c0cb4474c20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency logSecurity for langchain-failover 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for langchain_failover-0.2.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.