VOOZH about

URL: https://www.geeksforgeeks.org/data-science/custom-chains-in-langchain/

⇱ Custom Chains in langchain - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Custom Chains in langchain

Last Updated : 4 Nov, 2025

In LangChain, chains act as step-by-step workflows that complete tasks in sequence. Each step can use an LLM to process or transform data and then interact with external tools if needed. Built-in chains are like ready-made templates for common tasks. They link together LLMs, prompts and tools using pre-defined logic, helping developers save time by avoiding manual setup for every step.

Common Built-in Chains:

  • LLMChain: The most basic chain. It simply takes an input, formats it using a prompt and passes it to an LLM to get a response.
  • Sequential Chains: These link multiple sub-chains together, where the output of one step automatically becomes the input for the next. It's great for breaking down a complex problem.

Example: Step 1 generates a list of travel ideas and Step 2 uses that list to create a detailed itinerary.

Limitation

  • Less Flexible: Great for standard tasks but hard to modify for custom workflows.
  • Can Be Slow: Workflows with many steps or external API calls may take longer to execute.
  • Debugging is Hard: When something goes wrong in a long multi-step chain, figuring out where the issue is can be tricky.

Custom Chains

Custom Chains let developers design personalized workflows that connect LLMs, tools, and logic steps for advanced use cases. Unlike predefined chains, custom chains offer complete control over data flow and task execution.

  • We can define how data moves between steps and what logic applies at each stage.
  • Mix multiple LLM calls, Python functions and API integrations for added flexibility.
  • Add branching logic to make decisions dynamically based on inputs or results.
  • Combine LLMs with external tools like databases, APIs or computation modules for intelligent task handling.
👁 user_input
Custom Chain Workflow

Working

The process begins with user input which is processed by the LLM to understand the intent behind the query. The model then decides which tool to use i.e either a built-in tool for general operations or a custom tool for specialized logic.

1. User Input: The system receives input or a query from the user.

2. LLM Processing: The LLM interprets the intent and context of the request.

3. Tool Selection:

  • Built-in Tool: Used for standard operations like data retrieval or text summarization.
  • Custom Tool: Used for domain-specific or task-specific logic that requires custom computation or transformation.

4. Execution: The selected tool performs the required operation and sends the processed data back.

5. Output Generation: The final, processed information is returned as output by completing an adaptive and intelligent workflow.

Implementing Custom Chains

Lets see step by step how we can create custom chains in langchain.

1. Python class wrapping built chains

  • Here we set the Google API key for authentication and initialize the Gemini 2.5 Flash model with a temperature of 0.7 for balanced creativity.
  • Two PromptTemplates are defined one to generate a paragraph and another to summarize it.
  • Each prompt is wrapped in an LLMChain for modular and reusable execution.
  • A CustomChain combines both using SimpleSequentialChain, linking outputs and inputs.
  • When executed, it generates and then summarizes content, showing smooth task chaining with LLMs.

Output:

Artificial Intelligence (AI) is a rapidly evolving field creating machines capable of human-like intelligence, profoundly transforming modern life and prompting critical discussions about its future.

2. Subclassing Chain (or overriding its internal methods)

  • Here we create a workflow that summarizes and then translates text into Hindi.
  • Two PromptTemplates are defined i.e one for summarization and another for translation.
  • The SummarizeTranslateChain class controls data flow between both steps.
  • input_keys and output_keys handle input-output mapping.
  • When executed, it summarizes English text and translates it into Hindi using a custom chain.

Output:

कृत्रिम बुद्धिमत्ता मशीनों को सीखने, बुद्धिमत्तापूर्ण निर्णय लेने और विभिन्न कार्य करने में सक्षम बनाती है, जो मानवीय आवश्यकताओं के लिए अधिक स्मार्ट तथा अधिक अनुकूलनीय तकनीक बनाने हेतु लगातार विकसित होती रहती है।

3. Custom Step with Runnable

  • Here we create a number guessing game workflow.
  • Two PromptTemplates are defined one for guessing and one for revealing the number.
  • A RunnableSequence connects both steps for data flow.
  • Each step uses the pipe (|) operator to link the PromptTemplate with the LLM.
  • When executed, it generates a random number and the model guesses it, showing custom step execution with Runnable.

Output:

The actual number: 7
Final Output: Okay, the player guessed 6.
What was the actual number? I need that information to tell you if the guess was correct or how close it was!

4. Dynamic (self-constructing) chains

  • Here we build a Dynamic Math Quiz using LangChain and Gemini 2.5 Flash.
  • Two PromptTemplates are created one for checking the user’s answer and another for generating the next question.
  • Each question and check chain is built dynamically at runtime based on the player’s progress.
  • The quiz adapts level by level, generating new challenges when the user answers correctly.
  • This demonstrates dynamic chaining, where new LLM chains are constructed and executed on-the-fly instead of being predefined.

5. Testing

Output:

👁 Screenshot-2025-11-04-182924
Output

Applications

  • Multi-Step Automation: Automate sequential NLP tasks like summarization, translation, sentiment analysis using custom logic.
  • Dynamic Workflow Creation: Build adaptive pipelines that modify steps based on user input or data type like text, code, resume, etc.
  • Domain-Specific Assistants: Create specialized AI assistants like HR resume screener, job review analyzer or salary insight generator.
  • Custom Decision Flows: Implement branching logic where each output determines the next step which is ideal for chatbots, feedback analyzers or content reviewers.
Comment

Explore