Setting Up LangChain
Let us get your computer ready.
You need Python 3.9 or newer, a folder for your project, and a few packages.
Step 1: Make a project folder
A virtual environment keeps this project's packages separate from everything else.
Think of it as a clean lunchbox just for this project.
Step 2: Install LangChain
LangChain is split into small packages.
You install the core plus one package per provider you want to talk to.
langchain: the main toolkit and the init_chat_model helper
langchain-openai: talks to OpenAI models like gpt-4o-mini
langchain-ollama: talks to free local models
langchain-community: loaders and tools made by the community
langgraph: for agents and bigger workflows
Step 3: Get an API key
Cloud models need a key. It is like a password that also tracks your bill.
Go to platform.openai.com, sign in, and create a key. It starts with sk-.
Never paste the key into your code. Never push it to GitHub.
Step 4: Keep the key in a .env file
Make a file called .env in your project folder.
Then load it at the top of your Python file.
Add .env to your .gitignore so it never leaves your computer.
Step 5: Check that everything works
If you see a friendly hello, you are ready.
No key? Use a free local model
Install Ollama from ollama.com, then pull a small model.
Everything else in this course works the same with either model.
Common problems
ModuleNotFoundError: your virtual environment is not active. Run the activate command again.
AuthenticationError: the key is missing or wrong. Check the .env file and that load_dotenv() runs first.
RateLimitError: you sent too many requests, or your account has no credit.
Tip: Keep one small test file like check.py. Whenever something breaks, run it first to see if the basics still work.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.