Build Your First LLM Tool: A Beginner’s Guide to LangChain
Building an LLM application can sound complicated, but your first working tool may require fewer than 15 lines of Python.
In this tutorial, we’ll build a simple AI explainer using LangChain and an OpenAI model.
Why Use LangChain Instead of Calling the OpenAI API Directly?
Calling an AI model directly is perfectly reasonable for a small script. You send a prompt, receive a response, and display it.
The complexity begins when your application needs:
- Reusable prompt templates
- Structured output
- Multiple model providers
- Documents or external data
- Conversation history
- Tools and agents
- Logging and debugging
LangChain provides a standard interface for composing these pieces. It also makes switching between supported providers easier because your application does not need to be completely rewritten around each provider’s API. (Docs by LangChain)
Think of the raw API as an engine. LangChain gives you reusable parts for building the rest of the vehicle.
Install the Required Packages
pip install langchain langchain-openai python-dotenv
Create a .env file:
OPENAI_API_KEY=your_api_key_here
Never commit this file or expose your API key in a public repository. OpenAI recommends storing API keys securely rather than placing them directly inside source code. (OpenAI Platform)
Build a Basic LangChain Chain
Create a file named app.py:
from dotenv import load_dotenv
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
load_dotenv()
prompt = ChatPromptTemplate.from_template(
"Explain {topic} to a beginner using one example."
)
model = ChatOpenAI(model="gpt-5-nano")
chain = prompt | model | StrOutputParser()
print(chain.invoke({"topic": "vector databases"}))
Run it:
python app.py
The chain has three simple steps:
- Insert the topic into the prompt.
- Send the formatted prompt to the model.
- Convert the model response into a normal Python string.
The | operator connects each component into a readable pipeline. The current LangChain OpenAI integration is provided through the separate langchain-openai package and its ChatOpenAI class. (Docs by LangChain)
Where Should You Go Next?
Once this basic chain works, explore two important areas.
RAG, or Retrieval-Augmented Generation, lets your application retrieve information from PDFs, webpages, databases, or company documents before generating an answer.
Agents allow a model to decide when to call tools such as search functions, APIs, calculators, or custom Python functions. LangChain currently provides configurable agent-building functionality for connecting models with tools and application logic. (Docs by LangChain)
You can also turn your script into a web interface using Gradio and publish it through Hugging Face Spaces.
Here are two live examples from my own AI-tool experiments:
Start with one prompt and one model. Get that working first. Then add memory, retrieval, tools, and agents only when your application genuinely needs them.
About the author: Piyush Kumar Soni is an AI Developer, LLM Expert and Digital Growth Specialist building practical AI tools, automation systems and LLM-powered workflows.
Suggested Dev.to tags: ai, langchain, python, beginners
This article follows the Dev.to and topical-authority direction in your SERP plan, including the focus on LangChain development, Python-based AI tools and Hugging Face demos. Your supplied profile list confirms the two live Hugging Face Space URLs used above.
Top comments (0)