Build a guestbook · Exercise 03
Moderate messages
Screen new entries with an AI decision model and hold back the ones that look like spam.
Paste this into your agent
Copied! Now paste it into your agent. No agent yet? Here's how to get one.
Moderate messages
A guestbook on the open internet gets spam, sooner or later. You could write rules for it, but spam changes faster than rules do. In this exercise you’ll have a model screen every entry, and hold back the ones that look bad.
What’s a decision model?
Most AI models you’ve used generate text: you ask, they write. A decision model doesn’t write anything. You give it some input, called the state, and a few typed questions, and it returns a probability for every allowed answer. A question can be:
noul: yes or no. It returns the probability of yes.choice: one option from a set you define, like which team should handle a support ticket.score: a rating against a rubric you write.
There’s no reply to parse and nothing to wait for while it reasons, so it’s fast enough to sit in the middle of a request, and the answer is already in the shape your code needs.
What’s Clef?
Clef is a family of decision models trained by Cloudflare, running on Workers AI, with open weights. You’ll use Clef-flash, the fast one, which costs $0.038 per million input tokens. A guestbook entry is under 200 tokens, so the free daily allocation covers thousands of entries a day.
If you’ve heard of decision models before, it was probably Jev, from TypeSafe. Clef follows the same API, and Jev is in Cloudflare’s model catalog too, so you can call it the same way through AI Gateway, as typesafe/jev. It’s billed with AI Gateway credits rather than the Workers AI free allocation, so this exercise uses Clef.
Spam detection in a few lines
Here’s the whole idea, in a Worker that checks one entry:
import { env } from "cloudflare:workers";
export default {
async fetch(request: Request) {
const { name, message } = await request.json<{ name: string; message: string }>();
const { answers } = await env.AI.run(
"@cf/cloudflare/clef-flash",
{
model: "clef-flash",
state: { name, message },
questions: {
spam: {
type: "noul",
instructions: "Is this guestbook entry spam, an ad, or abusive?",
},
},
},
{ gateway: { id: "default" } },
);
const spam = (answers as { spam: { noul: number } }).spam.noul; // from 0 to 1
return Response.json({ spam, held: spam > 0.8 });
},
};
A friendly “Lovely site, thanks for sharing!” came back at about 0.02 when we tried it, and “CHEAP WATCHES visit www.best-replicas.biz now 80% off” at about 0.95. The model gives you a probability; where you draw the line is up to you.
Why AI Gateway?
The last argument sends the request through AI Gateway, using your account’s default gateway. Your code works the same either way, but now every decision is logged: you can see what Clef said about each entry, how long it took, and what it cost, without writing any logging yourself.
What you’ll build
Your guestbook, screening every new entry with Clef. Entries that look like spam are held back instead of deleted, so you can let through anything it got wrong.
Prerequisites
- You’ve completed Connect a database and still have that project on disk.