Build & Deploy AI Agents

Lesson 2

Module 2: Tools & AI

Give REVIEWBOT eyes and opinions with typed tools and Workers AI.

Module 2 turns REVIEWBOT from a persona into something that can actually look at code and form an opinion.

Anatomy of a tool

A tool the model can call has three parts:

  • a description so the model knows when to reach for it,
  • an inputSchema (Zod) so inputs are validated before your code runs,
  • an execute function that does the work.

One gotcha: a tool only becomes callable once you add it to the tools map inside onChatMessage. Easy to forget.

fetchPullRequest, with a noise filter

The fetchPullRequest tool pulls a PR’s files and diff from the GitHub API. Before handing it to the model, it filters noise such as lockfiles and generated output, so the model spends its limited attention on real changes. The unauthenticated GitHub limit is 60 requests per hour per IP; set a GITHUB_TOKEN secret to raise it to 5000.

reviewDiff on Workers AI

reviewDiff sends the filtered diff to a model running on Workers AI and parses structured findings back out. The model is @cf/google/gemma-4-26b-a4b-it (Gemma 4), which runs on free Neurons.

AI Gateway

Set AI_GATEWAY_ID and your model calls route through AI Gateway, which adds caching, rate limiting, and a log of every call, with no change to your calling code.

When you are ready, open the Tools & AI exercise to build it.