
Build a chatbot in Next.js — streaming, memory, and UI
A complete Next.js 15 chatbot with server-sent streaming and message history.
Next.js + Vercel AI SDK is the shortest path to a production chatbot. Streaming out of the box, edge runtime friendly.
The route handler#
typescript
import { streamText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
const tokenrex = createOpenAI({ apiKey: process.env.TOKENREX_API_KEY!, baseURL: "https://tokenrex.ai/api/public/v1" });
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({ model: tokenrex("gpt-5.4-mini"), messages });
return result.toDataStreamResponse();
}The UI#
Use the AI SDK's useChat hook — it handles the streaming state, message list, and input for you.
Models in this article
Review current prices, specifications and source-linked benchmarks.
Build this with TokenRex
TokenRex is an LLM marketplace by Asirex Labs. Choose an enabled model, create a private API key and use the supported endpoint shown in Docs. Check live prices before your first request.