Agent Wranglers

API-78Recommended

Decide what happens when a provider is down

For each call to an outside API, choose one of three paths for when it fails. Show a clear message, serve a saved answer, or queue the work and send it later.

How to do it

Reads, such as exchange rates or shipping prices, can use the last good answer with its age shown. Writes that can wait, such as a shipment booking, go to a queue and are sent when the provider is back. Writes that cannot wait show a message and keep the order in a state you can resume. After a run of failures, stop calling for a pause, so a dead provider does not hold up every request. The same switch for models is in keep working when the provider fails. Jobs that keep failing go to a dead letter queue. Test every path as test how your code handles each outside service failing says.

Examples

JavaScript example

async function shippingPrice(parcel) {
  try {
    const price = await carrier.quote(parcel);
    await priceCache.set(parcel.zone, price);
    return { price, fresh: true };
  } catch (error) {
    const saved = await priceCache.get(parcel.zone);
    if (!saved) throw error;
    return { price: saved, fresh: false };
  }
}

Python example

async def shipping_price(parcel):
    try:
        price = await carrier.quote(parcel)
        await price_cache.set(parcel.zone, price)
        return price, True
    except CarrierError:
        saved = await price_cache.get(parcel.zone)
        if saved is None:
            raise
        return saved, False

How to check

The adapter's settings name the path for each call. Block the provider's host in staging for 10 minutes. Pages that do not need it load as fast as before, and queued work is sent within 10 minutes of unblocking.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/using-other-companies-apis/provider-outages">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.