1. The Two-Condition Metering Gate
Before a DataRoute gateway request deducts prepaid USD credits, the finalized response passes two mandatory checks inside the metering middleware:
- HTTP Status Gate: The gateway response status must equal
200. Every4xxclient error and every5xxupstream error short-circuits directly tobillable: false. - Schema Validation Gate: The normalized
dataobject must pass strict route-level schema validation (required IDs, types, non-corrupted text/Markdown fields, and absence of upstream block-page markers). If an upstream returns HTTP200containing a login redirect, CAPTCHA challenge, or truncated JSON, DataRoute converts the outcome to422 validation_failedor retries failover—never charging the caller.
2. Valid Empty Sets vs. Malformed Payloads
Public data systems must distinguish carefully between a genuine empty query result and a broken upstream scrape:
- Genuine confirmed 404 or private target: When an upstream explicitly confirms a tweet ID or user handle does not exist or is restricted, DataRoute emits HTTP
404 resource_not_foundwithbillable: false. - Broken or empty scrape shell: When an upstream omits expected structural markers or returns a stripped anti-bot page, the schema gate rejects the payload, triggers secondary failover, and deducts
$0.00if all upstreams fail. - Validated zero-match search: When a search upstream returns a verified structured search envelope confirming zero public matches for an obscure query string, schema validation passes and explicit metadata confirms zero matches.
3. Atomic USD Ledger Reservation & Commit
The planned Stage 1 PostgreSQL USD credit ledger uses an idempotent hold-and-settle flow keyed on request_id:
1. Authenticate DATAROUTE_API_KEY + check available USD credit balance
2. Assign unique request_id and reserve route ceiling in ledger
3. Dispatch to primary engine / dual-upstream failover router
4. Validate response HTTP status === 200 AND route JSON schema
├─ PASS -> Commit exact USD charge, return HTTP 200 with billing.billable = true
└─ FAIL -> Release reservation ($0.00 charge), return normalized 4xx/5xx with billing.billable = false
4. Deterministic Non-Billable Error Codes
Every non-200 response guarantees billing.billable: false and includes an explicit error.retryable boolean so SDKs, n8n workflows, and AI agents apply exponential backoff only on transient upstream conditions (429, 502, 504) while halting immediately on parameter or credit errors (400, 401, 402).