Skip to main content
Chains are the building blocks of LangChain applications. They allow you to chain together multiple components, such as prompt templates, language models, and output parsers, into a single, unified execution pipeline.

Why Use LCEL?

LangChain Expression Language (LCEL) is a declarative way to compose chains. It offers several benefits out of the box:
  1. First-class Streaming Support: When you build your chains with LCEL, you get the best possible time-to-first-token (time elapsed until the first chunk of output comes out).
  2. Async Support: Any chain built with LCEL can be called both with the synchronous API (e.g., .invoke()) and with the asynchronous API (e.g., .ainvoke()).
  3. Optimized Parallel Execution: Whenever your LCEL chains have steps that can be executed in parallel, LangChain executes them automatically.

Structure of a Basic Chain

At its core, a chain consists of:
  • Input: The starting dictionary or string (e.g., {"topic": "science"}).
  • Prompt Template: Takes raw input and formats it into prompt messages.
  • Model: Takes prompt messages and returns an AI message response.
  • Parser: Takes the AI message and extracts raw text or structured data.
Using LCEL, you can link these components together cleanly using the pipe operator (|):
In the next sections, we will explore chains in detail, starting with basic chain configurations to advanced patterns like parallel execution and dynamic routing.