LangChain: a starter guide
What LangChain is and when you need it, with Python examples: a first chain, structured output and an agent with a tool, on a free local model.

In 30 seconds
- LangChain is a library that connects language models (LLMs) to your app: models, prompts, tools and agents.
- Its main benefit: one interface for many models, so switching is one line.
- If your app is a single question and answer, the model's own library may be enough. Use LangChain as the steps grow.
Install
pip install -U langchain langchain-ollama
ollama pull qwen2.5:3b
1. A first chain
A chain is a sequence of steps: a prompt template, then the model. The | sign passes one step's output to the next.
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
model = init_chat_model("ollama:qwen2.5:3b", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a patient teacher. Answer in two short sentences."),
("human", "{question}"),
])
chain = prompt | model
answer = chain.invoke({"question": "What is a database index?"})
print(answer.content)
To try another model, change "ollama:qwen2.5:3b" and install that provider's package.
2. Structured output
Instead of free text, ask for data in a fixed shape your code can use:
from langchain.chat_models import init_chat_model
from pydantic import BaseModel, Field
class Ticket(BaseModel):
customer: str = Field(description="Customer name")
problem: str = Field(description="The problem in a few words")
urgent: bool = Field(description="true when the customer has a deadline today or cannot work, otherwise false")
model = init_chat_model("ollama:qwen2.5:3b", temperature=0)
extractor = model.with_structured_output(Ticket)
ticket = extractor.invoke("Hi, this is Sara. URGENT: our payment page is broken and we cannot sell anything.")
print(ticket)
The output we got:
customer='Sara' problem='Payment page is broken' urgent=True
Careful: small models can fill fields wrongly. Describe each field clearly and check results before relying on them.
3. An agent with a tool
The agent decides when to call your function. The model reads the function's docstring, so write it clearly.
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.tools import tool
ORDERS = {"A100": "shipped", "B200": "preparing"}
@tool
def get_order_status(order_id: str) -> str:
"""Get the status of an order by its number, for example A100."""
return ORDERS.get(order_id.upper(), "order not found")
agent = create_agent(
model=init_chat_model("ollama:qwen2.5:3b", temperature=0),
tools=[get_order_status],
system_prompt="You are a shop assistant. Use the tools for order questions. Never guess.",
)
result = agent.invoke({"messages": [{"role": "user", "content": "Where is my order B200?"}]})
print(result["messages"][-1].content)
When to move to LangGraph
| You need | Use |
|---|---|
| Simple steps in order | A chain |
| A model that picks between tools | create_agent |
| Full control of the path, memory and human approval | LangGraph |
Common mistakes
- Copying old examples. LangChain's interfaces changed with version 1.0, so check the official docs.
- Vague tool descriptions, so the model picks the wrong tool.
- An agent with no tests. Keep fixed questions and run them after every change.
Next guide
Code tested on 30 November 2026 with Python 3.10, LangChain 1.4 and LangGraph 1.2.