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:

  1. HTTP Status Gate: The gateway response status must equal 200. Every 4xx client error and every 5xx upstream error short-circuits directly to billable: false.
  2. Schema Validation Gate: The normalized data object 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 HTTP 200 containing a login redirect, CAPTCHA challenge, or truncated JSON, DataRoute converts the outcome to 422 validation_failed or 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_found with billable: 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.00 if 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:

HTTP 200-Only Ledger Settlement Flow (Stage 1 Spec) Atomic Settlement
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).