𝗦𝗲𝘁𝘁𝗶𝗻𝗴 𝗨𝗽 𝗬𝗼𝘂𝗿 𝗘𝗻𝘃𝗶𝗿𝗼𝗻𝗺𝗲𝗻𝘁: 𝗟𝗮𝗻𝗴𝗖𝗵𝗮𝗶𝗻, 𝗟𝗮𝗻𝗴𝗚𝗿𝗮𝗽𝗵 & 𝗖𝗵𝗮𝘁 𝗟𝗟𝗠𝘀
I once spent two hours debugging a LangChain project. The problem was one missing environment variable. My API key was in a .env file, but I forgot to load it.
I wrote this guide so you do not waste time like I did.
You will learn to set up a working Python environment with LangChain, LangGraph, and a Chat LLM.
Required Packages:
- langchain: Core framework for chains and agents
- langchain-openai: Integration with OpenAI models
- langchain-core: Shared primitives like messages
- langgraph: Graph-based agent workflows
- python-dotenv: Loads variables from a .env file
- openai: Official OpenAI Python SDK
Step 1: Check Python Version LangChain needs Python 3.9 or higher. Run this in your terminal: python --version
Step 2: Create a Virtual Environment Always use a virtual environment to avoid dependency conflicts.
mkdir langchain-agents-series cd langchain-agents-series python -m venv venv
To activate on Mac/Linux: source venv/bin/activate
To activate on Windows: venv\Scripts\activate
Step 3: Install Dependencies With your environment active, run: pip install langchain langchain-openai langchain-core langgraph python-dotenv openai
Step 4: Secure Your API Keys Never hardcode your API key in your code. This is dangerous. If you push it to GitHub, bots will steal it.
- Create a .env file: touch .env
- Add your key: OPENAI_API_KEY=your-key-here
- Create a .gitignore file to hide your .env and venv folders.
Step 5: Your First Agent Create a file named hello_agent.py. Use the load_dotenv() function to read your keys.
Use temperature=0 when building agents. This makes the model output predictable and consistent.
Common Errors to Watch For:
- OPENAI_API_KEY not found: Check if your .env file is in the right folder and you called load_dotenv().
- ModuleNotFoundError: Your virtual environment is not active.
- AuthenticationError: Your API key is wrong.
- RateLimitError: You hit your limit or need to add credits to your OpenAI account.
Best Practices:
- Always use a virtual environment.
- Pin your versions with pip freeze > requirements.txt.
- Use temperature=0 for stability.
- Log your token usage to track costs.
Source: https://dev.to/ikram_khan/setting-up-your-environment-langchain-langgraph-chat-llms-43cf
Optional learning community: https://t.me/GyaanSetuAi