LangChain agents help programs think and act by using tools and language models together. They make AI smarter by letting it decide what to do next.
LangChain agents overview in Agentic AI
Start learning this pattern below
Jump into concepts and practice - no test required
from langchain.agents import initialize_agent from langchain.llms import OpenAI from langchain.tools import Tool # Define tools tools = [Tool(name="Calculator", func=calculator_function, description="Performs math calculations")] # Initialize language model llm = OpenAI(temperature=0) # Create agent agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) # Run agent agent.run("What is 12 times 15?")
The initialize_agent function sets up the agent with tools and a language model.
Agents can use different strategies like 'zero-shot-react-description' to decide actions.
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) agent.run("Calculate 5 plus 7")
agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True) agent.run("What is the weather in New York?")
This program creates a LangChain agent that uses a calculator tool to answer a math question. It runs the agent to calculate 12 times 15 and prints the answer.
from langchain.agents import initialize_agent from langchain.llms import OpenAI from langchain.tools import Tool import math # Define a simple calculator function def calculator_function(query: str) -> str: try: # Evaluate math expression safely result = str(eval(query, {"__builtins__": None}, {})) except Exception: result = "Error in calculation" return result # Create a calculator tool calculator_tool = Tool(name="Calculator", func=calculator_function, description="Performs math calculations") # Initialize language model llm = OpenAI(temperature=0) # Initialize agent with the calculator tool agent = initialize_agent([calculator_tool], llm, agent="zero-shot-react-description", verbose=False) # Run agent to calculate 12 times 15 output = agent.run("What is 12 times 15?") print(output)
Agents combine language models and tools to solve tasks step-by-step.
Choosing the right agent type affects how the AI decides what to do next.
Always test tools separately to ensure they work before adding to agents.
LangChain agents let AI use tools and language models together.
They help AI decide actions based on questions or tasks.
Agents make AI more flexible and powerful for real-world problems.
Practice
Solution
Step 1: Understand LangChain agents' role
LangChain agents help AI decide actions by choosing tools or language models based on the task.Step 2: Compare options with this role
Only To help AI decide which tools to use for a task matches this purpose; others describe unrelated tasks.Final Answer:
To help AI decide which tools to use for a task -> Option AQuick Check:
Agent purpose = Decide tools [OK]
- Confusing agents with data storage systems
- Thinking agents speed up training
- Assuming agents create reports
Solution
Step 1: Recall LangChain agent creation syntax
LangChain agents are created by calling Agent with named parameters like llm= and tools=.Step 2: Check each option's syntax
agent = Agent(llm=llm, tools=tools) uses named parameters correctly; others use incorrect or non-existent methods.Final Answer:
agent = Agent(llm=llm, tools=tools) -> Option BQuick Check:
Correct syntax uses named parameters [OK]
- Omitting parameter names
- Using non-existent create methods
- Confusing function names
from langchain.agents import Agent
llm = MockLLM(responses=["Answer 1"])
tools = [Tool(name="search", func=lambda x: "found info")]
agent = Agent(llm=llm, tools=tools)
result = agent.run("Find info about AI")
print(result)Solution
Step 1: Understand the MockLLM and tools setup
The MockLLM is set to respond with "Answer 1" regardless of input; tools have a function but agent uses LLM response first.Step 2: Analyze agent.run behavior
Agent calls LLM which returns "Answer 1"; tools are available but not triggered to override LLM output.Final Answer:
"Answer 1" -> Option CQuick Check:
LLM response = "Answer 1" [OK]
- Assuming tool output replaces LLM output
- Confusing input with output
- Expecting runtime errors without cause
from langchain.agents import Agent
llm = SomeLLM()
tools = [Tool(name="calc", func=calculate)]
agent = Agent(llm, tools)
result = agent.run("Calculate 2+2")
print(result)Solution
Step 1: Check Agent constructor usage
Agent requires named parameters like llm= and tools=; code uses positional arguments incorrectly.Step 2: Verify other parts
Assuming 'calculate' is defined and LLM imported, the main error is constructor call.Final Answer:
Agent constructor missing named parameters -> Option DQuick Check:
Constructor needs llm= and tools= [OK]
- Using positional arguments for Agent
- Assuming undefined functions cause error here
- Thinking run() needs extra args
Solution
Step 1: Understand agent tool selection
LangChain agents can automatically decide which tool to use when given multiple tools and an appropriate agent type.Step 2: Evaluate options for flexibility and automation
Provide both tools and use an agent type that decides tool usage automatically uses this automatic decision feature; others require manual or less efficient approaches.Final Answer:
Provide both tools and use an agent type that decides tool usage automatically -> Option AQuick Check:
Agent auto-selects tools = Provide both tools and use an agent type that decides tool usage automatically [OK]
- Manually calling tools defeats agent purpose
- Using only one tool limits flexibility
- Training separate agents adds complexity
