Documentation/Getting Started/5-Minute Step-by-Step Tutorial
5-Minute Step-by-Step Tutorial
Follow this step-by-step guide to build a fully functional, metered AI chat application with real-time cost tracking and Stripe paywalls.
Step 1: Create Your Next.js App
Open your terminal and create a new Next.js application:
bash
npx create-next-app@latest my-ai-app --typescript --tailwind --app --eslintcd my-ai-appStep 2: Install VibezCheck
Install vibezcheck along with the Vercel AI SDK:
bash
npm install vibezcheck ai @ai-sdk/openai stripeStep 3: Build the Metered AI Route
Create a new file at app/api/chat/route.ts. This is where the AI streams responses and vibezcheck counts tokens:
typescript
// app/api/chat/route.tsimport { streamText } from 'ai';import { vibezcheck } from 'vibezcheck';export const runtime = 'nodejs';export const dynamic = 'force-dynamic';export async function POST(req: Request) { const { messages, customer = 'demo@example.com' } = await req.json(); // ⚡ 1-Line Model Meter return streamText({ model: vibezcheck('openai/gpt--mini', { customer, maxCostPerCallUSD: 0.10, // Safety ceiling: abort if single call exceeds $0.10 onUsage: (event) => { console.log(`✅ Tokens: ${event.usage.totalTokens} | Cost: $${event.cost.totalUSD.toFixed(6)}`); }, }), messages, }).toDataStreamResponse();}Step 4: Connect the React Chat & Counter
Open app/page.tsx and paste this simple 1-hook chat interface:
tsx
// app/page.tsx'use client';import { VibezSessionProvider, useVibezChat, VibezSessionWidget } from 'vibezcheck/react';function Chat() { const { messages, input, handleInputChange, handleSubmit, isLoading } = useVibezChat({ model: 'gpt-4o-mini', customer: 'demo@example.com', }); return ( <main className="max-w-xl mx-auto p-6 space-y-4"> <h1 className="text-xl font-bold">My AI Chat</h1> {/* Messages Feed */} <div className="space-y-3 border border-slate-200 rounded-xl p-4 min-h-[] bg-slate-50"> {messages.map((m) => ( <div key={m.id} className="text-sm"> <strong>{m.role === 'user' ? 'You: ' : 'AI: '}</strong> <span>{m.content}</span> </div> ))} {isLoading && <div className="text-xs text-slate-400 italic">Thinking...</div>} </div> {/* Input Form */} <form onSubmit={handleSubmit} className="flex gap-2"> <input value={input} onChange={handleInputChange} placeholder="Ask a question..." className="flex-1 px-3 py-2 border border-slate-300 rounded-lg text-sm" /> <button type="submit" disabled={isLoading} className="px-4 py-2 bg-black text-white rounded-lg text-sm font-medium disabled:opacity-50" > Send </button> </form> {/* Floating real-time token & dollar counter */} <VibezSessionWidget theme="light" position="bottom-right" /> </main> );}export default function App() { return ( <VibezSessionProvider> <Chat /> </VibezSessionProvider> );}Step 5: Run and Test Locally
Start your development server:
bash
npm run devOpen http://localhost:3000 in your browser, type a message, and check your terminal. You will see real-time token counts and dollar amounts logged instantly with 0 database setup required!