Handling Third-Party API Downtime in Your Test Suite and Agents
Every API suite that depends on someone else's service eventually has the same bad morning: the payment sandbox, the email provider or the geocoding API goes down, and half the build turns red. Nothing in your code changed. The tests are telling the truth about the world, but they're telling it in a way that blocks every pull request and teaches people to ignore red builds.
There's a newer cost too. When a coding agent like GitHub Copilot in agent mode or Claude Code runs your suite and sees a wall of failures, it tries to fix them. A 503 from a provider looks like any other failure, and an agent under instruction to "make the tests pass" may add retries, loosen assertions or mock the call away inside the test. If your suite can't say "their service is down" clearly, neither a tired human nor an agent can tell an outage from a regression.
In this article I'll walk through how I structure a suite so a third-party outage stays a known, managed event: one seam for the external call, explicit tags, informative skips, simulated failure modes, and the same treatment for the model provider behind any LLM feature you test.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Put every external call behind one seam
If a third-party call is buried inside business logic, a 503 fails the test and tells you nothing. The fix is a thin client that owns the external call and nothing else:
# services/payment_client.py
import requests
class PaymentClient:
def __init__(self, base_url, api_key, session=None):
self.base_url = base_url
self.api_key = api_key
self._session = session or requests.Session()
def charge(self, amount_cents, token):
response = self._session.post(
f"{self.base_url}/charges",
json={"amount": amount_cents, "token": token},
headers={"Authorization": f"Bearer {self.api_key}"},
timeout=5,
)
response.raise_for_status()
return response.json()
Business logic depends on PaymentClient, not on requests. Unit tests inject a fake, and only the tests that deliberately exercise the client can be hurt by an outage. This is the same idea behind layering a test suite into unit, API and end-to-end: an outage should degrade one layer, not the whole pyramid.
Tag the tests that touch the real world
Make the boundary visible with a pytest marker. Register it so typos fail loudly:
# pytest.ini
[pytest]
markers =
live: calls a real external service
addopts = --strict-markers
import pytest
@pytest.mark.live
def test_real_charge_returns_transaction_id(payment_client):
result = payment_client.charge(100, "tok_test")
assert "transaction_id" in result
Now CI can run pytest -m "not live" on every push and keep pytest -m live for a scheduled job. A provider outage breaks the scheduled run, which is expected and visible, and never blocks someone's pull request. I cover the wider tagging strategy in tagging API tests so CI runs the right subset.
Skip with a reason instead of failing with noise
A raw ConnectionError in a CI log is close to useless. For live tests I add a fixture that checks the provider first and skips with a message a person (or an agent) can act on:
# conftest.py
import os
import pytest
import requests
PAYMENT_HEALTH_URL = os.environ.get("PAYMENT_HEALTH_URL", "")
@pytest.fixture
def require_payment_service():
try:
r = requests.get(PAYMENT_HEALTH_URL, timeout=3)
except requests.RequestException as exc:
pytest.skip(f"payment provider unreachable: {exc.__class__.__name__}")
if r.status_code >= 500:
pytest.skip(f"payment provider unhealthy: HTTP {r.status_code}")
A downed provider now shows as SKIPPED with a reason, not FAILED. Be careful where you use this, though. On the scheduled live run, a skip can hide a long outage or a provider that has quietly changed its health endpoint. I make the live job report its skip count and treat "all live tests skipped" as a failure of the job itself.
Simulate the outage you actually care about
Skipping protects the pipeline. It doesn't prove your code handles an outage. For that I simulate the failure with the responses library, which intercepts calls made through requests:
import responses
from services.payment_client import PaymentClient
from services.checkout import charge_with_retry
URL = "https://api.payments.example.com/charges"
@responses.activate
def test_charge_retries_once_after_503():
responses.add(responses.POST, URL, status=503, json={"error": "unavailable"})
responses.add(responses.POST, URL, status=200, json={"transaction_id": "txn_1"})
client = PaymentClient("https://api.payments.example.com", "key_test")
result = charge_with_retry(client, 100, "tok_test", attempts=2)
assert result["transaction_id"] == "txn_1"
assert len(responses.calls) == 2
This runs on every push, with no network, and it pins the behaviour: one retry, then success. Add the matching test for the case where every attempt fails and your code must surface a clear error rather than hang. The retry itself needs care, which I go into in retrying an API call without hiding real failures.
Recorded responses help with the happy path. Tools like vcrpy record real HTTP interactions into a cassette file and replay them later. Set the cassette to replay only in CI so a missing recording fails instead of silently calling the live service, and refresh cassettes on a schedule so they don't drift from what the provider really returns.
Your model provider is a third-party API too
If you test anything that calls an LLM, the model provider belongs in this same structure. It has outages, rate limits and slow responses like any other vendor, and its output is non-deterministic on top. I treat it exactly like the payment client:
- One seam. Feature code takes an injected
generate(prompt) -> strfunction. Unit tests pass a fake that returns fixed text, so prompt assembly, parsing and guardrails are tested with no network at all. - A separate marker. Tests that call the real model get their own marker and run on a schedule, measured as a pass rate over several runs rather than a single pass or fail.
- Timeouts and error paths simulated. A fake
generatethat raises a timeout proves the feature degrades cleanly when the provider does.
Tell your agents what an outage looks like
The last piece is making all of this legible to the coding agents that run your suite. A few lines in .github/copilot-instructions.md, or your agent's equivalent file, prevent the worst mistakes:
## External services
- Tests marked live call real providers. Do not run them unless asked.
- A SKIPPED live test with "provider unreachable" is an outage, not a bug. Report it; do not change code.
- Never add retries, sleeps or mocks inside a test to make a failing live test pass.
- New code that calls an external API goes through a client in services/, with a mocked test.
Instructions are guidance, so I back them with review: any agent-written change that touches a live test or adds a retry gets a human look before merge.
Third-party downtime is a fact of life. Isolate the seam, tag the live tests, skip with a reason, simulate the failures you care about, and give people and agents the same clear signal. Then an upstream outage is a line in a report instead of a fire drill. If you'd like to build a suite structured this way from scratch, my API testing courses walk through it with Python or Node and GitHub Copilot.