Lesson 1 of 9 · Core
What Is LangChain?
What LangChain is: standard interfaces for chat models, prompts, parsers, retrievers and tools, integration packages, LangGraph, LangSmith and when to use it.
- 8 min read
- Intermediate
- Reviewed
- Explain what LangChain is and the problems it solves
- Name the main packages: langchain-core, langchain, integrations, LangGraph and LangSmith
- Decide when LangChain is worth using and when a provider's SDK is enough
- Install LangChain and make a first model call
A toolkit for LLM applications
Calling a language model is one line of code. Building an application around it is more: prompts with variables, parsing the reply into data, fetching documents, calling tools, switching providers, tracing what happened. Every team ends up writing the same plumbing.
Its central idea is the standard interface. Every chat model, from any provider, is called the same way. Every retriever returns documents the same way. Every tool is described the same way. So you can swap one provider, vector database or tool for another without rewriting the rest of your program.
See the idea in plain Python
Here's the pattern LangChain is built on, with two made-up providers whose APIs look nothing alike. Small adapter classes give them the same invoke method, and the application code never needs to know which one it's using:
# Why a standard interface helps: two made-up providers with different APIs,
# wrapped so the rest of the program calls them the same way.
class AlphaSDK: # provider A: takes a prompt string, returns a dict
def complete(self, prompt, max_words):
return {"output_text": f"[Alpha] {prompt[:30]}...", "words_used": 12}
class BetaSDK: # provider B: takes a message list, returns a tuple
def chat(self, messages):
return ("assistant", f"[Beta] answered {len(messages)} message(s)")
class ChatModel: # the shared interface: invoke(messages) -> text
def invoke(self, messages):
raise NotImplementedError
class AlphaChat(ChatModel):
def invoke(self, messages):
prompt = "\n".join(f"{role}: {text}" for role, text in messages)
return AlphaSDK().complete(prompt, max_words=200)["output_text"]
class BetaChat(ChatModel):
def invoke(self, messages):
return BetaSDK().chat([{"role": r, "content": t} for r, t in messages])[1]
def summarise_ticket(model, ticket): # application code: knows nothing about providers
return model.invoke([("system", "Summarise support tickets in one line."), ("user", ticket)])
ticket = "My order 881 arrived damaged and I would like a replacement."
for model in [AlphaChat(), BetaChat()]: # swap providers without touching summarise_ticket
print(type(model).__name__, "->", summarise_ticket(model, ticket))LangChain does exactly this for dozens of real providers. Its integration packages contain the adapters; your code talks to the shared interface.
The LangChain family
LangChain is a set of packages that work together:
| Package | What it contains |
|---|---|
langchain-core | The shared interfaces: messages, chat models, prompts, output parsers, retrievers, tools, and LCEL for chaining them |
langchain | Higher-level pieces built on core, such as init_chat_model and create_agent |
| Integration packages | Adapters for each provider: langchain-anthropic, langchain-openai, langchain-google-genai and many more |
langgraph | A framework for stateful, multi-step agents as graphs; LangChain's agents run on it |
| LangSmith | A hosted platform (with a Python package, langsmith) for tracing, debugging and evaluating LLM applications |
This course covers LangChain itself; the LangGraph course goes deeper into graphs, and lesson 8 introduces LangSmith.
Your first call
Install LangChain and the integration for your provider. Here, Anthropic:
pip install -U langchain langchain-anthropicThen call a model through the standard interface:
from langchain.chat_models import init_chat_model
model = init_chat_model("anthropic:MODEL_NAME") # reads ANTHROPIC_API_KEY from the environment
reply = model.invoke("In one sentence, what is LangChain?")
print(reply.content)LangChain is an open-source framework that gives you standard building blocks, such as chat models, prompts, tools and retrievers, for building applications with large language models.
Replace MODEL_NAME with a current model name from your provider's documentation, and set your API key as an environment variable, as in the Working with AI APIs course. The string "anthropic:MODEL_NAME" tells init_chat_model which integration to load. To switch to OpenAI, install langchain-openai, set OPENAI_API_KEY and change only that string to "openai:MODEL_NAME". Nothing else changes.
When to use it, and when not to
LangChain earns its place when you:
- Want to switch or compare providers without rewriting code.
- Need integrations: document loaders, vector stores, retrievers and tools that already exist.
- Build retrieval (RAG) and agents and want tested building blocks.
- Want tracing and evaluation through LangSmith with almost no extra code.
A provider's own SDK may be the better choice when your application is small, uses one provider, and needs that provider's newest features on day one. An extra layer brings extra concepts, extra dependencies and occasional lag behind new provider features.
Common mistakes
Write an adapter for a third provider, the job LangChain's integration packages do for you.
class GammaSDK: # a third provider: system prompt separate, returns an object
class Reply:
def __init__(self, text):
self.text = text
def generate(self, system, turns):
return self.Reply(f"[Gamma] system={system!r}, {len(turns)} turn(s)")
class GammaChat:
def invoke(self, messages):
# messages is a list of (role, text). Pass the text of the "system" message as system=,
# and every other message as a dict {"role": role, "content": text} in turns.
# Return just the reply's text.
pass
print(GammaChat().invoke([("system", "Be brief."), ("user", "Hi"), ("assistant", "Hello!"), ("user", "Help?")]))
What is the main benefit of LangChain's standard interfaces?
Summary
- LangChain provides standard interfaces and integrations for LLM applications.
- The family: langchain-core, langchain, provider integration packages, LangGraph for agents as graphs, and LangSmith for tracing and evaluation.
init_chat_model("provider:model")creates any provider's chat model with the same interface.- Use it when you need portability, integrations, agents or tracing; a provider's SDK can be enough for small, single-provider apps.
Next: chat models in depth.
Finished reading? Tick it off to track your progress.