Fault Injection
Fault injection is the practice of deliberately causing failures, such as timeouts, error responses, malformed replies, slow dependencies, or crashes, in a controlled setting to see whether a system detects and recovers from them correctly. Happy-path tests exercise only the paths where everything works, so they say little about what happens at the moment something fails. Real failures are rare enough that recovery code may run for the first time during an incident. Injection makes those paths run on schedule by causing the failure in a controlled way.
What gets injected
A fault is a defined deviation at a boundary the system depends on. Typical categories:
- Unavailability: the dependency refuses connections or returns a 5xx error before doing anything.
- Latency and timeouts: the answer arrives after the caller has given up.
- Corrupt or malformed output: invalid JSON, a missing field, an unexpected type.
- Partial failure: the operation takes effect but the response is lost. This one matters most for retry logic.
- Process failure: the worker is killed or paused between two steps.
The injection point is usually a wrapper or test double around the dependency, which can be configured to fail on the nth call, on a given input, or at random with a set probability. Systems-level tools inject at the network or infrastructure layer instead.
Why the “effect applied, response lost” fault matters
A failure before the effect and a failure after it look identical to the caller: an exception or timeout. They need different handling. If the call did nothing and the failure is transient, retrying a safe operation can help. If it already happened, retrying without duplicate protection repeats the effect. The small simulation below injects each into a hypothetical refund service and lets an agent retry up to three times:
class Refunds:
def __init__(self, faults): self.faults, self.ledger, self.keys = list(faults), [], set()
def call(self, order, cents, key=None):
fault = self.faults.pop(0) if self.faults else None
if fault == "error_before_effect": raise TimeoutError("failed before effect")
if key is None or key not in self.keys:
self.ledger.append((order, cents))
if key: self.keys.add(key)
if fault == "timeout_after_effect": raise TimeoutError("effect applied, response lost")
return "ok"
def trial(fault, stable_key):
service = Refunds([fault]) # inject the fault only on the first call
key = "refund-O-1" if stable_key else None
for attempt in range(4): # first call plus up to three retries
try:
reply = service.call("O-1", 100, key=key)
return len(service.ledger), reply
except TimeoutError:
if attempt == 3:
raise
for fault in (None, "error_before_effect", "timeout_after_effect"):
without, reply1 = trial(fault, False)
with_key, reply2 = trial(fault, True)
print(f"{fault or 'none'}: without key={without} ({reply1}), stable key={with_key} ({reply2})")
Each trial starts with an empty refund ledger, refunds 100 cents for order O-1, and injects the chosen fault on the first call only. The retry loop reuses refund-O-1 when a stable key is enabled. The same code produces:
none: without key=1 (ok), stable key=1 (ok)
error_before_effect: without key=1 (ok), stable key=1 (ok)
timeout_after_effect: without key=2 (ok), stable key=1 (ok)
Results, with the number of refund entries in the ledger:
| Injected fault | Retry without a stable key | Retry with a stable key |
|---|---|---|
| none | 1 | 1 |
| error before the effect | 1 | 1 |
| timeout after the effect | 2 | 1 |
In every row the agent’s final reply was ok. Only the last row without a key is wrong, and only the ledger shows it. Happy-path tests and reply-based scoring never produce that case. With an idempotency key, the repeat is recognized and adds no second effect.
Use in agent evaluation
An agent run interleaves model decisions with tool calls, and the model sees whatever the tool returns. Injecting faults tests the combination: does the model retry when it should, stop when retries would be wrong, report the failure honestly, escalate rather than invent a result, and avoid re-running steps that already succeeded? The evaluation scores the outcome, preferably from an effect ledger, rather than the final message. Useful design points:
- Treat faults as test cases with expected behavior, written in advance: for “timeout after effect”, the correct run ends with exactly one refund or an escalation, never two.
- Inject through the same dispatcher the agent uses, so that the model sees realistic tool errors.
- Include faults in tool results that carry content, such as a malformed response or a result containing instructions, since the model reads these.
- Keep faults deterministic in regression tests (fail the first call) and add randomized runs separately to find cases you did not think of.
Limits and neighbors
Injected faults only cover the failure modes someone thought to inject; they show that tested recovery paths work, not that all failures are handled. Chaos engineering applies the same idea to production-like systems by injecting failures in running environments with a blast-radius limit. Fuzzing varies inputs to find bugs; fault injection varies the environment’s behavior. Load testing measures capacity under volume, not behavior under specific failures. Whatever the setting, run injection in a test or staging environment first, and bound its effects before running it anywhere real users or money are involved.
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
