LangChain Tutorial

This LangChain tutorial teaches you how to build applications powered by large language models (LLMs) in Python. LangChain is a free, open-source framework that gives you one consistent way to talk to chat models, write prompts, parse answers, search your own documents and let models call tools.

The course uses LangChain 1.x, the stable release line that cleaned up the older APIs. All examples run against open-source models served locally by Ollama, so you can follow along for free without an API key. Each lesson also shows how to swap in a hosted provider when you need one.

What is LangChain?

An LLM on its own only turns text into more text. Real applications need more: prompt templates, conversation history, structured answers your code can trust, access to private documents and the ability to take actions such as searching or calling an API.

LangChain provides building blocks for all of these. Every block shares the same interface (the Runnable interface), so you can connect them like pipes. The project is split into small packages: langchain-core holds the base abstractions, langchain is the main package with agents and helpers, and integration packages such as langchain-ollama or langchain-chroma connect to specific providers. LangGraph, from the same team, is the lower-level library that powers LangChain agents.

There is also LangChain.js for JavaScript and TypeScript. The concepts in this tutorial map almost one-to-one to it.

Why Learn LangChain?

  • Swap model providers (Ollama, OpenAI, Anthropic, Google, Mistral) by changing one line.
  • Ready-made integrations for hundreds of document loaders, vector stores and tools.
  • Composable pipelines with LCEL that support streaming and batching for free.
  • Agents built on LangGraph for tool calling, memory and human-in-the-loop flows.
  • Built-in tracing with LangSmith to debug what your app sent to the model.

Your First LangChain Example

Make sure Ollama is installed and you have pulled a model (setup is covered in the next lesson). Then this short script asks a local Llama model a question:

from langchain_ollama import ChatOllama

llm = ChatOllama(model="llama3.2", temperature=0)
reply = llm.invoke("In one sentence, what is LangChain?")
print(reply.content)
Sample output (your text will differ):
LangChain is an open-source framework that helps developers build applications powered by large language models.

ChatOllama is a chat model class. invoke sends one request and returns an AIMessage object; its content attribute holds the text. Because model output is generated, every output box in this tutorial is a hand-written sample. Your exact wording will vary.

What This LangChain Tutorial Covers

  • Setup, chat models, messages and prompt templates
  • LCEL and runnables for building chains
  • Output parsers and structured output with Pydantic
  • Chat history and conversational memory
  • Document loaders, text splitters, embeddings, vector stores and retrievers
  • Tools, agents with create_agent and LangGraph basics
  • Streaming, LangSmith observability and a final project

Prerequisites

You should be comfortable with basic Python 3.10+ (functions, classes, dictionaries, pip and virtual environments). No machine-learning background is needed. A computer with 8 GB of RAM can run small models such as llama3.2 (3B) comfortably; larger models need more memory or a GPU.

Frequently asked questions

Is LangChain free?

Yes. LangChain and LangGraph are open source under the MIT license. LangSmith, the hosted tracing platform, has a free tier and paid plans.

Is LangChain still worth learning in 2026?

Yes. Version 1.x simplified the API around create_agent and standard content blocks, and it remains one of the most widely used LLM frameworks. Knowing it also makes LangGraph easy to pick up.

What is the difference between LangChain and LangGraph?

LangChain gives high-level building blocks and a ready-made agent. LangGraph is a lower-level library for building custom stateful workflows as graphs. LangChain agents run on LangGraph under the hood.

Can I use LangChain without OpenAI?

Absolutely. This tutorial uses local open-source models through Ollama, and LangChain supports many other providers too.