100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

LangChain Cheat Sheet

LangChain Cheat Sheet

Build LLM applications with chains, agents, tools, retrievers, and LangGraph orchestration using the modern LangChain Expression Language.

3 PagesIntermediateJan 22, 2026

Build a Chain with LCEL

Compose a prompt, model, and output parser using the pipe operator.

python
from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_anthropic import ChatAnthropicprompt = ChatPromptTemplate.from_messages([    ("system", "You are a concise technical assistant."),    ("human", "{question}"),])model = ChatAnthropic(model="claude-sonnet-4-5", temperature=0)chain = prompt | model | StrOutputParser()result = chain.invoke({"question": "What is a monad?"})print(result)

RAG Chain with a Retriever

Wire a vector store retriever into a chain that grounds answers in retrieved context.

python
from langchain_community.vectorstores import Chromafrom langchain_core.runnables import RunnablePassthroughretriever = Chroma(persist_directory="./db", embedding_function=embeddings).as_retriever(k=4)def format_docs(docs):    return "\n\n".join(d.page_content for d in docs)rag_chain = (    {"context": retriever | format_docs, "question": RunnablePassthrough()}    | prompt    | model    | StrOutputParser())answer = rag_chain.invoke("How does the refund policy work?")

Tool-Calling Agent

Bind Python functions as tools and let the model decide when to call them.

python
from langchain_core.tools import toolfrom langgraph.prebuilt import create_react_agent@tooldef get_weather(city: str) -> str:    """Look up the current weather for a city."""    return f"It is sunny in {city}"agent = create_react_agent(model, tools=[get_weather])response = agent.invoke({"messages": [("human", "What's the weather in Austin?")]})print(response["messages"][-1].content)

Streaming Responses

Stream tokens from a chain instead of waiting for the full completion.

python
for chunk in chain.stream({"question": "Summarize the CAP theorem."}):    print(chunk, end="", flush=True)# async streamingasync for chunk in chain.astream({"question": "Summarize the CAP theorem."}):    print(chunk, end="", flush=True)

Core Building Blocks

The primary abstractions you compose to build a LangChain application.

  • Runnable- unified interface (invoke/stream/batch) implemented by prompts, models, parsers
  • PromptTemplate / ChatPromptTemplate- parameterized prompt with variable substitution
  • Retriever- fetches relevant documents given a query string
  • Memory / checkpointer- persists conversation state across turns (LangGraph)
  • Tool- a callable the model can invoke with structured arguments
  • LangGraph- graph-based orchestration for stateful, multi-step agents
Pro Tip

Prefer LangGraph's create_react_agent over the older AgentExecutor for anything new — it gives you explicit state, checkpointing, and human-in-the-loop interrupts that the legacy agent classes never supported well.

Was this cheat sheet helpful?

Explore Topics

#LangChain#LangChainCheatSheet#DataScience#Intermediate#BuildAChainWithLCEL#RAGChainWithARetriever#ToolCallingAgent#StreamingResponses#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet