AI Tools Example
Using v0-sdk with AI SDK for programmatic interaction
Learn how to build AI agents that can autonomously interact with the v0 platform using @v0-sdk/ai-tools and the AI SDK for complex workflows and automation.
Setup
-
Install dependencies:
pnpm install -
Set up environment variables: Create a
.envfile in this directory:V0_API_KEY=your_v0_api_key_here AI_GATEWAY_API_KEY=your_ai_gateway_api_key_here -
Get your API keys:
- v0 API Key: Get from v0.dev account settings
- AI Gateway API Key: Get from vercel.com AI Gateway settings
Examples
Simple Example (Recommended Start)
pnpm devShows the basic structure and available tools without AI SDK complexity.
Full AI Integration Examples
# Complete workflow examples
pnpm dev:full
# Chat-focused example
pnpm dev:chat
# Project management example
pnpm dev:project
# Advanced agent patterns example
pnpm dev:agentNote: The full AI integration examples require compatible versions of AI SDK and may have type compatibility issues. Start with the simple example first.
Key Concepts
1. All Tools (High Context)
import { v0Tools } from '@v0-sdk/ai-tools'
const result = await generateText({
model: 'openai/gpt-5-mini',
prompt: 'Create a new React component',
tools: v0Tools({ apiKey: process.env.V0_API_KEY }),
})⚠️ Note: This includes all ~20+ tools which adds significant context to your AI calls.
2. Selective Tools (Recommended)
import { v0ToolsByCategory } from '@v0-sdk/ai-tools'
const tools = v0ToolsByCategory({ apiKey: process.env.V0_API_KEY })
const result = await generateText({
model: 'openai/gpt-5-mini',
prompt: 'Create a new project and chat',
tools: {
...tools.project, // Only project tools
...tools.chat, // Only chat tools
},
})3. Available Tool Categories
tools.chat- Create, manage, and interact with v0 chatstools.project- Create and manage v0 projectstools.deployment- Handle deployments and logstools.user- Get user information and billing detailstools.hook- Manage webhooks for events
Agent Patterns
The dev:agent example demonstrates advanced AI agent patterns:
1. Multi-Step Agent with stopWhen
- Autonomous agents that can plan and execute complex workflows
- Uses
stepCountIs()to control execution length - Structured answers with answer tools
2. Sequential Processing (Chains)
- Step-by-step workflows where each step builds on the previous
- Quality checks and iterative improvement
- Requirements analysis → Project creation → Validation
3. Routing Agent
- Intelligent request classification and routing
- Dynamic model selection based on complexity
- Context-aware tool selection
4. Parallel Processing
- Independent tasks executed simultaneously
- Efficient resource utilization
- Result synthesis from multiple analyses
5. Evaluator-Optimizer
- Feedback loops for continuous improvement
- Quality assessment and iterative refinement
- Self-improving workflows
Tips
- Start with selective tools - Only include the categories you need to reduce context size
- Use
stopWhen- Control agent execution with conditions likestepCountIs(n) - Handle errors - Wrap AI calls in try-catch blocks
- Monitor usage - Check your API usage on both v0 and AI Gateway platforms
Troubleshooting
- "Invalid API key": Check your
.envfile and API key validity - "Tool not found": Ensure you're using the correct tool category
- Rate limits: Both v0 and AI Gateway have rate limits - add delays if needed
Source Code
View the complete source code on GitHub: v0-sdk/examples/ai-tools-example