Skip to content

Model Selector

A menu in the prompt field that lets the person choose which model answers, grouped by provider.

Installation

pnpm dlx shadcn@latest add @silkware/ai-model-selector

To copy the pattern by hand, install the components it composes:

pnpm dlx shadcn@latest add @silkware/dropdown-menu @silkware/input-group

Then install the icon package and copy the pattern into components/ai-elements/model-selector.tsx:

pnpm add lucide-react
components/ai-elements/model-selector.tsx
"use client"
import * as React from "react"import { ArrowUpIcon, ChevronDownIcon, PlusIcon } from "lucide-react"
import {  DropdownMenu,  DropdownMenuGroup,  DropdownMenuItem,  DropdownMenuLabel,  DropdownMenuSeparator,  DropdownMenuTrigger,} from "@/components/ui/dropdown-menu"import {  InputGroup,  InputGroupAddon,  InputGroupButton,  InputGroupTextarea,} from "@/components/ui/input-group"
const providers = [  {    name: "OpenAI",    models: [      { id: "gpt-5", name: "GPT-5" },      { id: "gpt-5-mini", name: "GPT-5 mini" },    ],  },  {    name: "Anthropic",    models: [      { id: "claude-sonnet-4-5", name: "Claude Sonnet 4.5" },      { id: "claude-haiku-4-5", name: "Claude Haiku 4.5" },    ],  },]
const models = providers.flatMap((provider) => provider.models)
const efforts = [  { id: "low", name: "Low" },  { id: "medium", name: "Medium" },  { id: "high", name: "High" },]
export function ModelSelectorDemo() {  const [text, setText] = React.useState("")  const [model, setModel] = React.useState(models[0].id)  const [effort, setEffort] = React.useState(efforts[1].id)  const formRef = React.useRef<HTMLFormElement>(null)  const currentModel = models.find((item) => item.id === model) ?? models[0]  const currentEffort = efforts.find((item) => item.id === effort) ?? efforts[1]  const canSend = text.trim().length > 0
  function onSubmit(event: React.FormEvent<HTMLFormElement>) {    event.preventDefault()    if (!canSend) {      return    }
    setText("")  }
  return (    <div className="mx-auto w-full max-w-lg py-6">      <form ref={formRef} onSubmit={onSubmit}>        <InputGroup>          <InputGroupTextarea            value={text}            onChange={(event) => {              setText(                typeof event === "string" ? event : event.currentTarget.value              )            }}            aria-label="Message"            placeholder="Ask the agent to change your code..."            onKeyDown={(event) => {              if (event.key === "Enter" && !event.shiftKey) {                event.preventDefault()                formRef.current?.requestSubmit()              }            }}            className="p-3"          />          <InputGroupAddon align="block-end">            <InputGroupButton              size="icon-sm"              variant="outline"              className="rounded-full"              aria-label="Attach"            >              <PlusIcon />            </InputGroupButton>            <DropdownMenuTrigger>              <InputGroupButton                size="sm"                variant="outline"                aria-label={`Model: ${currentModel.name}`}                className="ml-auto"              >                {currentModel.name}                <ChevronDownIcon />              </InputGroupButton>              <DropdownMenu                className="w-48"                selectionMode="single"                disallowEmptySelection                selectedKeys={[model]}                onSelectionChange={(keys) => setModel([...keys][0] as string)}              >                {providers.map((provider, index) => (                  <React.Fragment key={provider.name}>                    {index > 0 && <DropdownMenuSeparator />}                    <DropdownMenuGroup>                      <DropdownMenuLabel>{provider.name}</DropdownMenuLabel>                      {provider.models.map((item) => (                        <DropdownMenuItem key={item.id} id={item.id}>                          {item.name}                        </DropdownMenuItem>                      ))}                    </DropdownMenuGroup>                  </React.Fragment>                ))}              </DropdownMenu>            </DropdownMenuTrigger>            <DropdownMenuTrigger>              <InputGroupButton                size="sm"                variant="outline"              >                <span className="text-muted-foreground">Effort</span>                {currentEffort.name}                <ChevronDownIcon />              </InputGroupButton>              <DropdownMenu                className="w-36"                selectionMode="single"                disallowEmptySelection                selectedKeys={[effort]}                onSelectionChange={(keys) => setEffort([...keys][0] as string)}              >                <DropdownMenuGroup>                  <DropdownMenuLabel>Effort</DropdownMenuLabel>                  {efforts.map((item) => (                    <DropdownMenuItem key={item.id} id={item.id}>                      {item.name}                    </DropdownMenuItem>                  ))}                </DropdownMenuGroup>              </DropdownMenu>            </DropdownMenuTrigger>            <InputGroupButton              type="submit"              size="icon-sm"              variant="default"              className="rounded-full"              isDisabled={!canSend}              aria-label="Send"            >              <ArrowUpIcon />            </InputGroupButton>          </InputGroupAddon>        </InputGroup>      </form>    </div>  )}

Usage

import {  DropdownMenu,  DropdownMenuGroup,  DropdownMenuItem,  DropdownMenuLabel,  DropdownMenuTrigger,} from "@/components/ui/dropdown-menu"import { InputGroupButton } from "@/components/ui/input-group"
<DropdownMenuTrigger>  <InputGroupButton size="sm" variant="outline" aria-label={`Model: ${current.name}`}>    {current.name}    <ChevronDownIcon />  </InputGroupButton>  <DropdownMenu    selectionMode="single"    disallowEmptySelection    selectedKeys={[model]}    onSelectionChange={(keys) => setModel([...keys][0] as string)}  >    {providers.map((provider) => (      <DropdownMenuGroup key={provider.name}>        <DropdownMenuLabel>{provider.name}</DropdownMenuLabel>        {provider.models.map((item) => (          <DropdownMenuItem key={item.id} id={item.id}>            {item.name}          </DropdownMenuItem>        ))}      </DropdownMenuGroup>    ))}  </DropdownMenu></DropdownMenuTrigger>

Give each DropdownMenuItem an id: React Aria selects by key, and selectedKeys and onSelectionChange work on those ids. disallowEmptySelection keeps one model chosen at all times. The trigger's aria-label carries the current value, because a screen reader otherwise reads only the visible name without saying what it is.

The pattern has two selectors, model and effort, with the same structure. Remove the one you do not need.

Examples

In the prompt field

The demo places the selector in the footer of an Input Group, next to the effort menu and the send button. Choose a short name for the trigger: the full list lives in the menu.

On its own

The same menu works outside a field, for a setting that applies to a whole task rather than one message.

Supplier review runs

AI SDK

The AI SDK does not choose the model for you: the route does. Send the chosen id with the request, then pick the model on the server from an allow list, never from the raw string.

const { sendMessage } = useChat()
void sendMessage({ text }, { body: { model } })
import { convertToModelMessages, streamText, type UIMessage } from "ai"
const allowed = ["openai/gpt-5", "openai/gpt-5-mini"]
export async function POST(request: Request) {  const { messages, model }: { messages: UIMessage[]; model: string } =    await request.json()
  const result = streamText({    model: allowed.includes(model) ? model : allowed[0],    messages: await convertToModelMessages(messages),  })
  return result.toUIMessageStreamResponse()}

The ids in the pattern (gpt-5, claude-sonnet-4-5) are sample data for the menu. Replace them with the ids your provider expects. Offline, lib/ai.ts scripts the conversation and ignores the model, so the selector changes only what the menu shows.

Accessibility

KeyAction
EnterOpens the menu from the trigger, or selects the focused item.
SpaceOpens the menu from the trigger, or selects the focused item.
ArrowDownMoves to the next item. From the trigger it opens the menu.
ArrowUpMoves to the previous item. From the trigger it also opens the menu.
EscCloses the menu and returns focus to the trigger.

React Aria provides:

  • The menu pattern: roles, typeahead, arrow-key movement and focus return to the trigger.
  • Single selection with the selected item exposed to screen readers, and group labels for each provider.

You provide:

  • An aria-label on the trigger that includes the current value.
  • Plain-language model names. People do not read ids.

Guidelines

When to use

  • When the person can reasonably choose between models that differ in speed, cost or depth.
  • For a choice that applies to the next message and stays until changed.

When not to use

  • For a handful of fixed modes with no model behind them. Use a Toggle Group or Select.
  • When the product picks the model for the person. Show the model that ran, as text.

Content

  • Group by provider and order by capability, not alphabetically.
  • Name the model, and add the trade-off only if the person has to know it ("Fast", "Thorough").
  • Keep the default the middle choice.

Human in the loop

The person must be able to see which model produced an answer, because that changes how much to trust it. Keep the choice visible in the field, and do not switch models without telling them. A change that raises cost should say so before the next send.

Do

Show the current model on the trigger and mark it as selected in the menu.

Don’t

Don’t change the model in the middle of a thread without saying so.

Composition

ComponentWhat it does here
Dropdown MenuThe menu: trigger, groups, labels and single-selection items.
Input GroupThe prompt field the selector sits in, and the button used as the trigger.

The pattern exports one component, ModelSelectorDemo, a complete prompt field with the model and effort menus. The providers, models and efforts arrays at the top are the data.

Changed from the official pattern

The official source makes the attach button a submit button that is disabled while there is text to send. Here it is a plain button that is always available; connect it to a file input as the Prompt Input pattern does. The change is recorded in docs/architecture/decisions.md.