The Vercel AI SDK · Chapter 16 of 21

Guardrails

By the end of this chapter you can put rules around Mali that hold no matter what the model writes: a phone number a customer types never reaches the model, and a discount code the model makes up never reaches the customer, in a whole answer and in a stream that still streams. You will build both guards as middleware and measure what they change on Deepa's test messages. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for customers
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.

Why you need this

Diwali week is busy, and Deepa has two worries from her team's chat logs.

The first is a code. A customer asked "Is there a monsoon sale coupon?", and Mali answered:

Yes! Use the code MONSOON50 at checkout for 50% off everything this monsoon.

There is no MONSOON50. Mali's instructions list the only two codes, GREEN10 and BALCONY15, and end with "There are no other codes." The model invented one anyway. Now a customer expects half off, and Deepa's team has to say no to someone who was promised a discount by HariBhari's own website.

The second is a phone number. Customers type them to ask for a call back:

Thanks! Our team will call you on 98861 23456 about this within a working day.

That number went to the model provider in the request, and came back in the answer. Ananya has promised customers that their phone numbers stay with HariBhari. The model does not need the number to say "we'll call you": the app does, and the app already has it.

Instructions cannot fix either. Instructions ask; they do not make sure. A real model follows them most of the time, and "most of the time" across six hundred messages a day is a few promised discounts a week. Farhan says the answer is a guardrail: code that checks what goes in and what comes out, every time, outside the model. Chapter 15's middleware is the place for it.

What you build: 4 tasks

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

  1. Task 1

    redact.ts: phone numbers never reach the model

    Export redact(text), which replaces every Indian mobile number (10 digits starting 6 to 9, whole or split 5 and 5 by a space or a dash, with or without +91 in front) by exactly [phone number], and leaves pincodes, order ids and prices alone.

    Export redactPhones, a middleware whose transformParams returns params with the text parts of every user message redacted.

    The Check sends a conversation with a number in an earlier message and one in the last, and wants no phone number in the request the model received, the pincode still there, and Mali's instructions unchanged.

  2. Task 2

    coupons.ts: no made-up codes in a whole answer

    Export SAFE_LINE (exactly as the starter gives it), guard(text) and couponGuard. guard returns SAFE_LINE when the text holds a code (3 or more capital letters followed by 1 to 3 digits) that is not one of COUPONS, and the text unchanged otherwise. couponGuard's wrapGenerate returns doGenerate()'s result with every text part guarded.

    The Check asks for a Diwali discount code (the lab model makes one up) and wants SAFE_LINE, and asks about coupons and wants the answer with GREEN10 and BALCONY15 untouched.

  3. Task 3

    stream-guard.ts: guard a stream, keep it streaming

    Export streamGuard, a middleware whose wrapStream holds text back until a sentence ends ('. '), then sends everything up to the last '. ' on as one guarded text-delta, and when the text-end part arrives sends what is still held, guarded, before it. Other parts pass through.

    The Check streams a made-up code (no piece may show it; the customer sees SAFE_LINE), a short answer (all of it arrives), and, at the real pace, a long care guide: the same text as the model wrote, in at least 5 pieces, the first one before half the time has passed.

  4. Task 4 · interview question

    Interview: measure.ts, what do the guards change?

    Ask Deepa's five messages (in the starter, in order) with MALI_INSTRUCTIONS on plain lab('lab-chat'), then on lab('lab-chat') wrapped with redactPhones and couponGuard. For each, count the answers holding a made-up code and the answers holding a phone number, and print:

    plain: N made-up codes, M phone numbers repeated

    guarded: N made-up codes, M phone numbers repeated

    The Check reads the 10 requests and the lab model's answers, and wants your counts, and no phone number in any guarded request.

    Deepa will ask: Mali's instructions already say there are no other codes. Why is that not enough?

What you will learn

  • Input guards with transformParams: change what the model is sent.
  • Output guards with wrapGenerate: check a whole answer before anyone sees it.
  • Output guards on a stream, and why checking each piece is not enough.
  • Holding back as little as possible, so the stream keeps streaming.
  • Measuring a guard: what reached the customer, before and after.

More in Ready for customers

Build "Guardrails" yourself

HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within 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 The Vercel AI SDK are free. This one comes with the all labs plan.