Next.js
Next.js App Router with an OpenAI-compatible uncensored API
Stream Abliterator models into a Next.js App Router app: a route handler with the AI SDK, a useChat client page, server-side tools, and keeping the key private.
Create an AI SDK provider with createOpenAICompatible({ baseURL: "https://abliterator.ai/v1", apiKey: process.env.ABL_API_KEY }) in server code, stream from app/api/chat/route.ts with toUIMessageStreamResponse(), and render it with useChat.
In the App Router, the usual setup is a route handler that streams with the AI SDK and a client page that renders the stream with useChat. The provider is the AI SDK OpenAI-compatible provider; the route conventions are from the Next.js route handler docs. Type-checked with tsc --strict against next 15.5, ai 7.0 and @ai-sdk/react 4.0; the route handler was also called directly in Node against a stand-in server.
Install and configure
npm install ai @ai-sdk/openai-compatible @ai-sdk/react zod
echo 'ABL_API_KEY=sk-abl-v1-...' >> .env.local
Create the provider once in server-only code, lib/abl.ts:
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
export const abliterator = createOpenAICompatible({
name: "abliterator",
baseURL: "https://abliterator.ai/v1",
apiKey: process.env.ABL_API_KEY,
includeUsage: true,
});
Do not name the variable NEXT_PUBLIC_ABL_API_KEY. Next.js inlines NEXT_PUBLIC_ variables into the client bundle.
Stream a chat from a route handler
app/api/chat/route.ts receives the UI messages from useChat, converts them and streams back in the UI message format:
import { convertToModelMessages, streamText, type UIMessage } from "ai";
import { abliterator } from "@/lib/abl";
export const maxDuration = 60;
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: abliterator("deepseek-v4.1-flash-abliterated"),
system: "You are a security awareness coach for a company's staff.",
messages: await convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse();
}
convertToModelMessages is async in current AI SDK versions, so await it. The page, app/page.tsx, posts to /api/chat by default:
"use client";
import { useChat } from "@ai-sdk/react";
import { useState } from "react";
export default function Chat() {
const { messages, sendMessage, status } = useChat();
const [input, setInput] = useState("");
return (
<main>
{messages.map((m) => (
<p key={m.id}>
<b>{m.role}:</b>{" "}
{m.parts.map((part, i) => (part.type === "text" ? <span key={i}>{part.text}</span> : null))}
</p>
))}
<form
onSubmit={(e) => {
e.preventDefault();
sendMessage({ text: input });
setInput("");
}}
>
<input value={input} onChange={(e) => setInput(e.target.value)} disabled={status !== "ready"} />
</form>
</main>
);
}
Server-side tools in the route
Tools defined in the route run on your server, so they can read databases or internal APIs that the browser never sees. stopWhen lets the model use the result before answering:
import { convertToModelMessages, stepCountIs, streamText, tool, type UIMessage } from "ai";
import { z } from "zod";
import { abliterator } from "@/lib/abl";
export const maxDuration = 120;
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: abliterator("kimi-k3.1-pro-abliterated"),
system: "You help employees report suspicious emails. Check the reported-phish log first.",
messages: await convertToModelMessages(messages),
tools: {
lookupReports: tool({
description: "Count earlier reports of an email subject line.",
inputSchema: z.object({ subject: z.string() }),
execute: async ({ subject }) => ({ subject, reports: 7, firstSeen: "2026-11-25", verdict: "phishing" }),
}),
},
stopWhen: stepCountIs(3),
});
return result.toUIMessageStreamResponse();
}
Tool calls appear on the client as tool-lookupReports parts in m.parts, so you can render a status line while the tool runs.
Troubleshooting
apiKeyis undefined in production..env.localis not deployed. AddABL_API_KEYto your host's environment settings and redeploy.- The page shows nothing while the network tab shows text. The route returned
toTextStreamResponse(), butuseChatexpectstoUIMessageStreamResponse(). Use the UI message stream, or switch the client to a text stream transport. - Type error:
Promise<ModelMessage[]>is not assignable.convertToModelMessageswas not awaited. Older examples call it synchronously. - 404 on
/v1/responses. The model came from@ai-sdk/openai. Use the compatible provider inlib/abl.ts. - Function timeout. Raise
maxDuration; long tool chains on kimi-k3.1-pro-abliterated can exceed the default.
Frequently asked questions
Can I call Abliterator directly from a client component?
No. That would ship your key to every browser. Keep ABL_API_KEY in .env.local without the NEXT_PUBLIC_ prefix and call Abliterator only from route handlers, server actions or server components.
Why does my stream stop after a few seconds on Vercel?
Serverless functions have a duration limit. Export maxDuration from the route file to raise it within your plan's limit, and prefer deepseek-v4.1-flash-abliterated for chat UIs that need quick replies.
Does the Edge runtime work?
Yes. The AI SDK and the compatible provider only use fetch, so export const runtime = "edge" works if the rest of your route is Edge-compatible.