AI / LangGraph LangChain Interview questions
What are chain fallbacks and retries?
Fallbacks and retries are resilience mechanisms built into LangChain Runnables that make production chains tolerant of transient failures and model quality issues.
Fallbacks — .with_fallbacks() attaches one or more backup Runnables that are tried in order if the primary raises an exception. You can fall back to a cheaper model, a different provider, or a static response:
from langchain_openai import ChatOpenAI from langchain_anthropic import ChatAnthropic chain = ( ChatOpenAI(model="gpt-4o") .with_fallbacks([ ChatAnthropic(model="claude-3-sonnet-20240229"), ChatOpenAI(model="gpt-4o-mini"), ]) ) # If gpt-4o fails, tries Claude; if that fails, tries gpt-4o-mini
Retries — .with_retry() retries the same Runnable on failure with configurable stop conditions and wait strategies:
from langchain_openai import ChatOpenAI resilient_llm = ChatOpenAI().with_retry( retry_if_exception_type=(RateLimitError, Timeout), stop_after_attempt=3, wait_exponential_jitter=True, ) chain = prompt | resilient_llm | StrOutputParser()
You can combine both: retry first (for transient errors), then fall back (if the model is genuinely unavailable). Retries are best for rate-limit errors; fallbacks are best for model outages or quality failures (e.g. the primary model produces invalid JSON).
More Related questions...