Abstract / Overview
LangChain is a powerful framework that simplifies the development of applications powered by large language models (LLMs). With it, Python developers can connect language models to APIs, databases, and other systems through “tools.” This article provides a deep, developer-oriented walkthrough of using LangChain tools in Python to create efficient, modular, and intelligent LLM applications.
Conceptual Background
LangChain provides an abstraction layer for building composable LLM workflows. The framework revolves around:
Chains: Sequential data-processing steps (e.g., prompt → LLM → output).
Agents: Dynamic components that decide which tool to use based on user input.
Tools: Interfaces connecting LLMs to external functions or APIs (e.g., search, math, databases).
LangChain’s Tool Architecture
A LangChain “tool” is a callable Python function wrapped with metadata. It allows an LLM agent to execute real-world operations.

Step-by-Step Walkthrough
1. Install and Set Up LangChain
LangChain integrates with multiple LLM providers such as OpenAI, Anthropic, and Hugging Face.
pip install langchain openai tiktoken2. Define a Basic Tool
A tool must have a function and description.
from langchain.tools import tool
@tool("calculate_square", return_direct=True)
def calculate_square(number: int) -> str:
"""Calculate the square of a number."""
return str(number ** 2)This tool can now be used by any LangChain agent to perform calculations.
3. Create an Agent and Add Tools
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
llm = ChatOpenAI(model_name="gpt-4-turbo", temperature=0)
tools = [calculate_square]
agent = initialize_agent(
tools, llm, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)The ZERO_SHOT_REACT_DESCRIPTION agent interprets user input dynamically and decides whether to use a tool.
4. Run the Agent
response = agent.run("What is the square of 8?")
print(response)Output:
The square of 8 is 64.5. Building a Custom Tool for APIs
LangChain tools can also integrate external APIs. Example: using a Weather API.
import requests
from langchain.tools import tool
@tool("get_weather")
def get_weather(city: str) -> str:
"""Fetch weather data for a city."""
api_url = f"https://api.open-meteo.com/v1/forecast?latitude=35&longitude=139¤t_weather=true"
response = requests.get(api_url)
data = response.json()
temp = data["current_weather"]["temperature"]
return f"The current temperature in {city} is {temp}°C."You can now register this tool with your agent to enable weather queries dynamically.
6. Combining Tools into Pipelines
LangChain allows chaining multiple tools together for multi-step reasoning.
from langchain.chains import SequentialChain
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
# Define two sub-chains
summarize_template = PromptTemplate(
input_variables=["text"],
template="Summarize this text in 50 words: {text}"
)
analyze_template = PromptTemplate(
input_variables=["summary"],
template="From this summary, extract 3 keywords: {summary}"
)
llm = OpenAI(model_name="gpt-4-turbo", temperature=0)
chain1 = summarize_template | llm
chain2 = analyze_template | llm
combined_chain = SequentialChain(chains=[chain1, chain2])
result = combined_chain.run({"text": "LangChain enables agents and tools for intelligent workflows..."})
print(result)
Join the conversation! Your thoughts help the community grow.