- Sequential: components run in a defined order, with each step receiving the previous step’s output (for example: prompt → LLM → output parser).
- Parallel / concurrent: multiple components run at the same time (for example: multiple retrievers or API calls), and their outputs are aggregated and passed downstream.
- Conditional / routing: logic chooses which sub-chain to run based on input or intermediate results.
- Stateful / memory-enabled: chains incorporate memory components to maintain context across invocations (useful for chat or multi-turn workflows).

- One-off responses: Use a minimal sequential chain (prompt → LLM) when you only need a single formatted response.
- Retrieval-augmented generation (RAG): Add a retriever step before the LLM to fetch relevant documents from a knowledge base, then synthesize the retrieved context with the model output.
- Enforced output format: Insert an output parser after the model to validate, normalize, or transform responses into structured formats (JSON, CSV, etc.).
- Stateful conversations: Add memory to persist prior conversation turns or results and feed them back into the prompt/context.
- Parallel enrichment: Run multiple retrievers, APIs, or models in parallel and then aggregate outputs (rank, dedupe, or fuse) before the final step.
- Composability: Build sub-chains and reuse them as single components inside larger pipelines.
Example snippets (conceptual pseudo-code)
- Simple sequential chain (prompt → LLM → parser)
- Retrieval-augmented generation (RAG) with a retriever and combiner
- Parallel retrieval and aggregation
- Keep chains modular: encapsulate repeatable logic in sub-chains and reuse them as building blocks.
- Validate outputs: use output parsers or schema validators early when the downstream system expects structured data.
- Control context size: when combining many documents or tool outputs, apply truncation or scoring to fit the model’s context window.
- Monitor latency and cost: parallel steps can increase responsiveness but may also raise cost; balance concurrency with budget and SLAs.
- Version and test sub-chains: since chains are composable, maintaining tests for each sub-chain prevents regression when reusing them.
Chains provide a modular way to build complex pipelines by combining prompts, models, retrieval, parsing, and functions into reusable units.