Room 42, home

LangChain basics for beginners

The building blocks of LangChain (models, prompts, tools and chains) in plain English, plus when a plain provider SDK is all you need.

Agentsadvanced3 min readUpdated 12 Jun 2026

In short

  1. 01LangChain is a framework that gives you one interface across model providers plus standard pieces for prompts, tools, and structured output.
  2. 02Its core building blocks are models, messages and prompts, output parsers, and tools you bind for the model to call.
  3. 03You compose pieces with the pipe operator into one runnable chain that shares invoke, stream, and batch methods.
  4. 04Reach for it when you need to swap models or build an agent loop, but a plain provider SDK is simpler for one-off prompts.
Contents · 5 sections
  1. 1What is LangChain for, and when should I skip it?
  2. 2How do I install LangChain?
  3. 3What are the core pieces of LangChain?
  4. 4How do I compose a chain with the pipe operator?
  5. 5When should I use LangChain instead of a plain SDK?

LangChain is a frameworkA reusable layer of code that handles common plumbing so you write less boilerplate for building applications on top of language models. It gives you one interface across providersThe companies that host the language model, such as Anthropic, OpenAI, or Google, standard pieces for prompts, tools, and structured output, and a clean way to wire them together. This guide is the foundation you build agents on in the rest of this phase.

What is LangChain for, and when should I skip it?

LangChain earns its place when you want to swap models without rewriting, when you need standard handling for messages, tools, and structured output, or when you are building an agent loopA cycle where the model takes an action, sees the result, and decides what to do next until the task is done. Its value is the abstraction layerCode that hides provider-specific details behind a single uniform interface.

Be honest about the flip side: if you only ever call one provider with a simple prompt and no agent loop, the provider's own SDK is simpler and has fewer moving parts. Reach for LangChain when the abstraction pays for itself, not by default.

Worth knowing: LangChain 1.0 (late 2025) slimmed the core langchain package down to agent essentials and moved older chain classes into a separate langchain-classic package. If you find a tutorial importing from langchain.chains, it is pre-1.0.

How do I install LangChain?

Install the core plus the provider you want. Each provider is its own package:

pip install -U langchain langchain-core langchain-anthropic

Set your ANTHROPIC_API_KEY in the environment and you are ready.

What are the core pieces of LangChain?

A few building blocks cover most of what you do.

Models. Initialise a chat model. The provider-agnostic helper lets you swap providers with a string:

from langchain.chat_models import init_chat_model
 
model = init_chat_model("claude-sonnet-4-6", model_provider="anthropic")

Messages and prompts. Conversations are lists of messages, and prompt templates fill in variables:

from langchain_core.prompts import ChatPromptTemplate
 
prompt = ChatPromptTemplate.from_template("Answer in one sentence: {question}")

Output parsing. Parsers turn the model's reply into the shape you want, from a plain string up to a validated objectAn object whose fields are checked against a schema so bad or missing data is caught early:Common gotchaDo not treat with_structured_output as a guarantee that the field types are correct. It enforces the schema's shape, but the model still picks the values, so a confident wrong year: 2011 parses clean and slips through.Keep your own sanity checks on the numbers that matter, and prefer descriptive field names and docstrings on the Pydantic model so the model fills them right in the first place.

from pydantic import BaseModel
 
class Movie(BaseModel):
    title: str
    year: int
 
structured = model.with_structured_output(Movie)
structured.invoke("Give me details about Inception")  # -> a Movie

Tools. Decorate a function to make it callable by the model:

from langchain.tools import tool
 
@tool
def get_weather(city: str) -> str:
    """Return the weather for a city."""
    return f"Sunny in {city}"
 
model_with_tools = model.bind_tools([get_weather])

How do I compose a chain with the pipe operator?

LangChain's composition style chains pieces with the | operator. Each piece takes the previous one's output, and the whole chain shares one interface: invoke, stream, batch, and their async versions.Common gotchaA bare prompt | model chain returns an AIMessage, not a string, so print()-ing it shows the whole object repr and result.upper() throws.Add | StrOutputParser() (or read .content) when you want plain text out.

from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
 
prompt = ChatPromptTemplate.from_template("Answer in one sentence: {question}")
model = init_chat_model("claude-sonnet-4-6", model_provider="anthropic")
parser = StrOutputParser()
 
chain = prompt | model | parser
print(chain.invoke({"question": "What is a chain?"}))

That is a complete LangChain program: a prompt, a model, and a parser, composed into one runnable thing.

When should I use LangChain instead of a plain SDK?

If your task is "prompt in, text out", a chain like the one above is plenty. The moment you need the model to loop, call tools, branch, and remember state, a flat chain is not enough. That is what graphs are for, and the next guide introduces LangGraph.