LangChain Tutorial: Getting Started the Right Way
Install LangChain v1.0, build your first agent with create_agent, chain prompts with LCEL, and add retrieval. A practical tutorial using current patterns only.
By Galactic News Editorial · Published 2026-11-11T13:20:33+00:00 · Updated 2026-11-11T13:20:33+00:00
LangChain Tutorial: Getting Started the Right Way
This LangChain tutorial takes you from zero to a working agent. You will install LangChain v1.0, build your first agent, chain prompts with LCEL, and add retrieval over your own documents. Each step uses current patterns from the official docs. Old classes such as LLMChain and AgentExecutor are deprecated, so this guide skips them entirely.
What LangChain Is in 2026
LangChain is an open-source framework for building applications on large language models. It connects models to tools, data, and APIs. Version 1.0 reached general availability in October 2025. The release rebuilt the framework around a smaller core.
The central idea in v1.0 is simple. An agent equals a model plus a harness. The harness is everything around the model loop: the prompt, the tools, and any middleware that shapes behavior. You start with the primitives and compose only what your use case needs.
LangChain provides create_agent as its minimal agent harness. Agents built this way run on LangGraph, the low-level orchestration framework underneath. LangGraph adds durable execution, human-in-the-loop support, and persistence. You can use LangChain directly for a customizable harness, or step down to LangGraph for advanced control.
LangChain also standardizes how you talk to models. One interface covers chat models, embeddings, and more across providers. You can switch providers with minimal code changes. That keeps your application portable as requirements evolve.
LangChain Tutorial: Install and Set Up
Follow these steps in order. They give you a clean project that matches the official docs as they stand today.
- Check your Python version. Use Python 3.10 or newer. Run
python --versionto confirm. - Create a virtual environment. Run
python -m venv venv, then activate it. This keeps project packages separate from system packages. - Install LangChain. Run
pip install -qU langchain "langchain[openai]". This installs the core framework plus the OpenAI integration. - Add a provider package if you use another model. LangChain splits provider code into separate packages, such as
langchain-anthropicfor Claude orlangchain-google-genaifor Gemini. Install only the ones you need. - Set your model API key. Create a
.envfile withOPENAI_API_KEY=your-key-here. Load it in your script withpython-dotenv, or export it in your shell. - Verify the install. Run
python -c "import langchain; print(langchain.__version__)". A version number means the install worked.
Keep your keys out of version control. Add .env to .gitignore before your first commit. A leaked key is a billing risk, not just a security issue.
Build Your First Agent with create_agent
The fastest way to learn LangChain v1.0 is to build a small agent. An agent is a model that can call tools. The example below follows the official introduction almost exactly. It defines one tool, a weather lookup, and lets the model decide when to call it.
from langchain.agents import create_agent
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
agent = create_agent(
model="openai:gpt-5.5",
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)
print(result["messages"][-1].content_blocks)
Four parts matter here. First, the tool is a plain Python function. Its docstring tells the model what the tool does, so write docstrings with care. Second, the model string names the provider and model in one place. Third, the system prompt sets the agent's behavior. Fourth, invoke runs one turn and returns the full message list.
Try changing the tool next. Replace the weather function with a calculator, a search function, or a database lookup. The agent pattern stays the same. Only the tool changes. That is the point of the harness design.
Build a Chain with LCEL
Not every task needs an agent. For fixed, predictable steps, use a chain instead. LangChain Expression Language, or LCEL, composes components with the pipe operator. Each component's output becomes the next component's input.
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_template(
"Explain {topic} in one short paragraph."
)
chain = prompt | model | StrOutputParser()
print(chain.invoke({"topic": "photosynthesis"}))
This chain has three parts. The prompt template turns inputs into messages. The model generates text. The output parser returns a plain string instead of a message object. Every LCEL chain supports invoke, stream, and batch. Streaming is useful for chat interfaces. Batching helps when you process many inputs at once.
LCEL replaced the old chain classes. LLMChain and similar classes are deprecated in v1.0. They still exist in langchain-classic for backward compatibility, but new code should use LCEL. Tutorials that start with LLMChain are out of date. Skip them.
Add Retrieval to Your LangChain App
A model alone cannot answer questions about your private documents. Retrieval-augmented generation, or RAG, fixes that. The model gets relevant passages at inference time, then answers from that evidence. Indexing your documents has four steps.
- Load. Load your data sources into
Documentobjects. LangChain offers loaders for files, web pages, and databases. - Split. Break large documents into smaller chunks with a text splitter.
RecursiveCharacterTextSplitteris the recommended splitter for generic text. Large chunks are harder to search and may not fit the model's context window. - Embed. An embeddings model converts each chunk into a numeric vector. Similar meanings land close together in vector space. That makes similarity search possible.
- Store. Save chunks and embeddings in a vector store. Options include an in-memory store for learning, plus Chroma, Pinecone, Qdrant, and pgvector for real projects.
At query time, the flow reverses. The retriever finds the chunks most similar to the question. The model receives the question plus those chunks. It then generates an answer grounded in your documents. Keep the retrieved set small. A few strong chunks beat a long pile of weak ones.
For larger RAG projects, LangChain's Deep Agents pattern goes further. It indexes docs, retrieves chunks, writes them to a filesystem, and delegates analysis to subagents. That keeps the main agent's context clean. Start with the simple pattern above. Move to agents only when simple retrieval stops being enough.
Debug with LangSmith Tracing
Chains fail in ways that print statements cannot explain. LangSmith traces each request through your app. You can inspect traces, tool calls, state transitions, and latency in one place. Set LANGSMITH_TRACING=true and your API key to turn it on.
Tracing pays off fast. It shows which step produced a bad output. It shows how long each call took. It also lets you build datasets and evaluators for your RAG app. Add tracing on day one, not after your first production incident.
Common Mistakes to Avoid
| Mistake | What to do instead |
|---|---|
| Following tutorials that use LLMChain | Use LCEL and create_agent, the current v1.0 patterns |
| Installing one giant package | Install langchain plus only the provider packages you need |
| Hard-coding API keys in scripts | Load keys from environment variables or a .env file |
| Skipping text splitting | Split documents before embedding so chunks stay searchable |
| Using an agent for a fixed pipeline | Use a simple LCEL chain when the steps never change |
| Debugging without tracing | Enable LangSmith tracing from the start |
The biggest mistake is copying old code without checking the version. LangChain changed a lot between v0.3 and v1.0. Import paths moved. Chain classes were deprecated. AgentExecutor gave way to create_agent. Always check the official docs for the version you installed.
Bottom Line
Start with create_agent for agents and LCEL chains for fixed pipelines. Install only the packages you need. Split and embed your documents before you build RAG. Turn on LangSmith tracing early. These four habits will keep your first LangChain project on the current, supported path.