Calling external AI providers (like OpenAI, Gemini, or Claude) is a common task in modern web applications. However, these services can sometimes be slow, return errors, or become completely unavailable. This can lead to cascading failures that bring your entire application down. To prevent this, you can use the
Circuit Breaker pattern, which isolates failing services and even allows for a graceful failover to a backup provider. In Ruby, the
stoplight gem provides a straightforward way to implement this pattern and build a resilient client, in this article we will use it with
ruby_llm to handle AI providers. To see this implementation in action and explore the details, be sure to check out the
repository containing the code examples used in this article.