AI Integration Guide
Add AI-powered features to your app: chatbots, content generation, summarization, and more. No machine learning degree required.
In this chapter
- What it takes to add AI features like chat or summarization
- How API keys work and why they must stay on the backend
- Which AI model to pick for different jobs (and what each costs)
- Copy-paste prompts to plug AI into your existing project
What You Can Add
Pick Your AI Provider
The big three are OpenAI, Anthropic Claude, and Google Gemini. They all do roughly the same things, and all three now read up to about 1 million tokens at once. The differences are price, free tier, tooling, and vibe. Start here before you commit to one.
| Provider | Free tier? | Typical cost | Pick it for |
|---|---|---|---|
| OpenAI (GPT-6 models) | Pay-as-you-go | $ – $$ | Broadest tooling, image generation (GPT Image), the safe default |
| Anthropic (Claude) | Small test credit | $ – $$ | Following instructions, writing, long documents, code |
| Google (Gemini) | ✓ Real free tier | $ – $$ | Side projects, images + video in, free to start |
| Open-source (Replicate, HF, Groq) | Varies | Varies | Niche models, image/audio, ultra-fast inference (Groq) |
- Biggest ecosystem: every library supports it
- Native image generation (GPT Image)
- Great docs and endless tutorials
- No free tier: pay-as-you-go from the start
- Can get expensive at volume
- Best at following instructions precisely
- Handles 1M-token contexts (several books at once)
- Strong reasoning, writing, and coding
- No image generation (input only)
- No free tier, just a small test credit
- Real free tier: low rate limits but zero cost
- Multimodal: takes images, video, audio as input
- Huge context: 1M tokens
- Built-in image generation (Nano Banana)
- Free-tier inputs may be used to train Google's models
- Smaller ecosystem than OpenAI
- Thousands of open-source models
- Pay per second, no minimum
- Best for image, video, and audio models
- Cold starts can be slow (5–30s)
- Open-source model quality varies
- Blazingly fast: 10x+ faster than typical APIs
- OpenAI-compatible API format
- Only runs open-source models
- Fewer model options than Replicate
- Massive free model library
- Community-driven, new models daily
- One API for models from many providers
- No free inference credits: you buy credits (PRO includes $2/month)
- Requires more knowledge to pick the right model
Key Concepts
UI Patterns for AI Features
- Message bubbles (user on right, AI on left)
- Text input with send button at bottom
- Auto-scroll to newest message
- Typing indicator while AI responds
- Words appear one by one, like typing
- Cursor blinks at the end of the text
- Stop button to cancel generation
- Smooth scrolling as text grows
- Animated dots or spinner while waiting
- Skeleton text placeholders
- 'Thinking...' or 'Generating...' labels
- Estimated time for long operations
Tools & SDKs
AI Prompts for Adding AI Features
Copy-paste these prompts to quickly add AI features to your project.
“Add an AI chat widget using the OpenAI API with streaming responses. Show messages in a chat bubble layout with the user on the right and AI on the left. Include a text input and send button.”
“Set up the Gemini API in my Next.js app using the free tier. Create an /api/chat route that uses the latest Gemini Flash model with streaming, and show me the env variable I need and where to get the API key.”
“Swap the chat widget in my project from OpenAI to Gemini using the Vercel AI SDK. Keep everything else the same and point me to which env variables I need to change.”
“Add a 'Generate Description' button next to my product form. When clicked, use the AI to generate a product description based on the product name and category fields. Show a loading spinner.”
“Add a text summarization feature. Users paste an article into a textarea and click 'Summarize'. Use the Anthropic API to return a 3-bullet summary. Stream the response.”
“Set up the Vercel AI SDK with OpenAI in my Next.js app. Create an API route at /api/chat and a chat page with streaming messages, a stop button, and conversation history.”
“Add an AI-powered search to my documentation site. Use OpenAI embeddings to find the most relevant docs based on the user's question, then generate a natural language answer with citations.”
Frequently Asked Questions
How much does it cost to add AI features to my app?
AI providers charge per token (roughly per word). A typical chat message costs $0.001-0.01. For a small app, expect $5-20/month. Gemini has a real free tier for low-volume use. Anthropic gives new accounts a small test credit, and OpenAI is pay-as-you-go from the start.
Which AI model should I use?
For side projects, start with Gemini Flash: it has a free tier. For serious products, OpenAI's GPT-6 models (broadest ecosystem) or Claude (best at following instructions). For cheap bulk work like classification or summarization, use each provider's smallest model: GPT-6 Luna, Claude Haiku, or Gemini Flash-Lite. Model names change every few months, so ask your AI tool to check the provider's models page for the current one.
Is Gemini really free?
Yes, for low volume. You get a daily token quota on Gemini Flash with per-minute rate limits. Two catches: free-tier inputs may be used to train Google's models, and you'll hit the rate limits fast if your app gets popular. Paid mode opts you out of training.
Can I switch providers later?
Yes, use the Vercel AI SDK. It wraps OpenAI, Anthropic, Gemini, and more behind a unified interface. Start with one provider, then swap models in a single line of code if you need better quality, lower cost, or a free tier.
How do I keep my API key safe?
Never put your API key in frontend code. Anyone can see it in the browser. Always call AI APIs from your backend (API routes in Next.js). Store the key in environment variables and never commit it to Git. Your AI tool knows how to set this up securely.