0
0
LangChainframework~10 mins

Custom agent logic in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom agent logic
Start: Receive user input
Agent parses input
Check if input matches custom logic
Run custom logic
Generate custom response
Return response
The agent receives input, decides if custom logic applies, runs it or defaults, then returns the response.
Execution Sample
LangChain
from langchain.agents import Agent

class CustomAgent(Agent):
    def plan(self, input):
        if "hello" in input.lower():
            return "Say hi back"
        return super().plan(input)
This code defines a custom agent that replies specially when input contains 'hello', otherwise uses default logic.
Execution Table
StepInputCondition CheckedBranch TakenActionOutput
1"Hello, agent!""hello" in input.lower()?YesReturn custom response 'Say hi back'"Say hi back"
2"What is the time?""hello" in input.lower()?NoCall default agent plan method"Default response"
3N/AN/AN/AEnd of processingN/A
💡 Input processed by either custom or default logic, then response returned.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
inputNone"Hello, agent!""What is the time?"N/A
conditionFalseTrueFalseN/A
responseNone"Say hi back""Default response"N/A
Key Moments - 2 Insights
Why does the agent return a custom response only when 'hello' is in the input?
Because the condition '"hello" in input.lower()' is checked at each input (see execution_table step 1 and 2). If true, custom logic runs; otherwise, default logic runs.
What happens if the input does not match the custom condition?
The agent calls the default plan method (see execution_table step 2), so it behaves like a normal agent without custom logic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when input is 'Hello, agent!'?
A"Say hi back"
B"Default response"
C"Hello, agent!"
DNo output
💡 Hint
Check step 1 output column in the execution_table.
At which step does the agent decide to use default logic?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Branch Taken' column for step 2 in the execution_table.
If the condition checked was changed to check for 'bye' instead of 'hello', what would happen at step 1?
AAgent crashes
BCustom logic runs for input 'Hello, agent!'
CDefault logic runs for input 'Hello, agent!'
DNo response
💡 Hint
Refer to the condition in variable_tracker and execution_table step 1.
Concept Snapshot
Custom agent logic lets you add your own rules to decide how the agent responds.
Check input, run your code if condition matches, else use default.
Override methods like plan() to insert custom behavior.
Always return a response string.
This helps tailor agent answers to special cases.
Full Transcript
Custom agent logic in Langchain means you write your own rules inside the agent to handle inputs differently. The agent first gets the input, then checks if it matches your custom condition. If yes, it runs your special code and returns a custom response. If not, it falls back to the normal agent behavior. This way, you can make the agent say or do exactly what you want for certain inputs. The example code shows overriding the plan method to check if the input contains 'hello'. If it does, it returns a special message. Otherwise, it calls the original plan method. The execution table traces this decision for two inputs: one with 'hello' and one without. Variables like input, condition, and response change as the agent processes each input. This helps beginners see how the agent chooses which logic to run and what output it produces. The quizzes test understanding of when custom logic runs and what outputs appear. The snapshot summarizes the key idea: check input, run custom code if needed, else default. This approach makes agents flexible and easy to customize.