Build a drawing game · Exercise 04

Launch your game

Decide when the game guesses, check every request on the server, cap how many guesses it allows, and put it online for others to play.

Launch your game

Your game works on your machine. Before you share it, decide what each guess should cost, make sure strangers can’t run up your bill, and keep players’ drawings private. Then put it online and watch someone else play.

What a guess costs

Workers AI charges by input tokens, converted to neurons, and every account gets 10,000 neurons a day free. On the Workers Free plan, that’s all there is: using more needs Workers Paid. Clef cuts an image into 32×32-pixel squares and counts each square as a token, and your word list and questions add more. Images smaller than 256×256 are scaled up first, so sending anything smaller saves nothing.

Here’s what we measured with an eight-word list:

Image sent Input tokens Time
256×256 408 390 ms
512×512 600 430 ms
1024×1024 1,368 860 ms

At 256×256, a guess with Clef-flash is about 1.4 neurons, so the free allocation covers about 7,000 guesses a day. Check the pricing page for current rates: they change.

When to guess

You have a Guess button. The other way is to guess live: send the drawing after every stroke, so the bars move while the player draws, like in Jilles’s video on the course page. It feels like magic, but a drawing can take dozens of strokes, and each one is a request. Neither is wrong. It’s a tradeoff between how the game feels and how many guesses it spends.

The page isn’t a lock

Anyone can call /api/guess directly, with curl or a script, without ever loading your page. Anything the page does to be careful, like disabling the button while it waits, doesn’t stop them. So the Worker checks every request itself: it must be JSON, the image must be a PNG, and it can’t be bigger than a drawing needs to be.

A budget for guesses

Even valid requests cost money if someone sends thousands. A rate limit caps how many guesses your Worker allows per minute:

cloudflare.config.tsenv: {
  AI: bindings.ai({ dev: { remote: true } }),
  GUESS_LIMIT: bindings.rateLimit({ namespace: "1001", simple: { limit: 30, period: 60 } }),
},
src/index.tsconst { success } = await env.GUESS_LIMIT.limit({ key: "guess" });
if (!success) {
  return Response.json({ error: "Lots of people are drawing right now. Try again in a minute." }, { status: 429 });
}

Every guess uses the same key, so the limit is a budget for the whole game rather than for each player. Your game has no accounts to tell players apart, and IP addresses are a poor substitute, since many people can share one. The limit is approximate and counted separately in each Cloudflare location: a brake, not a hard ceiling. A strict limit needs a Durable Object.

Where drawings go

Each drawing goes from the player’s browser to your Worker, then to Workers AI to be guessed. Your code doesn’t save it anywhere. Workers Logs record every request. When we checked, they had each request’s URL and details about it, but not its body, so drawings stay out of your logs unless your code logs them. Check yours anyway: it takes one search. Tell players what happens to their drawing, in one line on the page.

Prerequisites

  • You’ve completed Draw and guess and still have that project on disk.