Skip to main content
Routor works with LangChain because ChatOpenAI accepts a custom baseURL. You get intelligent routing with zero changes to your chains, agents, or tools. This guide walks through a complete agent built with LangGraph that uses Routor for every model call.

Setup

Install the LangChain packages you need:
Create a Routor-backed model. The only differences from a normal ChatOpenAI instance are the baseURL and the apiKey:
Routor handles the model selection from here. Every invoke, stream, or tool call goes through the router.

A Complete LangGraph Agent

This example builds a research agent with LangGraph. The agent has two tools: a web search tool and a calculator. Routor detects the tool definitions and automatically routes to a tool-capable model ranked by function-calling accuracy.

Python

Node.js / TypeScript


How Routor Handles the Agent

When the agent sends a request with tools attached, Routor:
  1. Detects the tools array in the request
  2. Classifies the prompt difficulty and task category
  3. Narrows the model pool to only tool-calling models
  4. Ranks them by BFCL function-calling accuracy for the detected scenario (single, parallel, or multi-turn)
  5. Picks the best-value model and falls back silently if it fails
You do not configure any of this. The tools are detected automatically from the request body.

Using a Routing Profile

For a production agent, create a Routing Profile in the dashboard with constraints like a tier floor or cost cap. Each profile gets its own API key, so the profile rules apply with no code changes:

Reading Routing Metadata

LangChain only surfaces the standard OpenAI response fields, so the full routor object from the response body is not exposed through response_metadata or additional_kwargs. What you can read in LangChain is the actual model that handled the request:
For the full routing decision (tier, confidence, savings), check the Logs page in the dashboard, call the API directly with the OpenAI SDK (the response body includes a routor object), or use the Debug Endpoint to inspect a decision without spending credits.

Streaming

LangGraph supports streaming token-by-token. Routor passes through the stream in OpenAI SSE format, so it works without changes: