
Build a Model Router in 20 Lines with WhichModel
Build a Model Router in 20 Lines with WhichModel You have an AI agent that calls LLMs. It always uses the same model. You want it to pick the right model for each task — optimising for cost, capability, and quality — without maintaining a pricing database yourself. Here is how to build a model router in 20 lines using WhichModel and the MCP TypeScript SDK. The Code import { Client } from " @modelcontextprotocol/sdk/client/index.js " ; import { StreamableHTTPClientTransport } from " @modelcontextprotocol/sdk/client/streamableHttp.js " ; const client = new Client ({ name : " router " , version : " 1.0 " }); await client . connect ( new StreamableHTTPClientTransport ( new URL ( " https://whichmodel.dev/mcp " )) ); async function pickModel ( taskType : string , complexity : string , budget ?: number ) { const result = await client . callTool ({ name : " recommend_model " , arguments : { task_type : taskType , complexity , ...( budget && { budget_per_call : budget }), }, }); return JSON . p
Continue reading on Dev.to
Opens in a new tab



