Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Custom Agent Logic with LangChain
📖 Scenario: You are building a simple AI assistant using LangChain. This assistant will decide how to respond based on the user's input by using custom agent logic.
🎯 Goal: Create a LangChain agent with custom logic that chooses between two tools based on the user's question.
📋 What You'll Learn
Create a dictionary called tools with two tools: 'calculator' and 'search'
Create a variable called agent_config with a key use_search set to true
Write a function called custom_agent_logic that takes input_text and returns 'search' if agent_config['use_search'] is true and the input contains the word 'weather', otherwise returns 'calculator'
Create an agent instance called agent that uses custom_agent_logic to select the tool
💡 Why This Matters
🌍 Real World
Custom agents let you build AI assistants that decide how to respond based on the question, like choosing between searching the web or doing calculations.
💼 Career
Understanding custom agent logic is useful for AI developers building smart assistants, chatbots, or automation tools that need flexible decision-making.
Progress0 / 4 steps
1
Create the tools dictionary
Create a dictionary called tools with two entries: 'calculator' mapped to CalculatorTool() and 'search' mapped to SearchTool().
LangChain
Hint
Use a Python dictionary with keys 'calculator' and 'search' and assign the respective tool instances.
2
Add agent configuration
Create a dictionary called agent_config with a key 'use_search' set to true.
LangChain
Hint
Define a dictionary named agent_config with the key 'use_search' set to true.
3
Write the custom agent logic function
Write a function called custom_agent_logic that takes a parameter input_text. The function should return 'search' if agent_config['use_search'] is true and the string 'weather' is in input_text (case insensitive). Otherwise, return 'calculator'.
LangChain
Hint
Use input_text.lower() to check for 'weather' ignoring case. Use an if-else statement to return the correct tool name.
4
Create the agent with custom logic
Create a variable called agent and assign it an instance of CustomAgent initialized with tools and custom_agent_logic as the logic function.
LangChain
Hint
Instantiate CustomAgent with the tools dictionary and the custom_agent_logic function.
Practice
(1/5)
1. What is the main purpose of creating custom agent logic in Langchain?
easy
A. To define specific rules for how the agent plans and acts
B. To change the user interface of the agent
C. To improve the speed of the Langchain library
D. To add new data sources automatically
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 A
Quick Check:
Custom agent logic = planning and acting rules [OK]
Hint: Custom logic controls agent decisions, not UI or speed [OK]
Common Mistakes:
Thinking it changes the user interface
Assuming it speeds up the library
Believing it adds data sources automatically
2. Which method should you override to customize how an agent decides its next action in Langchain?
easy
A. run
B. plan
C. initialize
D. execute
Solution
Step 1: Identify key methods for custom logic
Langchain agents use methods like plan and run for behavior.
Step 2: Determine which controls decision making
The plan method decides the next action, so overriding it customizes decisions.
Final Answer:
plan -> Option B
Quick Check:
Decision method = plan [OK]
Hint: Override plan() to change agent's next action [OK]
Common Mistakes:
Overriding run() instead of plan() for decisions
Using initialize() which is not for planning
Confusing execute() with plan()
3. Given this custom agent code snippet, what will be printed?
class MyAgent:
def plan(self, input_text):
if 'hello' in input_text.lower():
return 'Greet'
return 'Ignore'
agent = MyAgent()
print(agent.plan('Hello there!'))
medium
A. Greet
B. Ignore
C. hello
D. Error
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 A
Quick Check:
Input contains 'hello' -> returns 'Greet' [OK]
Hint: Check string contains 'hello' ignoring case [OK]
Common Mistakes:
Ignoring case sensitivity in string check
Expecting the method to print input text
Assuming method returns 'Ignore'
4. What is wrong with this custom agent code?
class CustomAgent:
def plan(self, input_text):
if input_text.contains('test'):
return 'Found'
return 'Not Found'
medium
A. Missing return statement in plan method
B. plan method should not take input_text parameter
C. Indentation error in class definition
D. Using .contains() method which does not exist in Python strings
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 D
Quick Check:
Python strings use 'in', not .contains() [OK]
Hint: Use 'in' keyword for substring check in Python [OK]
Common Mistakes:
Using .contains() instead of 'in'
Thinking plan method can't take parameters
Assuming missing return statement
5. You want to create a custom Langchain agent that first plans actions based on input, then logs each action before running it. Which approach correctly combines planning and running with logging?
hard
A. Override only run() to plan, log, and execute actions
B. Override plan() to log actions and run() to decide actions
C. Override plan() to decide actions and override run() to log and execute actions
D. Use default plan() and run(), add logging outside the agent
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 C
Quick Check:
plan() decides, run() logs and executes [OK]
Hint: Plan decides; run executes and logs actions [OK]