ai-elements
Production-quality skill for developing AI-native applications with AI Elements (https://ai-sdk.dev/elements). Use when building chat interfaces, conversational AI applications, or any AI-powered UI that needs components like Message, Conversation, Response, Code Block, Reasoning, Tool calls, Sources, Prompt Input, or other AI-specific UI patterns. Essential for Next.js projects integrating Vercel AI SDK with shadcn/ui components.
AI Elements Development Skill
Comprehensive guide for building production-quality AI-native applications using AI Elements, the official component library built on shadcn/ui for the Vercel AI SDK.
Overview
AI Elements is a component library and custom registry that provides pre-built, customizable React components specifically designed for AI applications. It addresses the unique challenges of AI interfaces: streaming responses, tool calls, reasoning blocks, citations, and conversational patterns that standard UI libraries don't handle well.
Key Characteristics:
- Built on shadcn/ui - components live in your codebase for full customization
- TypeScript-first with proper type safety
- Designed for Vercel AI SDK integration (useChat, streamText, etc.)
- Handles streaming, incomplete markdown, and AI-specific UI patterns
- Production-ready with security, accessibility, and performance built-in
Quick Start Workflow
1. Project Setup
Initialize a new Next.js project with the required dependencies:
Important: AI Elements requires:
- Next.js project with AI SDK installed
- Tailwind CSS configured
- shadcn/ui CSS Variables mode (automatically configured by CLI)
- Node.js 18 or later
2. Install Components
Alternative Method (using shadcn CLI):
3. Setup API Route
Create /app/api/chat/route.ts:
4. Build Frontend Component
Create /app/page.tsx:
5. Configure Globals CSS
Add to /app/globals.css (required for Response component):
This imports Streamdown styles used by the Response component for markdown rendering.
Core Components
Essential Components
- Conversation - Container with auto-scrolling for chat messages
- Message - Individual chat messages with avatars and styling
- Response - Markdown renderer with streaming support (uses Streamdown)
- Prompt Input - Advanced input with file upload and model selection
- Code Block - Syntax-highlighted code with copy button
AI-Specific Components
- Reasoning - Collapsible AI reasoning/thinking display
- Tool - Tool call visualization with input/output
- Sources - Citation and source attribution
- Branch - Response variation navigation
- Loader - Loading states for AI operations
Interactive Components
- Actions - Action buttons for responses (copy, regenerate, etc.)
- Suggestion - Quick action suggestions
- Task - Task completion tracking
- Inline Citation - Inline source citations
- Image - AI-generated image display
Advanced Components
- Web Preview - Embedded web page previews
- Shimmer - Loading shimmer effects
- Queue - Operation queue visualization
For detailed documentation on each component, see references/COMPONENTS.md.
AI SDK Integration Patterns
Pattern 1: Basic Chat with useChat
Key Points:
useChatmanages state, streaming, and API calls automatically- Messages contain ordered
partsarray (text, tool-call, reasoning, etc.) - Status indicates streaming state for UI feedback
Pattern 2: Handling Message Parts
Messages contain parts that represent different types of content:
Pattern 3: Streaming API Route
Backend route using streamText:
Pattern 4: Multiple Model Support
Backend handles model selection:
Advanced Patterns
Reasoning Display (DeepSeek R1, etc.)
For models that output reasoning tokens:
Tool Calls with Status
Web Search with Citations
File Upload Support
Component Customization
All components are installed directly into your codebase at @/components/ai-elements/, allowing full customization:
Example: Customize Message Styling
Edit components/ai-elements/message.tsx:
Example: Custom Response Components
Best Practices
1. Component Organization
app/
├── api/
│ └── chat/
│ └── route.ts # API route
├── components/
│ ├── ai-elements/ # AI Elements components (auto-installed)
│ ├── chat/
│ │ ├── chat-interface.tsx
│ │ ├── chat-message.tsx
│ │ └── chat-input.tsx
│ └── ui/ # Other shadcn/ui components
├── lib/
│ ├── ai/
│ │ ├── providers.ts # Model provider configuration
│ │ └── tools.ts # Tool definitions
│ └── utils.ts
└── page.tsx
2. Type Safety
Always use TypeScript and import proper types:
3. Error Handling
4. Loading States
5. Performance Optimization
- Use React.memo for expensive components
- Implement virtualization for long conversations
- Stream responses instead of waiting for completion
- Use proper key props in lists
6. Accessibility
All AI Elements components are built with accessibility in mind:
- Keyboard navigation support
- Proper ARIA labels
- Focus management
- Screen reader compatibility
Enhance with custom ARIA attributes:
7. AI Gateway Integration
For production applications, use Vercel AI Gateway:
Benefits:
- $5/month free usage
- Single API key for all providers
- Built-in caching and rate limiting
- Usage analytics
Common Patterns
Multi-Turn Conversations with Context
Regenerate Response
Stop Streaming
Branch Conversations
Environment Configuration
Minimal Setup (.env.local)
Provider Configuration (lib/ai/providers.ts)
Troubleshooting
For comprehensive troubleshooting information including version compatibility, tool calling errors, and debugging patterns, see references/TROUBLESHOOTING.md.
Common Issues
Issue: Response component styles not applied
Solution: Ensure @source "../node_modules/streamdown/dist/index.js"; is in globals.css
Issue: Components not found after installation
Solution: Check components.json for correct path configuration. Default is @/components/ai-elements/
Issue: TypeScript errors with message.parts
Solution: Import and use proper types: import type { UIMessage } from 'ai';
Issue: Streaming not working
Solution: Ensure API route returns result.toUIMessageStreamResponse() not result.toDataStreamResponse() (v5+). For v3, use result.toDataStreamResponse(). See TROUBLESHOOTING.md for version differences.
Issue: Message parts not rendering Solution: Always check part.type and handle all cases (text, tool-call, tool-result, reasoning)
Issue: Tool calling schema validation errors Solution: See TROUBLESHOOTING.md for detailed guidance on tool schema validation, especially the "Invalid schema for function: got 'type: None'" error.
Project Templates
See assets/templates/ for complete project templates:
- basic-chat - Simple chat interface
- advanced-chat - Chat with tools, reasoning, and sources
- multimodal-chat - Chat with image and file upload
- agentic-chat - Multi-agent conversation system
Additional Resources
- Component Reference: references/COMPONENTS.md - Detailed API for each component
- AI SDK Integration: references/AI_SDK_INTEGRATION.md - Deep dive into useChat and streamText
- Streaming Patterns: references/STREAMING.md - Advanced streaming techniques
- Troubleshooting Guide: references/TROUBLESHOOTING.md - Version compatibility, common errors, and debugging patterns
- Examples: references/EXAMPLES.md - Complete code examples
Official Documentation
- AI Elements: https://ai-sdk.dev/elements
- AI SDK: https://ai-sdk.dev
- Streamdown: https://streamdown.ai
- shadcn/ui: https://ui.shadcn.com
Version Compatibility
- AI SDK: v5.x (v6 beta available)
- Next.js: 14.x - 15.x
- React: 18.x - 19.x
- Node.js: 18.x or later
- TypeScript: 5.x recommended
Last Updated: January 2025