Custom agent logic lets you control how an AI agent thinks and acts step-by-step. It helps you make the agent smarter and fit your special needs.
Custom agent logic in LangChain
from langchain.agents import Agent class CustomAgent(Agent): def plan(self, intermediate_steps, **kwargs): # Your custom planning logic here return plan def run(self, input): # Your custom run logic here return output
You create a new class that inherits from Agent.
Override methods like plan or run to change behavior.
class CustomAgent(Agent): def plan(self, intermediate_steps, **kwargs): # Always pick the first tool return {'tool': 'search', 'input': 'example query'}
class CustomAgent(Agent): def run(self, input): # Add a prefix to input before running modified_input = 'Check: ' + input return super().run(modified_input)
This program creates a custom agent that always uses a 'calculator' tool. The tool just reverses the input string as a fake calculation. When run with '12345', it returns '54321'.
class CustomAgent: def plan(self, intermediate_steps, **kwargs): # Simple plan: always use tool 'calculator' with input return {'tool': 'calculator', 'input': kwargs.get('input', '')} def run(self, input): plan = self.plan([], input=input) tool = plan['tool'] tool_input = plan['input'] # Simulate tool execution if tool == 'calculator': # Just return the input reversed as a fake calculation return tool_input[::-1] return 'No tool executed' agent = CustomAgent() result = agent.run('12345') print(result)
Custom agents let you control the agent's thinking steps.
Override only the methods you need to change.
Test your custom logic carefully to avoid infinite loops or errors.
Custom agent logic means writing your own rules for how the agent plans and acts.
This helps make agents fit special tasks or workflows.
It involves subclassing and overriding key methods like plan and run.
Practice
Solution
Step 1: Understand the role of custom agent logic
Custom agent logic is about controlling how the agent decides what to do next.Step 2: Identify the main purpose
It is used to write your own rules for planning and acting, not for UI or speed improvements.Final Answer:
To define specific rules for how the agent plans and acts -> Option AQuick Check:
Custom agent logic = planning and acting rules [OK]
- Thinking it changes the user interface
- Assuming it speeds up the library
- Believing it adds data sources automatically
Solution
Step 1: Identify key methods for custom logic
Langchain agents use methods likeplanandrunfor behavior.Step 2: Determine which controls decision making
Theplanmethod decides the next action, so overriding it customizes decisions.Final Answer:
plan -> Option BQuick Check:
Decision method = plan [OK]
- Overriding run() instead of plan() for decisions
- Using initialize() which is not for planning
- Confusing execute() with plan()
class MyAgent:
def plan(self, input_text):
if 'hello' in input_text.lower():
return 'Greet'
return 'Ignore'
agent = MyAgent()
print(agent.plan('Hello there!'))Solution
Step 1: Analyze the plan method logic
The method checks if 'hello' is in the input text (case-insensitive). If yes, returns 'Greet'.Step 2: Apply input to the method
Input is 'Hello there!', which contains 'hello' ignoring case, so it returns 'Greet'.Final Answer:
Greet -> Option AQuick Check:
Input contains 'hello' -> returns 'Greet' [OK]
- Ignoring case sensitivity in string check
- Expecting the method to print input text
- Assuming method returns 'Ignore'
class CustomAgent:
def plan(self, input_text):
if input_text.contains('test'):
return 'Found'
return 'Not Found'Solution
Step 1: Check string method usage
Python strings do not have a .contains() method; use 'in' keyword instead.Step 2: Identify correct string membership check
Correct way is: 'test' in input_text, so .contains() causes an error.Final Answer:
Using .contains() method which does not exist in Python strings -> Option DQuick Check:
Python strings use 'in', not .contains() [OK]
- Using .contains() instead of 'in'
- Thinking plan method can't take parameters
- Assuming missing return statement
Solution
Step 1: Understand responsibilities of plan() and run()
plan() decides what to do next; run() executes actions.Step 2: Combine logging with correct methods
Override plan() for custom decisions; override run() to add logging before execution.Final Answer:
Override plan() to decide actions and override run() to log and execute actions -> Option CQuick Check:
plan() decides, run() logs and executes [OK]
- Putting planning logic inside run() only
- Logging inside plan() without running actions
- Not overriding methods and logging externally
Ready to prove it?
Pass the test and this pattern moves unknown
