LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Browser
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
  • Tools
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Decisions
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Compat
  • Event
  • Llms
  • Openai Completions Stream
  • Profile
  • Stream
  • Structured Output
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Testing
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Gateway
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Standard Schema
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Uuid
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

Chat LangChain

What can I help with?

Enter to sendĀ·Shift+Enter new line

Menu

LangChain
BrowserUniversalHubNodeLoadSerializableEncoder BackedFile SystemIn MemoryTools
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextDecisionsBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsCompatEventLlmsOpenai Completions StreamProfileStreamStructured OutputLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryTestingToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingGatewayHashJson PatchJson SchemaMathSsrfStandard SchemaStreamTestingTiktokenTypesUuidVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScriptlangchainindexmodelRouterMiddleware
Functionā—Since v1.6

modelRouterMiddleware

Select an agent's model with a decision model's Choice answer (beta).

The middleware classifies the latest human message once before an agent run, stores the complete ChoiceAnswer in agent state as modelRoute, and uses its selected label for every model call in the run. Keeping the complete answer makes probabilities and confidence available in state and traces. Classifier failures propagate and terminate the run rather than silently selecting a different model.

Copy
modelRouterMiddleware(
  config: ModelRouterMiddlewareConfig
): AgentMiddleware<ZodObject<__type, strip>, undefined, unknown, readonly ClientTool | ServerTool[], readonly []>

Parameters

NameTypeDescription
config*ModelRouterMiddlewareConfig

Example

Copy
import { createAgent, modelRouterMiddleware } from "langchain";

const router = modelRouterMiddleware({
  choices: {
    fast: {
      model: "openai:gpt-5-mini",
      criteria: "Simple, well-scoped tasks.",
    },
    powerful: {
      model: powerfulModel,
      criteria: "Complex tasks requiring deeper reasoning.",
    },
  },
  instructions: "Choose the least costly model suited to the task.",
  classifier,
});
const agent = createAgent({
  model: "openai:gpt-5-mini",
  middleware: [router],
});
View source on GitHub