advanced20 min read· Module 8, Lesson 4
🔍Project: AI Code Reviewer
Build a tool that reviews code for bugs and improvements
Project: AI Code Reviewer
Build a tool that reads source files and provides code reviews.
What We're Building
A CLI tool that:
- Reads a source file
- Analyzes it for bugs, security issues, and improvements
- Outputs a structured review report
The Code
Create reviewer.js:
import Anthropic from "@anthropic-ai/sdk";
import fs from "fs";
import path from "path";
const client = new Anthropic();
async function reviewCode(filePath) {
const absolutePath = path.resolve(filePath);
if (!fs.existsSync(absolutePath)) {
console.error("File not found:", absolutePath);
process.exit(1);
}
const code = fs.readFileSync(absolutePath, "utf-8");
const ext = path.extname(filePath).slice(1);
console.log(`Reviewing ${filePath}...\n`);
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 4000,
messages: [{
role: "user",
content: `Review this ${ext} code for:
1. **Bugs** - Logic errors, off-by-one errors, null/undefined issues
2. **Security** - SQL injection, XSS, input validation
3. **Performance** - Unnecessary loops, missing caching, N+1 queries
4. **Best Practices** - Naming, structure, patterns
For each issue found:
- Severity: 🔴 Critical / 🟡 Warning / 🔵 Info
- Line number (approximate)
- Description
- Suggested fix with code example
If no issues found in a category, say "No issues found."
End with an overall score out of 10 and a summary.
\`\`\`${ext}
${code}
\`\`\``
}]
});
console.log(response.content[0].text);
console.log(`\nTokens used: ${response.usage.input_tokens} input, ${response.usage.output_tokens} output`);
}
// Get file path from command line
const filePath = process.argv[2];
if (!filePath) {
console.log("Usage: node reviewer.js <file-path>");
process.exit(1);
}
reviewCode(filePath);Usage
node reviewer.js src/auth/login.ts
node reviewer.js server.py
node reviewer.js index.htmlSample Output
Reviewing src/auth/login.ts...
## Bug Analysis
🔴 **Critical** (Line ~12)
SQL Injection vulnerability in user query
// Bad const user = db.query("SELECT * FROM users WHERE id = " + userId);
// Fix const user = db.query("SELECT * FROM users WHERE id = $1", [userId]);
🟡 **Warning** (Line ~25)
Missing null check on user object before accessing properties
## Overall Score: 6/10
Summary: The code has a critical SQL injection vulnerability that must
be fixed before deployment. Other issues are minor but should be
addressed in the next refactor cycle.
Extensions to Try
- Multiple files — review a whole directory
- JSON output — use structured outputs for machine-readable reports
- Git integration — automatically review changed files
- CI integration — run as part of your build pipeline
Congratulations! You've completed the course roadmap. Keep building, keep learning!