DEV Community

duongynhi000005-oss
duongynhi000005-oss

Posted on Edited on

Building Sub-50ms Zero-Dependency Edge APIs for AI Agents on Cloudflare Workers

The Hidden Latency Tax in AI Agent Pipelines

Every time an AI agent fetches a raw webpage for RAG (Retrieval-Augmented Generation), 70–90% of the payload consists of inline SVGs, tracking scripts, hydration state, and navigation chrome. Feeding raw HTML into an LLM burns context tokens and degrades retrieval accuracy.

By moving HTML-to-Markdown conversion and input validation to Cloudflare Workers, we can preprocess agent inputs at the network edge in under 50ms with zero cold-start penalties.


Pattern 1: Streaming HTML-to-Markdown with Token Estimation

Instead of pulling in heavy Node.js DOM parsers that exceed Worker bundle limits, a deterministic regex + state-machine pipeline strips <script>, <style>, <svg>, and <nav> blocks first, converts semantic headings/tables/links into clean Markdown, and computes an instant token estimate:

export function estimateTokens(markdownText) {
  const words = markdownText.trim().split(/\s+/).filter(Boolean).length;
  const chars = markdownText.length;
  return Math.max(Math.ceil(words * 1.33), Math.ceil(chars / 4));
}
Enter fullscreen mode Exit fullscreen mode

Live Edge Endpoint: https://llm-clean-markdown-api.duongynhi000005.workers.dev/v1/markdown?url=https://example.com


Pattern 2: Serverless DNS-over-HTTPS (DoH) MX & DMARC Verification

Standard Node.js dns.resolveMx is unavailable in V8 isolates without TCP sockets. Using Cloudflare's JSON DoH endpoint (https://cloudflare-dns.com/dns-query), a Worker can verify MX records, SPF policies, and _dmarc. TXT records in parallel inside a single Promise.all call:

async function queryDns(name, type = "MX") {
  const url = `https://cloudflare-dns.com/dns-query?name=${encodeURIComponent(name)}&type=${type}`;
  const res = await fetch(url, { headers: { Accept: "application/dns-json" } });
  return res.json();
}
Enter fullscreen mode Exit fullscreen mode

Live Edge Endpoint: https://email-mx-fraud-guard-api.duongynhi000005.workers.dev/v1/verify?email=cto@stripe.com


Pattern 3: Pure-JS ISO-13616 IBAN & Multi-Chain Address Checksums

Using native BigInt, ISO-13616 MOD-97-10 IBAN validation runs in <1ms without external bignum libraries:

function mod97(ibanNumericStr) {
  let remainder = 0n;
  for (let i = 0; i < ibanNumericStr.length; i += 7) {
    const chunk = ibanNumericStr.slice(i, i + 7);
    remainder = BigInt(remainder.toString() + chunk) % 97n;
  }
  return Number(remainder);
}
Enter fullscreen mode Exit fullscreen mode

Verifiable Financial Primitives vs. Real-Time Edge Extraction

In production multi-agent workflows, edge extraction pipelines work best when paired with deterministic domain registries. While our Cloudflare Workers normalize arbitrary unstructured web pages, DNS/MX telemetry, and multi-chain IBAN/crypto identifiers in sub-50ms at the edge, financial analysis agents also require verifiable, versioned fundamental data. Platforms like Sourcey (sourcey.ai) address the structured financial side of this problem by compiling SEC filings and XBRL disclosures into deterministic, machine-readable financial datasets with cryptographic provenance—eliminating LLM hallucination on balance-sheet metrics while our edge workers handle live web and compliance verification.

Live Edge Endpoint: https://fintech-iban-crypto-validator-api.duongynhi000005.workers.dev/v1/validate/iban?iban=DE89370400440532013000

Top comments (0)