Bird
Raised Fist0
Agentic AIml~10 mins

LangChain agents overview in Agentic AI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic LangChain agent using an LLM.

Agentic AI
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
agent = initialize_agent(llm, tools=[], agent= [1] )
Drag options to blanks, or click blank then click option'
A"toolkit-agent"
B"chat-completion"
C"simple-agent"
D"zero-shot-react-description"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid or unsupported agent type string.
Passing the agent type without quotes.
2fill in blank
medium

Complete the code to add a tool to the LangChain agent.

Agentic AI
from langchain.agents import Tool

tool = Tool(name="Search", func=lambda x: "Searching..." + x, description="Search tool")
agent = initialize_agent(llm, tools=[[1]], agent="zero-shot-react-description")
Drag options to blanks, or click blank then click option'
Atool
Btools
Csearch_tool
DSearch
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string name of the tool instead of the Tool object.
Passing an undefined variable.
3fill in blank
hard

Fix the error in the code to run the LangChain agent on an input.

Agentic AI
response = agent.run([1])
print(response)
Drag options to blanks, or click blank then click option'
Ainput("What is the weather today?")
BWhat is the weather today?
C"What is the weather today?"
Dagent("What is the weather today?")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the question without quotes causing syntax errors.
Using input() function inside run() which is not needed here.
4fill in blank
hard

Fill both blanks to create a custom tool and add it to the agent.

Agentic AI
def custom_func(input_text):
    return f"Processed: {input_text}"

custom_tool = Tool(name=[1], func=[2], description="Custom processing tool")
agent = initialize_agent(llm, tools=[custom_tool], agent="zero-shot-react-description")
Drag options to blanks, or click blank then click option'
A"CustomTool"
Bcustom_func
C"custom_func"
DcustomTool
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name as a string instead of a function object.
Not quoting the tool name string.
5fill in blank
hard

Fill all three blanks to create an agent that uses multiple tools and runs a query.

Agentic AI
tool1 = Tool(name=[1], func=lambda x: f"Tool1: {x}", description="First tool")
tool2 = Tool(name=[2], func=lambda x: f"Tool2: {x}", description="Second tool")
agent = initialize_agent(llm, tools=[tool1, tool2], agent="zero-shot-react-description")
response = agent.run([3])
print(response)
Drag options to blanks, or click blank then click option'
A"ToolOne"
B"ToolTwo"
C"How do I use these tools?"
D"Tool1"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both tools causing confusion.
Passing the query without quotes.

Practice

(1/5)
1. What is the main purpose of LangChain agents in AI?
easy
A. To help AI decide which tools to use for a task
B. To store large amounts of data efficiently
C. To train AI models faster using GPUs
D. To create static reports from data

Solution

  1. Step 1: Understand LangChain agents' role

    LangChain agents help AI decide actions by choosing tools or language models based on the task.
  2. 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.
  3. Final Answer:

    To help AI decide which tools to use for a task -> Option A
  4. Quick Check:

    Agent purpose = Decide tools [OK]
Hint: Agents decide actions and tools for AI tasks [OK]
Common Mistakes:
  • Confusing agents with data storage systems
  • Thinking agents speed up training
  • Assuming agents create reports
2. Which of the following is the correct way to create a simple LangChain agent in Python?
easy
A. agent = Agent(llm, tools)
B. agent = Agent(llm=llm, tools=tools)
C. agent = Agent.create(llm, tools)
D. agent = create_agent(llm, tools)

Solution

  1. Step 1: Recall LangChain agent creation syntax

    LangChain agents are created by calling Agent with named parameters like llm= and tools=.
  2. Step 2: Check each option's syntax

    agent = Agent(llm=llm, tools=tools) uses named parameters correctly; others use incorrect or non-existent methods.
  3. Final Answer:

    agent = Agent(llm=llm, tools=tools) -> Option B
  4. Quick Check:

    Correct syntax uses named parameters [OK]
Hint: Use named parameters llm= and tools= to create agents [OK]
Common Mistakes:
  • Omitting parameter names
  • Using non-existent create methods
  • Confusing function names
3. Given this code snippet, what will be the output?
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)
medium
A. Error: Missing tool function
B. "found info"
C. "Answer 1"
D. "Find info about AI"

Solution

  1. 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.
  2. Step 2: Analyze agent.run behavior

    Agent calls LLM which returns "Answer 1"; tools are available but not triggered to override LLM output.
  3. Final Answer:

    "Answer 1" -> Option C
  4. Quick Check:

    LLM response = "Answer 1" [OK]
Hint: MockLLM returns preset answer, tools don't override by default [OK]
Common Mistakes:
  • Assuming tool output replaces LLM output
  • Confusing input with output
  • Expecting runtime errors without cause
4. What is wrong with this LangChain agent code?
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)
medium
A. Tool function 'calculate' is undefined
B. LLM instance is not imported
C. Agent.run() requires extra arguments
D. Agent constructor missing named parameters

Solution

  1. Step 1: Check Agent constructor usage

    Agent requires named parameters like llm= and tools=; code uses positional arguments incorrectly.
  2. Step 2: Verify other parts

    Assuming 'calculate' is defined and LLM imported, the main error is constructor call.
  3. Final Answer:

    Agent constructor missing named parameters -> Option D
  4. Quick Check:

    Constructor needs llm= and tools= [OK]
Hint: Always use named parameters when creating Agent [OK]
Common Mistakes:
  • Using positional arguments for Agent
  • Assuming undefined functions cause error here
  • Thinking run() needs extra args
5. You want to build a LangChain agent that uses both a calculator tool and a web search tool. Which approach best ensures the agent chooses the right tool based on the question?
hard
A. Provide both tools and use an agent type that decides tool usage automatically
B. Manually call each tool in sequence and combine results
C. Use only one tool at a time to avoid confusion
D. Train separate agents for each tool and merge outputs later

Solution

  1. Step 1: Understand agent tool selection

    LangChain agents can automatically decide which tool to use when given multiple tools and an appropriate agent type.
  2. 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.
  3. Final Answer:

    Provide both tools and use an agent type that decides tool usage automatically -> Option A
  4. Quick Check:

    Agent auto-selects tools = Provide both tools and use an agent type that decides tool usage automatically [OK]
Hint: Use agent types that pick tools automatically [OK]
Common Mistakes:
  • Manually calling tools defeats agent purpose
  • Using only one tool limits flexibility
  • Training separate agents adds complexity