How to Use LangChain: The Beginner's Guide (First Chain, Memory, RAG, Your First Agent, and the v1.0 Break)

CodexFuse Team · 2026-10-31T03:57:27.257Z · 15 min read

How to Use LangChain: The Beginner's Guide (First Chain, Memory, RAG, Your First Agent, and the v1.0 Break)

Learning how to use LangChain is the step that turns "I can call a chat API" into "I can build an AI app." LangChain is an open-source Python framework for building applications on top of large language models: it gives you one interface for many model providers, a standard way to describe tools, ready-made agents that call those tools in a loop, and integrations for the document loaders, vector stores, and retrievers used in RAG (per the official LangChain docs). This langchain for beginners guide runs the whole ladder on the current v1.x API: install, your first model call, your first LCEL chain, prompts and structured output, memory, a first RAG pipeline, custom tools, and your first agent — plus the v1.0 break that makes half the older tutorials online wrong.

You will build in this order: initialize a chat model with init_chat_model, chain a prompt to it with the pipe operator, add memory through an agent's thread, retrieve documents for RAG, define tools with the @tool decorator, and assemble it all with create_agent. If you want to sanity-check the raw model API before wiring it into LangChain, a free REST API tester in your browser lets you fire requests at any chat endpoint and inspect the response without writing a test script.

What Is the Fastest Way to Start Using LangChain?

Install the packages, put your key in a .env file, and initialize any provider's model with one function — you get a working model call in about five minutes.

  1. Install LangChain and your provider package: pip install -U langchain langchain-openai python-dotenv (per the current-era v1.x tutorial at aiseason.tech).
  2. Store your key in a .env file — OPENAI_API_KEY=... — and load it with load_dotenv(); never paste real keys into code you share.
  3. Initialize a model: from langchain.chat_models import init_chat_model, then llm = init_chat_model("openai:gpt-4o-mini") (the provider:model format from the official models docs).
  4. Call it: llm.invoke("Explain LangChain in one sentence") returns an AIMessage whose .content is the answer.
  5. Chain it: prompt | llm | StrOutputParser() turns prompt → model → plain text into one callable.
  6. Agent it: create_agent(model="openai:gpt-4o-mini", tools=[my_tool], system_prompt="...") runs the tool-calling loop for you.

What Is LangChain, Exactly?

LangChain is an orchestration layer between your application and language models — not a model itself. A raw chat API gives you one stateless request-response call; real applications need pipelines (multiple steps), memory (conversation context), and tools (APIs, calculators, databases). LangChain provides standard building blocks — prompts, chains, memory, retrieval, agents — so you assemble instead of hand-wiring everything, per the official docs.

The framework is provider-agnostic: each provider package (OpenAI, Anthropic, Google, OpenRouter, Ollama, and more — the docs list them all) implements the same chat-model interface, so swapping models means changing a string, not rewriting logic. The docs even note that new model names work immediately with no LangChain update, because provider packages pass names straight to the provider API.

Two facts shape the current landscape. First, LangChain 1.x re-centered the framework on agents: the quickstart now builds "a fully functional AI agent in just a few minutes," with chains as one tool in the box rather than the whole box. Second, LangChain agents run on LangGraph — the docs state it plainly — which is why agent skills like checkpointing and middleware carry over if you later drop down to LangGraph for explicit control.

How Do You Install LangChain and Connect Your First Model?

Create a project folder and a virtual environment, then install the core package plus the integration package for the provider you will actually call.

mkdir langchain-first && cd langchain-first
python -m venv .venv
source .venv/bin/activate
pip install -U langchain langchain-openai python-dotenv

The install shape above comes from the current-era v1.x tutorial at aiseason.tech (34 days old — the only beginner piece found on today's SERP that teaches the 1.x API; the dev.to and Medium guides are 176–200 days old and still teach v0.3 syntax). For another provider, swap the integration package: langchain-anthropic, langchain-google-genai, and so on.

Keys live in the environment, never in code. Create a .env file with OPENAI_API_KEY=your-key-here, load it with load_dotenv() from python-dotenv, and keep the file out of version control — our GitHub beginner guide covers the ignore-file habit that keeps secrets out of repos. The official quickstart recommends exactly this setup.

Now initialize and call a model. init_chat_model is the v1.x entry point — one function for every provider:

from dotenv import load_dotenv
load_dotenv()

from langchain.chat_models import init_chat_model

llm = init_chat_model("openai:gpt-4o-mini")
response = llm.invoke("What is LangChain in one sentence?")
print(response.content)

The "openai:gpt-4o-mini" string is the documented provider:model format (the models docs use "openai:o1" as the example). invoke() is the simplest of three invocation methods the docs define: invoke for one answer, stream for token-by-token chunks (AIMessageChunk objects that sum into a full message), and batch for parallel independent requests. Chat models also retry failed requests automatically — up to 6 attempts with exponential backoff for network errors, 429s, and 5xx — while 401/404 client errors are not retried.

How Do You Build Your First LangChain Chain?

A chain is a pipeline where each step's output feeds the next — and in v1.x you build one with the pipe operator, a pattern called LCEL (LangChain Expression Language).

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

prompt = ChatPromptTemplate.from_messages([
    ("system", "You explain AI concepts to beginners in two sentences."),
    ("human", "{topic}"),
])

chain = prompt | llm | StrOutputParser()
print(chain.invoke({"topic": "LangChain"}))

Three ideas are doing the work here. The prompt template turns variables into messages; the model generates; the output parser converts the model's AIMessage into a plain string. chain.invoke() accepts the same call whether it is one chain or twenty steps — composition is the point.

Use a chain when the flow is fixed and predictable: classify → summarize, translate → reformat, extract → validate. Chains are the "assembly line" lane; the moment the app needs to decide which step comes next at runtime, you have outgrown chains and need an agent (covered below). That chain-vs-agent line is the single most useful judgment a LangChain beginner can learn:

SituationUse a chainUse an agent
Steps are known in advanceYes — pipe them togetherOverkill
The app must choose tools at runtimeNoYes — create_agent
Needs conversation memoryManual message listsYes — thread + checkpointer
Needs to call external APIsOnly fixed callsYes — tools in a loop
Latency and cost sensitivityLowest — fixed callsHigher — loop until done

How Do Prompts and Structured Output Work?

Prompt templates standardize inputs and become powerful when your app scales — one template, many calls, no string-concatenation bugs.

ChatPromptTemplate.from_messages builds the message list chat models expect: a system message for instructions, a human message for input, with {variables} filled at invoke time. Keep instructions in the system message and data in the human message; that separation survives refactors far better than one giant prompt string.

When you need machine-readable answers instead of prose, ask for structured output. LangChain supports Pydantic models, TypedDict, and JSON Schema as output contracts, per the official models docs. In an agent you pass response_format= to create_agent and get a validated object back instead of free text — and when you are debugging what the model actually returned, pasting the raw response into a JSON viewer beats squinting at terminal output.

How Does Memory Work in LangChain?

In v1.x, memory belongs to agents: you give create_agent a checkpointer, and every conversation thread persists automatically — the old ConversationBufferMemory classes from the v0.x tutorials are legacy.

The mechanism is thread-level persistence. You pass a thread_id with each invocation, and the agent's state — the append-only messages list in AgentState — is checkpointed per thread. Invoke with the same thread_id tomorrow and the agent remembers yesterday. The docs recommend an in-memory checkpointer while developing and a database-backed one in production.

from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[get_weather],
    checkpointer=InMemorySaver(),
)

config = {"configurable": {"thread_id": "user-123"}}
result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in Austin?"}]},
    config,
)

Long threads eventually overflow the model's context window, and the docs are blunt about the consequences: models get "distracted" by stale content, respond slower, and cost more. The v1.x answer is middleware — SummarizationMiddleware compresses history before it overflows, and the trim/delete message patterns handle the rest. That middleware layer is genuinely the biggest conceptual change from v0.x: behavior you used to hand-code (trimming, summarizing, retries) now ships as composable pieces you snap onto the agent.

How Do You Build RAG with LangChain?

RAG (Retrieval-Augmented Generation) fixes the two hard limits the official retrieval docs name: models have finite context and frozen training data. Instead of dumping documents into the prompt, you build a searchable knowledge base and fetch only the relevant chunks at query time.

The pipeline has four swappable stages — document loaders (PDFs, web pages, text files), text splitters (chunking), embeddings (turning chunks into vectors), and vector stores (searchable storage). LangChain's whole design here is modularity: you can swap any stage without rewriting the app's logic, per the docs. The honest beginner version looks like this:

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS

docs = TextLoader("notes.txt").load()
chunks = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100).split_documents(docs)
store = FAISS.from_documents(chunks, OpenAIEmbeddings())
retriever = store.as_retriever(search_kwargs={"k": 4})

Two numbers in that snippet deserve attention because beginners get them wrong: chunk_size=800 keeps each chunk focused enough that a vector match means something, and k=4 retrieves only the top four chunks. A naive beginner pattern is one giant chunk and a huge top-k — which is just stuffing the context window and calling it retrieval. Retrieval that returns everything is not retrieval.

The official docs define three RAG architectures, and the choice is a real design decision, not trivia:

ArchitectureHow it worksBest for
2-Step RAGRetrieval always runs before generation — simple, predictableFAQ and documentation bots
Agentic RAGThe agent decides when and how to retrieve mid-reasoningResearch assistants with multiple sources
Hybrid RAGRetrieval plus validation loops — query rewriting, answer checksDomain Q&A that needs quality control

Start with 2-step RAG: it is the fastest to build, the easiest to debug, and it teaches you what good retrieval looks like before an agent starts making retrieval decisions on your behalf.

How Do You Give a LangChain App Tools?

A tool is any Python function with a name, a description, and typed arguments — the @tool decorator turns a plain function into one the model can call.

from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city. Use for any weather question."""
    return f"Sunny, 72°F in {city}."

The docstring is load-bearing: the model reads it to decide when to call the tool, so vague docstrings produce bad tool use — the current-era tutorial flags this as a top beginner mistake. Write the docstring like documentation for a smart colleague who cannot see your code: what the tool does, when to use it, and what each argument means.

To make tools callable, the model must be bound to them with bind_tools — or, far more commonly, you hand them to create_agent and the agent loop handles the rest: the model requests a tool call, the harness executes your function, the result goes back into the conversation, and the loop continues until the task is done. Tools can also read and write agent state through the hidden runtime parameter, which the docs describe as the way tools access short-term memory without the model seeing it.

How Do You Build Your First LangChain Agent?

An agent is a model calling tools in a loop until the task is complete — and create_agent is the v1.x harness that runs the loop, shaped by the prompt, the tools, and middleware.

from langchain.agents import create_agent

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[get_weather],
    system_prompt="You are a helpful assistant. Answer concisely.",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Should I bring a jacket to Austin?"}]}
)
print(result["messages"][-1].content)

At its simplest, create_agent takes a model (as a "provider:model" string or an initialized instance), a list of tools, and a system prompt. Behind that simplicity the harness manages the full agent state — the append-only message history, checkpoints per thread_id — and invokes with a plain message dict, exactly as the official agents docs show.

The reason create_agent replaced the old initialize_agent/AgentExecutor pair is extensibility: every advanced capability is middleware now. Need the agent to survive flaky networks? ModelRetryMiddleware and ToolRetryMiddleware. Handling sensitive data? PIIMiddleware enforces redaction deterministically instead of trusting a prompt. Need a human to approve risky actions? HumanInTheLoopMiddleware pauses the loop until someone approves, edits, or rejects. Take exactly the pieces your use case needs and skip the rest — that composability is the v1.x pitch.

Two production habits from the official quickstart: trace everything with LangSmith (set the env vars and every agent call becomes inspectable, which is how you debug a 12-step tool loop), and treat guardrails as code — policies that cannot live in a prompt get middleware, not longer system prompts. When you outgrow a single agent, the docs point to subagents (delegation with isolated contexts) and LangGraph for explicit branching, retries, and human approval.

What Broke in LangChain 1.0?

The 1.0 release reorganized the framework around agents and middleware, which means a large share of older tutorials now teach APIs that no longer exist in their old form. The current-era tutorial is explicit: "many posts use pre-1.0 APIs such as initialize_agent or AgentExecutor — in LangChain 1.x, start from create_agent."

What you wantPre-1.0 tutorials teachLangChain 1.x way
Initialize a chat modelProvider classes like ChatOpenAI(...) everywhereinit_chat_model("provider:model") — one function, every provider
Build an agentinitialize_agent + AgentExecutorcreate_agent(model=, tools=, system_prompt=)
Conversation memoryConversationBufferMemory / ConversationChainCheckpointer + thread_id on the agent
Context managementHand-rolled trimming codeSummarizationMiddleware and message utilities
Reliabilitytry/except around everythingModelRetryMiddleware / ToolRetryMiddleware
Simple pipelinesLLMChain, RetrievalQALCEL pipe (prompt | llm | parser) or 2-step RAG

The practical rule: check the date and the imports. If a tutorial is older than the 1.x era or its imports include langchain.chains.LLMChain, initialize_agent, or AgentExecutor, translate it to the right column before running anything. The old syntax is not just unfashionable — it will not run against current packages.

LangChain vs LlamaIndex: Which Should You Learn First?

Learn LangChain first if you are building applications; reach for LlamaIndex when your application's hardest problem is retrieval itself.

LangChainLlamaIndex
Core focusFull AI applications — agents, tools, memory, chainsData-centric RAG — ingestion, indexing, retrieval quality
Agent storycreate_agent + middleware is the center of the frameworkAgents exist, but retrieval is the headline
RAG depthSolid basics (loaders, splitters, vector stores)Deeper — advanced indexing, query engines, evaluation
EcosystemBroadest provider integrations; LangGraph underneathStrong data-connector library
Best first projectA tool-using assistant with memoryA high-quality Q&A bot over your documents

They are complements more than rivals: a common real-world stack uses LlamaIndex-style retrieval thinking inside a LangChain agent. But for a beginner, LangChain's breadth — one framework from first chain to first agent — makes it the better starting point.

What Are the Beginner Mistakes to Avoid?

Most LangChain failures are the same five, and all of them are avoidable on day one.

MistakeWhy it hurtsDo this instead
Copying pre-1.0 tutorials verbatiminitialize_agent / LLMChain no longer match the APITranslate to create_agent / LCEL using the table above
Vague tool docstringsThe model cannot decide when to call the toolDocument what, when, and each argument — like a real API doc
Keys in code or screenshotsLeaked credentials get abused within hours.env + load_dotenv(), git-ignored
Giant chunks and huge top-kContext stuffing masquerading as retrievalSmall focused chunks (~800 tokens), top-k of 3–5
No limits on agentsRunaway loops burn money and timeStep limits, tool allow-lists, human approval for risky actions

One more from the production side: when your agent needs a server to live on, size the machine before you deploy — our VPS hosting guide explains the tiers, and the Docker beginner guide walks through containerizing a Python app so the agent ships the same everywhere.

Sources

  • LangChain official docs — Models (init_chat_model, provider:model format, retries, invoke/stream/batch): docs.langchain.com/oss/python/langchain/models — crawled 2026-10-31 00:18 UTC
  • LangChain official docs — Agents (create_agent, middleware, thread_id, AgentState): docs.langchain.com/oss/python/langchain/agents — crawled 2026-10-31 00:19 UTC
  • LangChain official docs — Quickstart (agent arc, .env setup, LangSmith tracing): docs.langchain.com/oss/python/langchain/quickstart — crawled 2026-10-31 00:18 UTC
  • LangChain official docs — Short-term memory (checkpointer, SummarizationMiddleware): docs.langchain.com/oss/python/langchain/short-term-memory — crawled 2026-10-31 00:21 UTC
  • LangChain official docs — Retrieval (RAG architectures table): docs.langchain.com/oss/python/langchain/retrieval — crawled 2026-10-31 00:19 UTC
  • aiseason.tech — "LangChain Tutorial for Beginners: Build an AI Agent (2026)" (v1.x, create_agent; 34 days old): https://aiseason.tech/langchain-tutorial
  • dev.to/kalyna_pro — "LangChain for Beginners: Complete Guide (2026)" (v0.3 syntax reference; 176 days old): https://dev.to/kalyna_pro/langchain-for-beginners-complete-guide-2026-cnf

Frequently Asked Questions

Is LangChain good for beginners?

Yes, once you know basic Python. Start with a model call, then one chain, then one tool, then create_agent — and build one agent loop by hand first if you can, because LangChain is much easier once you understand what it automates (advice echoed by the current-era aiseason.tech tutorial).

Is LangChain free to use?

The LangChain library itself is open source and free. You pay only for the model calls you make — or nothing at all if you point init_chat_model at a local model or a provider's free tier (per the aiseason.tech v1.x tutorial's FAQ).

Should I learn LangChain or LangGraph first?

Start with LangChain's create_agent to get a working agent quickly, then learn LangGraph when you need explicit control: branching, retries, long-running state, or human approval. LangChain agents run on LangGraph, so the skills carry over — this is the official docs' own guidance.

How is LangChain 1.x different from older versions?

1.x centers the framework on create_agent plus composable middleware, with init_chat_model as the universal model entry point. The pre-1.0 APIs — initialize_agent, AgentExecutor, LLMChain, ConversationBufferMemory — belong to the old era; tutorials teaching them need translation before they will run.

Do I need LangChain, or can I just call the model API directly?

Call the API directly for one-shot tasks. Reach for LangChain when you need the same model behind swappable providers, structured multi-step pipelines, conversation memory, document retrieval, or tools an agent can call in a loop — the framework earns its keep the moment hand-rolled glue code starts sprawling.

Featured on Twelve Tools Launch Llama Tools Launch your MCP