0Pricing
AI Agents with LangChain & Autonomous Workflows · Lezione

Routing e chain condizionali

Create chain che scelgono dinamicamente un percorso in base all’input, inviando ogni richiesta alla sub-chain più appropriata per ottenere workflow più intelligenti e ramificati.

Routing e chain condizionali è una lezione AI Agents with LangChain & Autonomous Workflows gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento AI Agents with LangChain & Autonomous Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Beyond Straight-Line Chains

Sequential chains always run the same steps in order. But real workflows branch: a billing question and a coding question need different handling.

This lesson covers routing chains that choose a path based on the input.

What a Router Does

A router inspects the input, decides which destination chain best fits, and forwards the input there. It is a dispatcher in front of several specialist chains.

input -> router -> { billing_chain | code_chain | general_chain }

Defining Destination Chains

Each destination is a normal chain tuned for one kind of task, with its own prompt and configuration.

billing = LLMChain(llm=llm, prompt=billing_prompt)
coding  = LLMChain(llm=llm, prompt=coding_prompt)

Describing Each Route

The router needs to know what each destination is for. Provide a name and a short description so it can match an input to the right one.

routes = [
  {'name': 'billing', 'description': 'invoices, refunds, payments'},
  {'name': 'coding',  'description': 'programming and debugging help'}
]

LLM-Based Routing

One approach asks the LLM itself to classify the input and name the destination. It is flexible and handles fuzzy intent, but adds a model call and some unpredictability.

# router prompt asks model to output: {'destination': 'billing', 'input': ...}

Rule-Based Routing

When intent is clear from structure, a deterministic function can route faster and cheaper than an LLM.

def route(query):
    if 'refund' in query.lower():
        return 'billing'
    return 'general'

The Default Route

Some inputs match no specialist. Always provide a default destination so the chain never fails on an unexpected request.

default_chain = LLMChain(llm=llm, prompt=general_prompt)

Composing the Router Chain

LangChain's routing chain ties the router, the destination map, and the default together into a single callable that picks the path automatically.

from langchain.chains.router import MultiPromptChain
chain = MultiPromptChain(
    router_chain=router,
    destination_chains={'billing': billing, 'coding': coding},
    default_chain=default_chain)

Handling Misroutes

Routing is a prediction and can be wrong. Log the chosen destination, let a destination decline and fall back, and monitor misroute rates so you can refine descriptions over time.

LLM vs Rule-Based Tradeoffs

Choose your router by the situation:

  • Rules: fast, cheap, deterministic; brittle for fuzzy intent
  • LLM: flexible, handles nuance; slower, costs a call, less predictable

Hybrids use rules first and fall back to the LLM.

A Routing Workflow

Putting it together:

  • Build specialist destination chains
  • Describe each route clearly
  • Choose rule-based, LLM-based, or hybrid routing
  • Always include a default and monitor misroutes

Quick Check

Test your understanding of routing chains.

Recap

You learned to build branching, conditional chains.

  • A router dispatches input to specialist destination chains
  • Routing can be rule-based, LLM-based, or hybrid
  • Clear route descriptions improve accuracy
  • Always include a default and monitor misroutes

Domande Frequenti

La lezione «Routing e chain condizionali» è gratuita?

Sì — il testo completo di «Routing e chain condizionali» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso AI Agents with LangChain & Autonomous Workflows, passa a CoddyKit PRO. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.

Cosa imparerò in «Routing e chain condizionali»?

Create chain che scelgono dinamicamente un percorso in base all’input, inviando ogni richiesta alla sub-chain più appropriata per ottenere workflow più intelligenti e ramificati. Eserciti AI Agents with LangChain & Autonomous Workflows con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare AI Agents with LangChain & Autonomous Workflows?

Non è richiesta alcuna esperienza precedente. AI Agents with LangChain & Autonomous Workflows su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Routing e chain condizionali»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione AI Agents with LangChain & Autonomous Workflows?

Sì. Ogni lezione AI Agents with LangChain & Autonomous Workflows include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione alle chain di LangChain
  2. Chain sequenziali e semplici
  3. Personalizzare la logica delle chain
  4. Routing e chain condizionali
← Torna a AI Agents with LangChain & Autonomous Workflows