# LLMRouterChain

> **Class** in `langchain_classic`

📖 [View in docs](https://reference.langchain.com/python/langchain-classic/chains/router/llm_router/LLMRouterChain)

A router chain that uses an LLM chain to perform routing.

This class is deprecated. See below for a replacement, which offers several
benefits, including streaming and batch support.

Below is an example implementation:

    ```python
    from operator import itemgetter
    from typing import Literal
    from typing_extensions import TypedDict

    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.runnables import RunnableLambda, RunnablePassthrough
    from langchain_openai import ChatOpenAI

    model = ChatOpenAI(model="gpt-4o-mini")

    prompt_1 = ChatPromptTemplate.from_messages(
        [
            ("system", "You are an expert on animals."),
            ("human", "{query}"),
        ]
    )
    prompt_2 = ChatPromptTemplate.from_messages(
        [
            ("system", "You are an expert on vegetables."),
            ("human", "{query}"),
        ]
    )

    chain_1 = prompt_1 | model | StrOutputParser()
    chain_2 = prompt_2 | model | StrOutputParser()

    route_system = "Route the user's query to either the animal "
    "or vegetable expert."
    route_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", route_system),
            ("human", "{query}"),
        ]
    )

    class RouteQuery(TypedDict):
        """Route query to destination."""
        destination: Literal["animal", "vegetable"]

    route_chain = (
        route_prompt
        | model.with_structured_output(RouteQuery)
        | itemgetter("destination")
    )

    chain = {
        "destination": route_chain,  # "animal" or "vegetable"
        "query": lambda x: x["query"],  # pass through input query
    } | RunnableLambda(
        # if animal, chain_1. otherwise, chain_2.
        lambda x: chain_1 if x["destination"] == "animal" else chain_2,
    )

    chain.invoke({"query": "what color are carrots"})

    ```

## Signature

```python
LLMRouterChain()
```

## Extends

- `RouterChain`

## Properties

- `llm_chain`
- `input_keys`

## Methods

- [`from_llm()`](https://reference.langchain.com/python/langchain-classic/chains/router/llm_router/LLMRouterChain/from_llm)

## ⚠️ Deprecated

Deprecated since version 0.2.12. Use langchain.agents.create_agent instead. Will be removed in version 2.0.0. Build routing logic with `create_agent` (e.g. with subagents or prompt-selection middleware). See https://docs.langchain.com/oss/python/langchain/agents

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/42f8f79293cfb7589e5bc1d74a8ae4dfd0bf15e3/libs/langchain/langchain_classic/chains/router/llm_router.py#L24)