DEV Community

Muhammad Ikramullah Khan
Muhammad Ikramullah Khan

Posted on

Setting Up Your Environment: LangChain, LangGraph & Chat LLMs

The first time I tried to set up a LangChain project, I spent two hours debugging an error that turned out to be a single missing environment variable. My API key was sitting right there in a .env file, but I had forgotten to load it. Two hours. Gone.

I am writing this article so that does not happen to you.

By the end of this guide, you will have a fully working Python environment with LangChain, LangGraph, and a Chat LLM connected and running. We will do it step by step, and I will flag every place where things commonly go wrong.

Let us get into it.


What We Are Installing

Here is everything we need:

Package What It Does
langchain Core framework for building chains and agents
langchain-openai LangChain's integration with OpenAI models
langchain-core Shared primitives (messages, runnables, etc.)
langgraph Graph-based agent workflows
python-dotenv Loads environment variables from a .env file
openai The official OpenAI Python SDK

We will use OpenAI's gpt-4o-mini as our Chat LLM throughout this series. It is fast, affordable, and widely available. If you prefer to use a different provider like Anthropic or Google, I will point out where to swap things out.


Step 1: Check Your Python Version

LangChain requires Python 3.9 or higher. Open your terminal and run:

python --version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

Python 3.11.4
Enter fullscreen mode Exit fullscreen mode

If you are on Python 3.8 or lower, head to python.org and download the latest stable version before continuing.


Step 2: Create a Project Folder

Let us keep things organized from the start:

mkdir langchain-agents-series
cd langchain-agents-series
Enter fullscreen mode Exit fullscreen mode

Everything we build in this series will live inside this folder.


Step 3: Set Up a Virtual Environment

A virtual environment keeps your project's dependencies isolated from the rest of your system. This is not optional. It will save you from dependency conflicts down the road.

# Create the virtual environment
python -m venv venv

# Activate it on Mac/Linux
source venv/bin/activate

# Activate it on Windows
venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Once activated, your terminal prompt will change to show (venv) at the beginning. That means you are inside the virtual environment and any packages you install will stay isolated to this project.


Step 4: Install the Packages

With your virtual environment active, run:

pip install langchain langchain-openai langchain-core langgraph python-dotenv openai
Enter fullscreen mode Exit fullscreen mode

This will take a minute or two. Once done, verify everything installed correctly:

pip list | grep -E "langchain|langgraph|openai"
Enter fullscreen mode Exit fullscreen mode

You should see output similar to:

langchain                0.3.x
langchain-core           0.3.x
langchain-openai         0.2.x
langgraph                0.2.x
openai                   1.x.x
Enter fullscreen mode Exit fullscreen mode

The exact version numbers may differ, but as long as all five packages appear, you are good.


Step 5: Get Your OpenAI API Key

To use GPT models, you need an API key from OpenAI.

  1. Go to platform.openai.com
  2. Sign up or log in
  3. Click your profile icon in the top right, then select API keys
  4. Click Create new secret key
  5. Copy the key immediately. You will not be able to see it again once you close the dialog.

Make sure you have some credits on your account. New accounts usually get a small amount of free credits. For this series, gpt-4o-mini is very affordable and your credits should go a long way.


Step 6: Store Your API Key Safely

Never hardcode your API key directly in your Python files. If you push your code to GitHub with a hardcoded key, it will get scraped and used by bots within minutes.

Instead, we use a .env file.

Create a file called .env in your project root:

touch .env
Enter fullscreen mode Exit fullscreen mode

Open it and add your key:

OPENAI_API_KEY=sk-your-actual-key-goes-here
Enter fullscreen mode Exit fullscreen mode

Now create a .gitignore file to make sure this file never gets committed:

touch .gitignore
Enter fullscreen mode Exit fullscreen mode

Add this to .gitignore:

.env
venv/
__pycache__/
*.pyc
Enter fullscreen mode Exit fullscreen mode

Your project folder should now look like this:

langchain-agents-series/
├── .env
├── .gitignore
└── venv/
Enter fullscreen mode Exit fullscreen mode

Step 7: Your First Chat LLM Call

Now for the fun part. Create a new file called hello_agent.py:

# hello_agent.py

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage

# Load environment variables from .env file
load_dotenv()

# Verify the API key is loaded
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found. Make sure your .env file exists and contains the key.")

# Initialize the Chat LLM
llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0,        # 0 = deterministic, 1 = more creative
    max_tokens=500        # limit response length
)

# Build a list of messages
messages = [
    SystemMessage(content="You are a helpful assistant who explains technical concepts simply."),
    HumanMessage(content="What is LangChain in one sentence?")
]

# Call the model
print("Sending request to OpenAI...")
response = llm.invoke(messages)

# Print the response
print("\nResponse:")
print(response.content)

# Print some metadata
print("\nModel used:", response.response_metadata.get("model_name"))
print("Tokens used:", response.response_metadata.get("token_usage"))
Enter fullscreen mode Exit fullscreen mode

Run it:

python hello_agent.py
Enter fullscreen mode Exit fullscreen mode

If everything is set up correctly, you will see something like:

Sending request to OpenAI...

Response:
LangChain is a Python framework that makes it easy to build applications
powered by large language models by providing tools for chaining prompts,
managing memory, and integrating with external services.

Model used: gpt-4o-mini-2024-07-18
Tokens used: {'completion_tokens': 38, 'prompt_tokens': 35, 'total_tokens': 73}
Enter fullscreen mode Exit fullscreen mode

You just made your first LangChain API call. That is your "Hello, World" moment.


Understanding What Just Happened

Let us break down the key lines:

load_dotenv()
Enter fullscreen mode Exit fullscreen mode

This reads your .env file and loads all the variables into your environment. LangChain's OpenAI integration automatically picks up OPENAI_API_KEY from there.

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
Enter fullscreen mode Exit fullscreen mode

This creates a Chat LLM instance. temperature=0 makes the model's output consistent and predictable, which is ideal when building agents.

messages = [
    SystemMessage(content="..."),
    HumanMessage(content="...")
]
Enter fullscreen mode Exit fullscreen mode

Chat models take a list of structured messages rather than a plain string. We will cover these message types in depth in the next article.

response = llm.invoke(messages)
Enter fullscreen mode Exit fullscreen mode

invoke() is LangChain's standard method for running a chain or model. It is synchronous, meaning it waits for the full response before continuing.


Using a Different LLM Provider

If you want to use Anthropic's Claude instead of OpenAI, here is how to swap it in:

pip install langchain-anthropic
Enter fullscreen mode Exit fullscreen mode
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-3-5-haiku-20241022",
    temperature=0
)
Enter fullscreen mode Exit fullscreen mode

Add ANTHROPIC_API_KEY=your-key to your .env file and everything else stays exactly the same. LangChain's unified interface means you can switch providers without rewriting your application logic.

For Google Gemini:

pip install langchain-google-genai
Enter fullscreen mode Exit fullscreen mode
from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(
    model="gemini-1.5-flash",
    temperature=0
)
Enter fullscreen mode Exit fullscreen mode

Add GOOGLE_API_KEY=your-key to your .env file.


Streaming Responses

For real applications, you often want to stream the response token by token rather than waiting for the full output. Here is how to do that:

# streaming_example.py

from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

load_dotenv()

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

messages = [HumanMessage(content="Tell me a short story about a robot learning to cook.")]

print("Streaming response:\n")

# Stream tokens as they arrive
for chunk in llm.stream(messages):
    print(chunk.content, end="", flush=True)

print("\n\nDone!")
Enter fullscreen mode Exit fullscreen mode

The stream() method yields chunks as they are generated, giving your users a much better experience for longer responses.


Error Handling

Production code needs to handle failures gracefully. Here are the most common errors you will encounter and how to handle them:

# error_handling_example.py

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from openai import AuthenticationError, RateLimitError, APIConnectionError

load_dotenv()

def call_llm(prompt: str) -> str:
    """Call the LLM with proper error handling."""

    llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
    messages = [HumanMessage(content=prompt)]

    try:
        response = llm.invoke(messages)
        return response.content

    except AuthenticationError:
        # Wrong or missing API key
        raise ValueError(
            "Invalid API key. Check your OPENAI_API_KEY in the .env file."
        )

    except RateLimitError:
        # Too many requests or out of credits
        raise RuntimeError(
            "Rate limit hit. Either slow down requests or check your OpenAI billing."
        )

    except APIConnectionError:
        # Network issue
        raise ConnectionError(
            "Could not connect to OpenAI. Check your internet connection."
        )

    except Exception as e:
        # Catch-all for unexpected errors
        raise RuntimeError(f"Unexpected error calling LLM: {str(e)}")


# Test it
if __name__ == "__main__":
    try:
        result = call_llm("What is 2 + 2?")
        print("Result:", result)
    except Exception as e:
        print("Error:", e)
Enter fullscreen mode Exit fullscreen mode

Best Practices

A few habits to build from day one:

Always use a virtual environment. Never install LangChain or any project dependencies globally. It will cause conflicts eventually.

Pin your dependencies. Once your project is working, save the exact versions you are using:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Anyone else (or future you) can then reproduce your exact environment with:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Never commit your .env file. Double-check your .gitignore before every git push.

Use temperature=0 for agents. Agents need to be predictable. Higher temperature values introduce randomness that can cause agents to behave inconsistently.

Log your token usage. Token costs add up. Keep an eye on the token_usage field in responses, especially during development.


Troubleshooting

"OPENAI_API_KEY not found" error

Make sure:

  1. Your .env file is in the same directory as your script
  2. You called load_dotenv() before accessing the key
  3. The variable name in .env is exactly OPENAI_API_KEY with no spaces around the =

"ModuleNotFoundError: No module named 'langchain'"

Your virtual environment is probably not activated. Run source venv/bin/activate (Mac/Linux) or venv\Scripts\activate (Windows) and try again.

"AuthenticationError: Incorrect API key"

Go back to platform.openai.com/api-keys and generate a new key. Make sure you copied the full key including the sk- prefix.

"RateLimitError" on a new account

New OpenAI accounts sometimes have very low rate limits until you add a payment method. Go to your OpenAI billing settings and add a card. You will not be charged until you actually use credits.

Response is very slow

Switch to gpt-4o-mini if you are using a larger model. It is significantly faster and cheaper while still being very capable for learning purposes.


Your Project Structure So Far

langchain-agents-series/
├── .env                      # Your API keys (never commit this)
├── .gitignore                # Tells Git to ignore .env and venv
├── requirements.txt          # Pinned dependencies
├── venv/                     # Virtual environment
├── hello_agent.py            # Your first LangChain call
├── streaming_example.py      # Streaming responses
└── error_handling_example.py # Error handling patterns
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Always use a virtual environment to isolate your project dependencies.
  • Store API keys in a .env file and load them with python-dotenv.
  • Never hardcode API keys or commit .env to version control.
  • ChatOpenAI is LangChain's interface for OpenAI's chat models.
  • Chat models take a list of structured messages, not plain strings.
  • Use temperature=0 for agents to keep behavior predictable.
  • LangChain's unified interface makes it easy to swap between different LLM providers.
  • Always add error handling around LLM calls in production code.

What Is Coming Next

Now that our environment is running, it is time to really understand how Chat LLMs work. In the next article, we will dig into the three message types (SystemMessage, HumanMessage, AIMessage), learn how to structure multi-turn conversations, and build a simple conversation loop that keeps track of history.

See you in Article 3.

Top comments (0)