Skip to Content
Kokonut UI

AI-20AINew, October 21, 2026

AI Token Alternatives

An answer where every unsure word shows what else the model weighed. Pick another word and the reply writes itself again from there.

Install with
lucide-react, motion

Usage

The alternatives come from your model's logprobs: the other words it nearly wrote at each step. fromLogprobs turns an OpenAI-style logprobs.content list into the answer, making a slot of every word where another one was at least 8% likely, and keeping the real probability of every other word for the confidence view.

Pass compose and it's called with the word picked in each slot. Ask the model again from that point and return the new answer, or a Promise of it: the words up to the pick stay, and the rest is written again once it lands. A throw shows the error's message under the answer.

"use client";

import AITokenAlternatives, { fromLogprobs } from "@/components/kokonutui/ai-token-alternatives";

export function Reply({ question }: { question: string }) {
  return (
    <AITokenAlternatives
      compose={async (choice, signal) => {
        const res = await fetch("/api/alternatives", {
          method: "POST",
          body: JSON.stringify({ question, choice }),
          signal,
        });
        if (!res.ok) {
          throw new Error("Couldn't reach the model. Try another word.");
        }
        const { logprobs } = await res.json();
        return fromLogprobs(logprobs);
      }}
      prompt={question}
    />
  );
}
// app/api/alternatives/route.ts
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";

export async function POST(req: Request) {
  const { question, choice } = await req.json();
  const { providerMetadata } = await generateText({
    model: openai("gpt-4.1-mini"),
    prompt: question,
    // Your choice of words so far, as a prefix the model continues from.
    system: Object.keys(choice).length ? `Keep these words: ${Object.values(choice).join(", ")}` : undefined,
    providerOptions: { openai: { logprobs: 4 } },
  });
  return Response.json({ logprobs: providerMetadata?.openai?.logprobs ?? [] });
}

Slot ids from fromLogprobs follow token positions (t0, t1…), so the words before a pick keep their ids from one take to the next.

PROSlash Command Menu and more like it are in Kokonut UI Pro

Installation

AI Token Alternatives is a free, open-source React component in the Kokonut UI AI collection. Add it with the shadcn CLI, which copies the source into components/kokonutui/ai-token-alternatives.tsx, so the code is yours to change.

npx shadcn@latest add @kokonutui/ai-token-alternatives

Or add it by hand: install its packages, then copy the source from Copy code above into components/kokonutui/ai-token-alternatives.tsx.

npm install lucide-react motion

Props

prompt
string

The user's message, drawn as a bubble. Only the demo draws one by default.

slots
TokenSlot[]

The words with alternatives. compose can return new ones instead.

compose
(choice: Record<string, string>, signal: AbortSignal) => ComposedAnswer | Promise<ComposedAnswer>

Builds the answer from the word picked in each slot. Return it or a Promise of it: after a pick, the words up to that one stay and the rest is written again once it arrives. signal aborts when another pick replaces this one. Throw and the answer shows the error's message.

className
string

Extra classes for the root element.

TokenSlot

idrequired
string
optionsrequired
TokenOption[]

Most likely first. The first one is what the model wrote.

ComposedAnswer

An answer, with its own slots when they change from one take to the next.

type ComposedAnswer =
  | AnswerPiece[]
  | { pieces: AnswerPiece[]; slots: TokenSlot[] };

TokenOption

textrequired
string
prequired
number

How likely the model was to pick this word, from 0 to 1.

AnswerPiece

Plain text, plain text with the model's probability for it, or a word the model nearly wrote differently.

type AnswerPiece =
  | string
  | { text: string; p: number }
  | { slot: string };

Dependencies

NeedsVersionComes from
lucide-react^0.556.0npm, installed by the CLI
motion^12.23.25npm, installed by the CLI
cn from lib/utils.tsshadcn init
React19Your app
Tailwind CSS4Your app

No provider, font or global CSS to set up: the file works as soon as it lands.