LangChain Tutorial for Beginners: Step by Step Guide
A hands-on intro to LangChain, paired with the Code Snippet Generator.
LangChain tutorials from even a year ago show code that barely resembles what you’d write today. The library moves fast, and a lot of beginner guides floating around are already teaching outdated patterns without saying so. If you’re still deciding whether LangChain is the right framework to start with at all, see AI Agent Framework for Beginners first.
Getting started, step by step, is what the rest of this covers — including working code you can copy and run.
What LangChain Actually Gives You
LangChain isn’t the AI. It’s the connective tissue — a consistent way to wire a language model together with tools, memory, and prompts, instead of writing that plumbing yourself for every project.
Under the hood, LangChain’s agent tooling now runs on LangGraph, which handles the actual execution loop: call the model, check if it wants to use a tool, run the tool, feed the result back, repeat until done — the same plan-act-observe-decide cycle covered generally in How AI Agents Work: An Interactive Breakdown. You don’t need to touch LangGraph directly for basic use — LangChain’s own agent functions handle that for you, though the official LangGraph documentation is worth bookmarking once you outgrow the basics.
Step 1: Install and Set Up
Three packages cover most starting projects: the core library, the OpenAI integration, and langchain-core, which the other two quietly depend on.
pip install langchain langchain-openai langchain-core
Set your API key as an environment variable rather than pasting it into your code:
export OPENAI_API_KEY="your-api-key-here"
Step 2: A Plain Language Model Call
Before agents, start with the simplest possible piece — calling a model directly. This confirms your setup works before adding any complexity.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0)
response = llm.invoke("Explain agentic AI in one sentence.")
print(response.content)
Step 3: Give It a Tool
A model on its own can’t check real data. A tool is just a regular Python function the model can choose to call when it needs information it doesn’t already have.
from langchain.tools import tool
@tool
def check_inventory(product: str) -> str:
"""Look up how many units of a product are in stock."""
inventory = {"laptop": 45, "keyboard": 120, "mouse": 200}
count = inventory.get(product.lower())
return f"{count} units in stock" if count else "Product not found"
The docstring matters here — the model reads it to decide when this tool is relevant, so write it like you’re explaining the function to someone who’s never seen your code.
Step 4: Build the Agent
This is where the model and tools come together into something that can reason about what to do, not just answer directly — and once you’re coordinating several of these together, What Are Multi-Agent AI Systems? A Visual Breakdown covers the patterns for that.
from langchain.agents import create_agent
agent = create_agent(
model="gpt-4o",
tools=[check_inventory],
system_prompt="You are a helpful inventory assistant."
)
result = agent.invoke({
"messages": [{"role": "user", "content": "How many keyboards do we have?"}]
})
print(result["messages"][-1].content)
That’s a working agent. It reads the question, decides it needs the inventory tool, calls it, and answers using the real result — not a guess.
| Stage | What you’re doing |
|---|---|
| 1. Plain call | Confirm your model and API key work |
| 2. Add a tool | Give it one real function to call |
| 3. Build the agent | Wire the model and tool together with create_agent |
| 4. Test with real inputs | Try phrasing you didn’t write the code around |
If a tutorial doesn’t say which LangChain version its code targets, don’t trust the import paths on the page — check the current docs before you copy a single line.
Where Beginners Get Stuck
The most common issue isn’t the agent logic — it’s tool docstrings that are too vague for the model to know when to use them, and multi-tool setups attempted before a single-tool version is working reliably. Get one tool working end to end before adding a second one.
Generate Your Own Starting Point
Copying the general pattern is one thing — having the exact starter snippet for what you’re building is faster. If you’d rather build this without writing any code, How to Build an AI Agent (No Code Required) covers that path instead. The generator below produces a working starting snippet for the kind of agent you’re building, ready to paste in and run.
Code Snippet Generator