Building a ChatGPT clone with Next.js + Claude

Building a ChatGPT clone with Next.js + Claude
A "ChatGPT clone" is a misleading framing. ChatGPT is a product with a research lab behind it. What you actually want is a streaming chat interface tied to your own model provider and your own database, built in a few hours, not a few months.
The minimum working chat
Three files get you a working chat with claude-opus-4-7 or claude-sonnet-4-6 (verify in current Anthropic docs, model names move):
1. Route handler, app/api/chat/route.ts. A POST handler that takes a messages array, calls Anthropic's streaming endpoint via the Vercel AI SDK (@ai-sdk/anthropic), and returns the streamed response. The AI SDK normalizes streaming across providers so swapping to OpenAI later is a one-line change.
2. Chat UI, app/chat/page.tsx. A client component using useChat from ai/react. The hook owns the messages state, the input, the submit handler, and the streaming reader. About fifty lines of JSX for a working interface.
3. Database tables. Two tables, threads(id, workspace_id, title, created_at) and messages(id, thread_id, role, content, created_at). Store the entire message on every turn so reloading the thread is one query. For multi-tenant apps, wire workspace_id and RLS so threads cannot leak across tenants.
That is a working chat. Everything below is what separates a demo from a product.
What separates the demo from the product
- Auth and persistence. The demo stores messages in React state and forgets them on reload. The product writes to
messagesin the same route handler that streams the response, after the stream completes. UseonFinishfrom the AI SDK to write the final assistant message, do not try to write during the stream. - Multi-tenant scoping. Every message belongs to a workspace. Every read filters by workspace. The pattern is identical to the /features/postgres-rls-boilerplate approach, set the workspace context at the start of the request, let RLS enforce isolation.
- Token accounting. Anthropic returns input and output token counts with the response. Store them per message. The total per thread or per workspace becomes the basis for the credit ledger.
- Credit metering. Before the model call, check the workspace's credit balance. After the call, debit the cost. If the balance is zero, return a "top up" error instead of hitting the API. This is the difference between a free tool and a paid product, and it is the headline feature of /saasforge-ai.
- System prompt management. A real product has prompts that change as you tune behavior. Store the active system prompt in your database keyed by feature or by workspace so changing it does not require a deploy.
- Tool use and structured output. Once chat works, the next request is "can it call my function" or "can it return a JSON object." Both are first-class in the AI SDK,
toolsandexperimental_output(verify in current AI SDK docs).
What the AI SDK handles for you
The streaming plumbing, chunked transfer encoding, the React hook that re-renders on every chunk, the abort signal when the user closes the tab, is solved by the SDK. You do not write it. The same is true for retries, the message normalization across providers, and the structured-output decoders.
What the SDK does not do: persistence, auth, billing, multi-provider failover across vendors, prompt management. Those are product concerns and they are exactly the layer a SaaS template should ship pre-wired.
For chat plus RAG plus credits as a working Next.js 16 starter, Claude and OpenAI via the AI SDK, Supabase Auth, Stripe credit metering, message persistence with RLS, /saasforge-ai is the shortest path. The RAG pipeline that turns uploaded documents into retrievable context lives at /features/pgvector-chat. Start from the working integration; tune the prompts and the UX from there.
Keep reading on related features
RAG pipeline in a Next.js SaaS with Supabase pgvector
A retrieval-augmented generation pipeline ingests documents (uploads), splits them into chunks, embeds the chunks with a model,…
AI SaaS starter for Next.js, chat, RAG, credits, Stripe
An AI SaaS starter is more than a chat demo. SaaSForge AI combines streaming chat against Claude and OpenAI, a document-upload…
Ship this with a Boilerlykit template
Skip the wiring. Each template ships the patterns from this article as production code with MDX docs.