📄 ai-sdk/docs/advanced/model-as-router

File: model-as-router.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/docs/advanced/model-as-router

AI SDK

Menu

v5 (Latest)

AI SDK 5.x

AI SDK by Vercel

AI SDK 6 Beta

Foundations

Overview

Providers and Models

Prompts

Tools

Streaming

Getting Started

Navigating the Library

Next.js App Router

Next.js Pages Router

Svelte

Vue.js (Nuxt)

Node.js

Expo

Agents

Agents

Building Agents

Workflow Patterns

Loop Control

AI SDK Core

Overview

Generating Text

Generating Structured Data

Tool Calling

Model Context Protocol (MCP) Tools

Prompt Engineering

Settings

Embeddings

Image Generation

Transcription

Speech

Language Model Middleware

Provider & Model Management

Error Handling

Testing

Telemetry

AI SDK UI

Overview

Chatbot

Chatbot Message Persistence

Chatbot Resume Streams

Chatbot Tool Usage

Generative User Interfaces

Completion

Object Generation

Streaming Custom Data

Error Handling

Transport

Reading UIMessage Streams

Message Metadata

Stream Protocols

AI SDK RSC

Advanced

Prompt Engineering

Stopping Streams

Backpressure

Caching

Multiple Streamables

Rate Limiting

Rendering UI with Language Models

Language Models as Routers

Multistep Interfaces

Sequential Generations

Vercel Deployment Guide

Reference

AI SDK Core

AI SDK UI

AI SDK RSC

Stream Helpers

AI SDK Errors

Migration Guides

Troubleshooting

Copy markdown

Generative User Interfaces

==========================================================================================================

Since language models can render user interfaces as part of their generations, the resulting model generations are referred to as generative user interfaces.

In this section we will learn more about generative user interfaces and their impact on the way AI applications are built.

Deterministic Routes and Probabilistic Routing


Generative user interfaces are not deterministic in nature because they depend on the model's generation output. Since these generations are probabilistic in nature, it is possible for every user query to result in a different user interface.

Users expect their experience using your application to be predictable, so non-deterministic user interfaces can sound like a bad idea at first. However, language models can be set up to limit their generations to a particular set of outputs using their ability to call functions.

When language models are provided with a set of function definitions and instructed to execute any of them based on user query, they do either one of the following things:

  • Execute a function that is most relevant to the user query.
  • Not execute any function if the user query is out of bounds of the set of functions available to them.

app/actions.ts

const sendMessage = (prompt: string) =>  generateText({    model: 'gpt-3.5-turbo',    system: 'you are a friendly weather assistant!',    prompt,    tools: {      getWeather: {        description: 'Get the weather in a location',        parameters: z.object({          location: z.string().describe('The location to get the weather for'),        }),        execute: async ({ location }: { location: string }) => ({          location,          temperature: 72 + Math.floor(Math.random() * 21) - 10,        }),      },    },  });
sendMessage('What is the weather in San Francisco?'); // getWeather is calledsendMessage('What is the weather in New York?'); // getWeather is calledsendMessage('What events are happening in London?'); // No function is called

This way, it is possible to ensure that the generations result in deterministic outputs, while the choice a model makes still remains to be probabilistic.

This emergent ability exhibited by a language model to choose whether a function needs to be executed or not based on a user query is believed to be models emulating "reasoning".

As a result, the combination of language models being able to reason which function to execute as well as render user interfaces at the same time gives you the ability to build applications where language models can be used as a router.

Language Models as Routers


Historically, developers had to write routing logic that connected different parts of an application to be navigable by a user and complete a specific task.

In web applications today, most of the routing logic takes place in the form of routes:

  • /login would navigate you to a page with a login form.
  • /user/john would navigate you to a page with profile details about John.
  • /api/events?limit=5 would display the five most recent events from an events database.

While routes help you build web applications that connect different parts of an application into a seamless user experience, it can also be a burden to manage them as the complexity of applications grow.

Next.js has helped reduce complexity in developing with routes by introducing:

  • File-based routing system
  • Dynamic routing
  • API routes
  • Middleware
  • App router, and so on...

With language models becoming better at reasoning, we believe that there is a future where developers only write core application specific components while models take care of routing them based on the user's state in an application.

With generative user interfaces, the language model decides which user interface to render based on the user's state in the application, giving users the flexibility to interact with your application in a conversational manner instead of navigating through a series of predefined routes.

Routing by parameters

For routes like:

  • /profile/[username]
  • /search?q=[query]
  • /media/[id]

that have segments dependent on dynamic data, the language model can generate the correct parameters and render the user interface.

For example, when you're in a search application, you can ask the language model to search for artworks from different artists. The language model will call the search function with the artist's name as a parameter and render the search results.

Art made by Van Gogh?

searchImages("Van Gogh")

Here are a few of his notable works

Starry Night

Starry Night

Sunflowers

Sunflowers

Olive Trees

Olive Trees

Wow, these look great! How about Monet?

searchImages("Monet")

Sure! Here are a few of his paintings

Frau im Gartenfrau

Frau im Gartenfrau

Cliff Walk

Cliff Walk

Waves

Waves

Media Search

Let your users see more than words can say by rendering components directly within your search experience.

Routing by sequence

For actions that require a sequence of steps to be completed by navigating through different routes, the language model can generate the correct sequence of routes to complete in order to fulfill the user's request.

For example, when you're in a calendar application, you can ask the language model to schedule a happy hour evening with your friends. The language model will then understand your request and will perform the right sequence of tool calls to:

  1. Lookup your calendar
  2. Lookup your friends' calendars
  3. Determine the best time for everyone
  4. Search for nearby happy hour spots
  5. Create an event and send out invites to your friends

I'd like to get drinks with Max tomorrow evening after studio!

searchContacts("Max")

max's avatar

max

@mleiter

shu's avatar

shu

@shuding

getEvents("2023-10-18", ["jrmy", "mleiter"])

4PM

5PM

6PM

7PM

studio

4-6 PM

searchNearby("Bar")

wild colonial

200m

the eddy

1.3km

createEvent("2023-10-18", ["jrmy", "mleiter"])

4PM

5PM

6PM

7PM

studio

4-6 PM

Drinks at Wild Colonial

6-7 PM

Exciting! Max is free around that time and Wild Colonial is right around the corner, would you like me to mark it on your calendar?

Sure, sounds good!

Planning an Event

The model calls functions and generates interfaces based on user intent, acting like a router.

Just by defining functions to lookup contacts, pull events from a calendar, and search for nearby locations, the model is able to sequentially navigate the routes for you.

To learn more, check out these examples using the streamUI function to stream generative user interfaces to the client based on the response from the language model.

On this page

Generative User Interfaces

Deterministic Routes and Probabilistic Routing

Language Models as Routers

Routing by parameters

Routing by sequence

Deploy and Scale AI Apps with Vercel.

Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.

Trusted by industry leaders:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert