Vercel Agent Tutorial: AI Code Review in 47 Minutes
Build AI code review agents with Vercel SDK. Learn sandbox validation, streaming UI & production patterns. $0.30/review, 60% faster reviews, 100K free requests.
Per Review (GPT-4o)
Setup Time
Time Savings
Free Requests/Month
Key Takeaways
Why Vercel AI Agent SDK Matters
The Vercel AI Agent SDK represents a paradigm shift in how developers build autonomous AI workflows. Unlike traditional chatbots that simply respond to prompts, agents can break down complex tasks into steps, validate their work, and iterate until completion.
What Makes Vercel AI Agent SDK Unique
Built-in Streaming
Stream agent thoughts, actions, and results in real-time without complex WebSocket setup
Native Next.js Integration
Designed specifically for Next.js with App Router support and Server Components
Tool Calling Abstraction
Simple function definitions that automatically handle OpenAI/Anthropic tool calling protocols
ToolLoopAgent Class
New unified interface that handles tool execution loops automatically, reducing boilerplate by 40% and managing context, stopping conditions, and message arrays for you
Tool Execution Approval
Native human-in-the-loop patterns with needsApproval flag, perfect for sensitive operations like code deployment or data deletion
Enhanced Streaming
Improved backpressure support, better error handling with onError callbacks, and custom stream transformations with experimental_transform
Code review is the perfect application for AI agents because it requires multiple sequential steps: analyzing code structure, checking for bugs, validating against style guides, running tests, and generating feedback. Manual code review takes 30-45 minutes per PR, while AI agents complete reviews in 5-10 minutes with comparable quality. Integrating AI-powered automation into your development workflow can dramatically improve team productivity.
Quick Start: 47-Minute Setup
This tutorial gets you from zero to a deployed AI code review agent in 47 minutes. We'll build a complete system with sandbox validation, streaming UI, and GitHub integration. If you need help implementing production-ready web applications, our team specializes in Next.js and AI integrations.
ToolLoopAgent class for simplified agent orchestration, improved streaming with backpressure support, and enhanced error handling. The v6 SDK reduces boilerplate code by up to 40% compared to previous versions while providing better TypeScript types and debugging tools. Learn more at v6.ai-sdk.dev.Step 1: Install Dependencies (3 minutes)
# Install Vercel AI SDK v6 Beta with OpenAI
npm install ai@beta @ai-sdk/openai@beta @ai-sdk/react@beta zod
# Install E2B sandbox for code execution
npm install @e2b/sdk
# Note: Pin to specific versions in production as v6 is in beta
# and APIs may change in patch releasesai@6.0.0-beta.1) to avoid unexpected breaking changes in patch releases.Step 2: Create the Agent API Route (15 minutes)
Create app/api/review/route.ts:
import { openai } from '@ai-sdk/openai';
import { streamText, tool, stepCountIs, UIMessage, convertToModelMessages } from 'ai';
import { z } from 'zod';
import { Sandbox } from '@e2b/sdk';
export const runtime = 'edge';
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: openai('gpt-5'), // or 'gpt-4o' for compatibility
system: 'Expert code reviewer analyzing bugs, security, and best practices',
messages: convertToModelMessages(messages),
stopWhen: stepCountIs(5), // v6: Multi-step tool execution
tools: {
validateCode: tool({
description: 'Execute code in sandbox',
inputSchema: z.object({ code: z.string() }), // v6: inputSchema
execute: async ({ code }) => {
const sandbox = await Sandbox.create();
try {
const result = await sandbox.runCode(code);
return { success: !result.error, output: result.stdout };
} finally {
await sandbox.close();
}
},
}),
},
});
return result.toUIMessageStreamResponse(); // v6: UI message response
}Step 3: Build the Frontend (20 minutes)
Create app/review/page.tsx:
'use client';
import { useChat } from '@ai-sdk/react'; // v6: new import path
import { useState } from 'react';
export default function CodeReviewPage() {
const [code, setCode] = useState('');
const { messages, sendMessage, isLoading } = useChat({ // v6: sendMessage
api: '/api/review',
});
return (
<div className="container mx-auto p-6">
<h1 className="text-3xl font-bold mb-6">AI Code Review</h1>
<textarea
value={code}
onChange={(e) => setCode(e.target.value)}
className="w-full h-64 p-4 border rounded-lg font-mono"
placeholder="Paste your code here..."
/>
<button
onClick={() => sendMessage({ text: code })} // v6: sendMessage API
disabled={isLoading}
className="mt-4 px-6 py-2 bg-blue-600 text-white rounded-lg"
>
{isLoading ? 'Reviewing...' : 'Review Code'}
</button>
<div className="mt-8 space-y-4">
{messages.map((msg) => (
<div key={msg.id} className="p-4 rounded-lg bg-gray-50">
{/* v6: messages have parts array */}
{msg.parts.map((part, i) => {
if (part.type === 'text') {
return <div key={i} className="prose">{part.text}</div>;
}
if (part.type === 'tool-validateCode') {
return <pre key={i}>{JSON.stringify(part, null, 2)}</pre>;
}
})}
</div>
))}
</div>
</div>
);
}parameters → inputSchema, maxSteps → stopWhen: stepCountIs(5), and toDataStreamResponse() → toUIMessageStreamResponse()@ai-sdk/react, use sendMessage({text: '...'}) instead of append(), and access message content via msg.parts[] arrayStep 4: Deploy to Vercel (7 minutes)
# Deploy to Vercel
npx vercel --prod
# Add environment variables in dashboard:
# OPENAI_API_KEY, E2B_API_KEYE2B Sandbox Validation
Sandbox validation is critical for production AI agents. Without it, you're trusting AI-generated code to run in your environment—a major security risk. E2B provides isolated execution environments where code runs safely.
Advanced Sandbox Configuration
import { Sandbox } from '@e2b/sdk';
// Create sandbox with custom configuration
const sandbox = await Sandbox.create({
template: 'nodejs',
timeout: 30000, // 30 seconds max
envVars: { NODE_ENV: 'test' },
});
// Execute with safety checks
const result = await sandbox.runCode(code, 'javascript', {
networkAccess: false, // Restrict network
cpuLimit: '1000m', // Limit CPU
memoryLimit: '512Mi', // Limit memory
});
// Always cleanup
await sandbox.close();Streaming UI & Real-Time Updates
Streaming UI transforms the user experience from "waiting and hoping" to "watching progress in real-time." Developers see exactly what the AI is analyzing, reducing perceived wait time by 60%. This is particularly crucial for code review where analysis can take 15-30 seconds—without streaming, users often abandon the process thinking it has frozen.
- Built-in streaming with zero configuration required
- Automatic retry and error handling for network issues
- 60% faster perceived response time in user studies
- Real-time tool call visibility for debugging agents
Implementing Streaming
'use client';
import { useChat } from '@ai-sdk/react'; // v6: new import path
export default function StreamingReview() {
const { messages, isLoading } = useChat({
api: '/api/review',
// v6: Tool calls are accessed via message.parts array
});
return (
<div className="h-screen flex flex-col">
<div className="flex-1 overflow-y-auto p-6 space-y-4">
{messages.map((msg) => (
<div key={msg.id}>
{msg.parts.map((part, i) => {
if (part.type === 'text') {
return <div key={i}>{part.text}</div>;
}
// Handle tool parts like tool-validateCode
if (part.type.startsWith('tool-')) {
return <div key={i} className="text-sm text-gray-500">
Tool: {part.type.replace('tool-', '')}
</div>;
}
})}
</div>
))}
</div>
{isLoading && (
<div className="p-4 bg-blue-50 border-t">
<div className="flex items-center gap-2">
<div className="animate-spin size-4 border-2
border-blue-600 border-t-transparent
rounded-full" />
<span className="text-sm">Analyzing code...</span>
</div>
</div>
)}
</div>
);
}Production-Ready Patterns
Moving from prototype to production requires implementing error handling, retry logic, rate limiting, and monitoring. Production AI agents must handle network failures gracefully, prevent abuse through rate limiting, and provide visibility into costs and performance. Our automation services can help you build robust, production-grade AI systems that scale with your business needs.
Error Handling & Retries
export async function POST(req: Request) {
try {
const { messages }: { messages: UIMessage[] } = await req.json();
// Validate input
if (!messages || messages.length === 0) {
return new Response('Invalid messages', { status: 400 });
}
const result = streamText({
model: openai('gpt-4o'),
messages: convertToModelMessages(messages),
tools: { ... },
stopWhen: stepCountIs(10), // v6: stopWhen instead of maxSteps
maxRetries: 3, // Retry on failures
onError: ({ error }) => {
console.error('Agent error:', error);
if (error.message.includes('rate_limit')) {
return 'Rate limit exceeded. Try again soon.';
}
return 'Error during review. Please try again.';
},
});
return result.toUIMessageStreamResponse(); // v6: UI message response
} catch (error) {
return new Response('Server error', { status: 500 });
}
}Rate Limiting
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';
// 10 requests per minute
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, '1 m'),
});
export async function POST(req: Request) {
const ip = req.headers.get('x-forwarded-for') || 'anonymous';
const { success } = await ratelimit.limit(ip);
if (!success) {
return new Response('Rate limit exceeded', { status: 429 });
}
// Continue with review...
}Cost Optimization
GPT-5
$0.15/review • Latest model
gpt-4o-mini
$0.05/review • Budget option
GitHub Integration
Integrate your AI code review agent with GitHub to automatically review pull requests, post comments, and update status checks.
GitHub Webhook
import { Octokit } from '@octokit/rest';
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
export async function POST(req: Request) {
const payload = await req.json();
if (payload.action === 'opened') {
const pr = payload.pull_request;
// Get PR files
const { data: files } = await octokit.pulls.listFiles({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pr.number,
});
// Review each file
for (const file of files) {
const review = await reviewCode(file.patch);
// Post review comment
await octokit.pulls.createReviewComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pr.number,
body: review.summary,
path: file.filename,
line: review.line,
});
}
}
return new Response('OK');
}GitHub Actions
Create .github/workflows/ai-review.yml:
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run AI Review
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
curl -X POST https://your-app.vercel.app/api/review \
-H "Content-Type: application/json" \
-d '{"pr_number": ${{ github.event.pull_request.number }}}'Best Practices & Pitfalls
Learn from common mistakes and implement battle-tested patterns for reliable AI agents.
maxSteps and request timeouts to prevent infinite loopsProduction Checklist
Implement error handling with retries
Use maxRetries and custom error handlers
Add rate limiting per user/IP
Prevent abuse with Upstash Redis
Set maxSteps and timeouts
Prevent infinite loops and runaway costs
Use sandbox validation for code execution
Never run untrusted code in production
Monitor token usage and costs
Set up alerts when usage exceeds thresholds
Ready to Ship Your AI Agent?
You now have everything needed to build production-ready AI code review agents with Vercel AI SDK v6. From the new ToolLoopAgent class to sandbox validation and GitHub integration, you've learned the patterns used by top engineering teams. Our implementations have processed over 50,000 pull requests with 95%+ accuracy, helping engineering teams ship 30% faster while maintaining rigorous quality standards.
Frequently Asked Questions
Related Articles
Continue exploring AI development and coding tools with these related guides