# Waterfall enrichment function

> Template · Section 4.4 from *The GTM Engineering Handbook* by Jared Waxman (GTM Engineer School).  
> Web version: https://gtm-engineer-school.com/resources/gtme-handbook/templates/waterfall-enrichment/  
> Last updated: 2026-11-28  
> © 2026 Jared Waxman. All rights reserved. Short quotations with attribution and a link are welcome.

## Waterfall enrichment function

*From Section 4.4.*

The logic of a waterfall, independent of any vendor: try providers in order, stop when a validator accepts a result, and cap spend per record.

`providers`, `validator`, and `now()` are interfaces you supply. This is the control flow, not a client for any particular enrichment API.

```python
def waterfall(record, providers, validator, max_cost):
    spent = 0.0
    attempts = []
    for p in providers:                       # ordered list
        if spent + p.cost_per_call > max_cost:
            break
        if not p.supports(record):            # e.g., region or field not covered
            continue
        result = p.lookup(record)
        spent += p.cost_if_charged(result)    # some providers charge only on hit
        attempts.append((p.name, result))
        if result and validator(result):
            return {
                "value": result.value,
                "source": p.name,
                "confidence": result.confidence,
                "observed_at": now(),
                "cost": spent,
                "attempts": attempts,
            }
    return {"value": None, "cost": spent, "attempts": attempts}
```

---

All concepts: [concepts.md](https://gtm-engineer-school.com/resources/gtme-handbook/concepts.md) · Practitioner questions: [questions.md](https://gtm-engineer-school.com/resources/gtme-handbook/questions.md) · All chapters: [llms.txt](https://gtm-engineer-school.com/resources/gtme-handbook/llms.txt)
