Build an AI gateway (TypeScript) · Chapter 18 of 21

Guardrails at the edge

By the end of this chapter you can keep customers' personal data from ever reaching a provider, refuse a request that is too big before it costs a paisa, hold back an answer that should never reach a customer, and prove all of it with the providers' own numbers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Run it in production
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Build an AI gateway (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Customers write what they write. "You can call me on 81290 85705." "My Aadhaar is 3709 5334 4639 if you need it." "My email is vivek.r@example.com." Jawab's app passes their words on, and Darwaza passes them to whichever provider answers. After v0 replays Tuesday, lab ledger ends with the providers' own count:

personal data the providers were sent: 14 phone, 22 email, 26 aadhaar

Sixty-two pieces of personal data in one day, sent to three companies outside Jawab, none of which needed them to triage a ticket or draft a reply. Darwaza as it is now sends 61 (one such request was refused or answered from the cache). Anjali has promised Jawab's brands that customers' details stay with Jawab. Lakshmi has a second worry: a feature with a bug once sent a whole order history as one message, and it was billed like a novel. And Joseph has seen the worst kind of answer: right in every word, for the wrong brand.

Darwaza checks nothing on the way in or on the way out. It is the one place every request passes, which makes it the right place to check.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    pii.ts: find personal data

    Write and export PII: { phone, email, aadhaar }, a regular expression (with the g flag) for each: an Indian mobile number (10 digits starting 6 to 9, maybe +91 first, maybe a space or a dash after the fifth digit), an email address, and an Aadhaar number (12 digits in groups of four, the first 2 to 9, joined by spaces or dashes).

    Write and export maskPii(text): { text, found }: every match replaced by [phone], [email] or [aadhaar], and how many were replaced. Order ids, amounts, dates, pincodes and OTPs stay as they are.

  2. Task 2

    guard.ts: mask, and stop what is too big

    Write and export guardRequest(req): when all the messages together are over 24,000 characters, { ok: false, status: 413, error }; otherwise { ok: true, req, masked }: a new request with maskPii applied to every message except the system prompt (the request you were given stays unchanged), and masked, how many items were replaced.

    Write and export handle(req): guardRequest first; a refusal goes back as { ok: false, status: 413, error, kind: 'too_large' }; otherwise gateway.handle(the guarded request) from ./darwaza.ts.

  3. Task 3

    answer-check.ts: look at the answer

    Write and export checkAnswer(team, text): { ok: false, reason } when the text names another of Jawab's brands (a TEAMS name other than the team's own, in any case), otherwise { ok: true }.

    Write and export handle(req): handle from ./guard.ts first; when it answered and checkAnswer fails, return { ok: false, status: 502, error, kind: 'guard' } instead; otherwise its response as it is.

  4. Task 4 · interview question

    Interview: pii-report.ts, what did the providers see?

    Write and export piiSent(gateway): replay day({ tags: ['pii'] }) through the gateway with replay(gateway, { entries, msPerMinute: 5 }), and return { phone, email, aadhaar }: the ledger's pii counts after the replay minus before (ledger() from ../../shared/jawab.ts).

    Under if (import.meta.main), print v0: N sent (P phones, E emails, A Aadhaar numbers) with piiSent(v0) (v0 from ./darwaza.ts), then the same line for Darwaza with guardrails, with piiSent(handle) from ./answer-check.ts.

What you will learn

  • Guardrails: rules at the gateway's edge, on the way in and out.
  • Finding personal data with patterns, and what a pattern must not match.
  • Masking: replacing it before any provider sees it, and why the system prompt is left alone.
  • A size limit that refuses before anything is spent.
  • A check on the answer before it goes back.
  • Measuring what reached the providers, from their own ledger.

More in Run it in production

Build "Guardrails at the edge" yourself

Jawab, a Kochi support desk, answers customers for six Kerala brands on three AI providers that fail, slow down and cost different amounts. You build Darwaza, its gateway, and replay a whole day of traffic against an SLO and a budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Build an AI gateway (TypeScript) are free. This one comes with the all labs plan.