0
0
LangChainframework~5 mins

Why agents add autonomy to LLM apps in LangChain

Choose your learning style9 modes available
Introduction

Agents help large language model (LLM) apps act on their own. They let the app decide what to do next without waiting for you.

When you want the app to handle multiple tasks by itself.
When the app needs to choose the best tool or action automatically.
When you want the app to gather information from different places without manual steps.
When you want the app to solve problems step-by-step on its own.
When you want to build smarter assistants that can plan and act independently.
Syntax
LangChain
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.tools import Tool

llm = OpenAI(temperature=0)
tools = [Tool(name="Search", func=search_function, description="Useful for web search")]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

The initialize_agent function creates an agent that can pick tools and decide actions.

You provide tools and an LLM, and the agent uses them to work autonomously.

Examples
The agent uses the search tool and LLM to answer the question by itself.
LangChain
agent.run("Find the latest news about space exploration.")
The agent can decide to use a calculator tool or do the math itself.
LangChain
agent.run("Calculate 15% tip for a $45 bill.")
Sample Program

This example shows an agent that can add two numbers by choosing the Adder tool on its own.

LangChain
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
from langchain.tools import Tool

# Simple tool to add two numbers
def add_numbers(inputs: str) -> str:
    try:
        a, b = map(int, inputs.split())
        return str(a + b)
    except Exception:
        return "Error: provide two numbers separated by space"

llm = OpenAI(temperature=0)
tools = [Tool(name="Adder", func=add_numbers, description="Adds two numbers")]

agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False)

# Agent decides to use the Adder tool
result = agent.run("Add 7 5")
print(result)
OutputSuccess
Important Notes

Agents let your app think and act without you telling every step.

They combine language understanding with tools to solve tasks better.

Make sure your tools have clear descriptions so the agent picks the right one.

Summary

Agents add independence to LLM apps by choosing actions automatically.

They help apps handle complex tasks by using tools and reasoning.

Using agents makes your app smarter and more helpful without extra coding for each step.