Build Your First Chatbot with LangChain + OpenAI

LLMs (Large Language Models) are transforming how we build tools, services, and developer experiences. And there’s no better way to dive in than by creating your own chatbot using LangChain and OpenAI.
We’ve officially kicked off our LLM Learning Series to help developers get hands-on with AI and build useful, real-world tools. 🚀
In this post, we’ll walk you through how to:
✅ Set up a LangChain project
✅ Connect to OpenAI’s API
✅ Read keys securely
✅ Build your first AI-powered chatbot
Let’s get started! 💬
📦 Step 1: Project Setup
First, create a new Python project:
mkdir langchain-chatbot
cd langchain-chatbot
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
Install the necessary dependencies:
pip install langchain openai python-dotenv
🔐 Step 2: Store & Read API Keys
You’ll need an OpenAI API key.
Create a .env
file:
OPENAI_API_KEY=your_openai_key_here
Then read the key securely in your Python script:
from dotenv import load_dotenv
import os
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
🤖 Step 3: Connect to the OpenAI Model via LangChain
LangChain makes it easy to interface with LLMs. Here’s how to connect your code to OpenAI:
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key=openai_api_key)
This gives you access to the llm()
function, which can now generate responses based on prompts.
💬 Step 4: Build Your First Chatbot
Here’s a basic chatbot loop that lets you have a simple conversation in your terminal:
def chatbot():
print("🤖 Hello! Ask me anything (type 'exit' to quit)")
while True:
user_input = input("You: ")
if user_input.lower() == 'exit':
print("Bot: Goodbye!")
break
response = llm(user_input)
print(f"Bot: {response}")
chatbot()
Now run your script:
python chatbot.py
🎉 You’ve just created your first AI chatbot using LangChain + OpenAI!
🧠 What You’ve Learned So Far
By completing this short project, you now understand:
How to set up a basic Python AI project
How to securely read and use API keys
How to use LangChain to connect with OpenAI models
How to create a chatbot with minimal code
🔜 What’s Next in the Series?
Coming up next, we’ll explore:
➡️ Prompt engineering techniques
➡️ Memory in LangChain (so your chatbot remembers context)
➡️ Connecting LangChain with external tools, APIs, and files
➡️ Deploying your chatbot as a web app
Follow the tag #LLMLearningSeries or subscribe to stay updated!
Let’s keep learning and building with AI — the possibilities are just getting started.
📣 Want to collaborate or have questions?
DM me on Twitter/Instagram or comment below. Let’s connect and build the future of dev tools together!
Subscribe to my newsletter
Read articles from sunny g directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

sunny g
sunny g
I am a full-stack developer with 8+ years of experience, passionate about the JavaScript ecosystem. I have a bachelor's degree in computer science. I am most skilled and passionate about Angular and React. I am able to provide meaningful contributions to the design, installation, testing, and maintenance of any type of software system. I like to challenge myself in new roles. I have built and successfully delivered applications in multiple domains. In my free time, I like to write blogs related to software development. I have the pleasure of working on exciting projects across industries. The applications that I developed were scalable, deployable, and maintainable. I have a vision of providing cutting-edge web solutions and services to enterprises. Developed zero-to-one products.