Build Simple LLM Agents with LangChain: A Hands‑On Tutorial

This guide explains what AI agents are, how they combine large language models with planning, memory, and tool use, and provides a step‑by‑step LangChain implementation—including environment setup, tool integration, and a runnable example that solves math and performs web searches.

JavaEdge
JavaEdge
JavaEdge
Build Simple LLM Agents with LangChain: A Hands‑On Tutorial

0 Introduction

No need for separate software for different tasks

Command devices using everyday language

Agents are an advanced form of artificial intelligence

Will become reality within five years

Everyone will have a personal assistant Agent

Applicable across industries such as healthcare, education, entertainment, etc.

1 What Are Agents?

AI Agents are LLM‑based autonomous entities that can understand, plan, decide, and execute complex tasks. Unlike a simple ChatGPT response, an Agent not only tells you how to do something but actually performs the action.

Agents = LLM + planning skills + memory + tool usage

Essentially, an Agent is an orchestration and execution system for LLMs.

A simplified decision loop for an Agent processes one task at a time:

2 How LangChain Implements Agents

State the requirement or question

Combine problem with a Prompt

Enter the ReAct loop

Search Memory

Find usable tools

Execute the tool and observe the result

Repeat steps 1‑6 if necessary

Obtain the final answer

3 The Simplest Agent Implementation

3.0 Requirements

Ability to solve math problems

When the answer is unknown, perform a web search

3.1 Install Dependencies

!pip install langchain==0.2.1  # LangChain core
!pip install langchain-community==0.2.1  # Third‑party integrations
!pip install python-dotenv==1.0.1  # Manage .env files
!pip install dashscope==1.19.2  # Model library

Create a .env file and store your API key there.

import os
from dotenv import find_dotenv, load_dotenv
from langchain_community.llms import Tongyi
from langchain_core.runnables import RunnableSequence
from langchain.prompts import PromptTemplate

load_dotenv(find_dotenv())
DASHSCOPE_API_KEY = os.environ["DASHSCOPE_API_KEY"]
# Define the LLM
llm = QwenTurboTongyi(temperature=1)

3.2 Set Up Tools

serpapi

– an aggregated search engine; requires the Google Search package and an API key (https://serpapi.com/manage-api-key) llm-math – a ready‑made math computation chain

# Install Google Search package
! pip install google-search-results
import os
os.environ["SERPAPI_API_KEY"] = "XXXX"

The value of SERPAPI_API_KEY is the free API key you obtained after registration.

from langchain.agents import load_tools
tools = load_tools(["serpapi", "llm-math"], llm=llm)

3.3 Define the Agent

Use a few‑shot prompt to enhance generation.

from langchain.agents import initialize_agent, AgentType

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,  # various types available
    verbose=True,  # print logs
)
agent.run("请问现任的美国总统是谁?他的年龄的平方是多少?")

Conclusion

This minimal example demonstrates how to assemble a functional LLM Agent with LangChain: install the required packages, configure API keys, load search and math tools, initialize a zero‑shot ReAct agent, and run a natural‑language query that combines knowledge retrieval and computation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonLLMtool integrationLangChainTutorial
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.