Skip to main content
This tutorial walks through the code-review-bot example from the SDK repository. By the end, you’ll understand how to combine custom tools, system prompts, completion lifecycle, and event streaming into a real application.

What It Builds

A code review agent that:
  1. Reads a git diff from the local repo
  2. Optionally reads full file contents for context
  3. Produces structured review comments with severity levels
  4. Ends the run with a summary and approve/reject decision

Prerequisites

  • Node.js 22+
  • An Anthropic API key
  • A git repository with at least one commit

Get the Code

Or read along with the source on GitHub.

How It Works

Defining Tools with Zod Schemas

The bot uses createTool with zod schemas for type-safe tool definitions. Here’s the review comment tool:
Key points:
  • z.enum constrains severity to valid values, which improves model accuracy
  • .describe() on each field tells the model what to provide
  • The tool accumulates results in an array for post-run processing

Completion Tools

The submit_review tool uses lifecycle: { completesRun: true } to signal that the agent’s work is done:
Without this, the agent would keep looping until maxIterations. With it, the agent calls submit_review when it’s done and the run ends cleanly.

System Prompt

The system prompt gives the agent a structured workflow to follow:
Telling the agent exactly which tools to use and when keeps the workflow predictable.

Event Streaming

The bot subscribes to events to show progress as the agent works:
This prints review comments as they’re made, so you see results streaming rather than waiting for the full run to finish.

Post-Run Processing

After the run, the bot groups comments by severity and prints a summary:
The tool calls accumulate structured data during the run, and the application processes it after. This pattern is useful any time you want the agent to produce structured output.

Run It

Extending Further

From here, you could:
  • Add a tool that posts review comments back to GitHub via the API
  • Use continue() for follow-up questions about specific findings
  • Add a checkstyle tool that runs linters on the changed files
  • Connect it to a webhook for automatic PR reviews

More Examples

CLI Agent

Interactive terminal chat with tools and multi-turn conversation.

Multi-Agent

Parallel agents streaming to a web UI.
See Creating Custom Tools and Writing Plugins for more on extending agent capabilities.