LangChain Setup Guide

Build LLM-powered applications

Last updated: May 22, 2026

✨ What's New in LangChain v1.3.x (May 2026)

All code examples below use the latest v1.3.1 patterns.

What is LangChain?

LangChain is one of the most popular frameworks for building AI applications. It lets you chain together LLM calls, add memory, connect to databases, and build autonomous agents. Essential for serious AI orchestration work.

Step 1: Install LangChain

Create a new Python project:

# Create project folder
mkdir my-langchain-app
cd my-langchain-app

# Create virtual environment
python -m venv venv
source venv/bin/activate

# Install LangChain (v1.3.1+)
pip install langchain langchain-core langchain-community

Install Model Providers:

# For OpenAI (updated models)
pip install langchain-openai

# For Ollama (local models)
pip install langchain-ollama

# For Anthropic (Claude)
pip install langchain-anthropic

# For Hugging Face
pip install langchain-huggingface

Step 2: Set Up API Keys

Create a .env file for your API keys:

# .env file
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
HUGGINGFACEHUB_API_TOKEN=hf_...

# For Ollama (no key needed - runs locally)
OLLAMA_BASE_URL=http://localhost:11434

Load environment variables in your Python code:

from dotenv import load_dotenv
load_dotenv()

Step 3: Your First Chain

Basic LLM Chain (Updated Models):

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Initialize the model (updated: use gpt-4o or gpt-4o-mini, NOT gpt-3.5-turbo)
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

# Create a prompt template
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "{input}")
])

# Create the chain
chain = prompt | llm | StrOutputParser()

# Run the chain
response = chain.invoke({"input": "What is AI orchestration?"})
print(response)

With Ollama (Local):

from langchain_ollama import ChatOllama

llm = ChatOllama(
    model="llama3.2",
    base_url="http://localhost:11434"
)

chain = prompt | llm | StrOutputParser()
response = chain.invoke({"input": "Hello!"})
print(response)

Step 4: Advanced Streaming (v2/v3)

LangChain v1.3.x introduces content-block-centric streaming:

Streaming v2 (Content Blocks):

from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(model="gpt-4o")

# Stream with version="v3" for latest features
for chunk in llm.stream("Tell me a story", version="v3"):
    print(chunk.content, end="", flush=True)

Event Streaming (v3):

# Stream events with v3 support
async for event in llm.astream_events(
    "Write a poem",
    version="v3"
):
    kind = event["event"]
    if kind == "on_chat_model_stream":
        content = event["data"]["chunk"].content
        print(content, end="", flush=True)

Why v3? Better metadata, improved token counting, and cleaner event structure.

Step 5: Add Memory

Make your chain remember previous messages:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory

# Create prompt with memory
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder(variable_name="history"),
    ("user", "{input}")
])

# Create the chain
chain = prompt | llm

# Add memory
store = {}

def get_session_history(session_id: str):
    if session_id not in store:
        store[session_id] = InMemoryChatMessageHistory()
    return store[session_id]

chain_with_memory = RunnableWithMessageHistory(
    chain,
    get_session_history,
    input_messages_key="input",
    history_messages_key="history"
)

# Use with session ID
response = chain_with_memory.invoke(
    {"input": "My name is Jackie"},
    config={"configurable": {"session_id": "user123"}}
)

response = chain_with_memory.invoke(
    {"input": "What's my name?"},
    config={"configurable": {"session_id": "user123"}}
)
# Will respond: "Your name is Jackie"

Step 6: Build a RAG Chain

Retrieval Augmented Generation with your own documents:

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores import Chroma

# Load documents
loader = TextLoader("my-documents.txt")
docs = loader.load()

# Split into chunks
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200
)
splits = text_splitter.split_documents(docs)

# Create vector store
embeddings = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = Chroma.from_documents(
    documents=splits,
    embedding=embeddings
)

# Create retriever
retriever = vectorstore.as_retriever()

# Build RAG chain
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

template = """Answer the question based only on the following context:
{context}

Question: {question}
"""

prompt = ChatPromptTemplate.from_template(template)

rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# Query your documents
response = rag_chain.invoke("What does the doc say about AI?")
print(response)

Essential LangChain Packages

langchain

Core library

langchain-core

Base abstractions

langchain-community

Community integrations

langchain-openai

OpenAI integration

langchain-anthropic

Anthropic integration

langchain-ollama

Ollama integration

🔧 Troubleshooting

ModuleNotFoundError

Make sure your virtual environment is activated: source venv/bin/activate

API key errors

Check that .env file is loaded and keys are correct. Use os.getenv("OPENAI_API_KEY") to verify.

Ollama connection refused

Make sure Ollama is running: ollama serve

Deprecation warnings

LangChain v1.3.x removed gpt-3.5-turbo references. Update to gpt-4o or gpt-4o-mini.

🎉 LangChain is Ready!

You can now build sophisticated AI applications with chains, memory, and RAG. Explore CrewAI and AutoGen for multi-agent systems, or check the AI Toolkit for more orchestration frameworks.