CTCaliberTek
Cheat Sheets

LangChain Cheat Sheet: Models, Tools, Agents, Memory, and RAG (v1)

Published October 13, 2026·Updated October 29, 2026·8 min read
Like SaveLog in to like, save, and track progress.

Introduction

LangChain v1 is built around a standard chat model interface and one function for building agents, create_agent. This cheat sheet lists the calls you use most. LangChain changes quickly, so confirm details in the official documentation for your installed version.

Prerequisites

  • Python 3.10 or newer
  • An API key for a model provider such as Anthropic

Step-by-Step Guide

Step 1: Install and create a model

Install langchain with a provider extra and keep your key in the environment. init_chat_model takes a provider:model string.

pip install -U langchain "langchain[anthropic]" python-dotenv

export ANTHROPIC_API_KEY="your-key"

from langchain.chat_models import init_chat_model
model = init_chat_model("anthropic:claude-opus-5-5", max_tokens=16000)

Step 2: Call the model

invoke returns an AIMessage, so read .text for the answer. stream yields chunks, and batch runs many inputs.

response = model.invoke("In one sentence, what is LangChain?")
print(response.text)

model.invoke([
    {"role": "system", "content": "You are a helpful translator."},
    {"role": "user", "content": "Translate to French: I love programming."},
])

for chunk in model.stream("Why do parrots talk?"):
    print(chunk.text, end="", flush=True)

answers = model.batch(["Define RAG.", "Define an agent."])

Step 3: Structured output

Pass a Pydantic class to with_structured_output and get a validated object back.

from pydantic import BaseModel, Field

class Movie(BaseModel):
    """Details about a movie."""
    title: str = Field(description="The movie's title")
    year: int = Field(description="The release year")

structured_model = model.with_structured_output(Movie)
movie = structured_model.invoke("Tell me about the movie Inception.")
print(movie.title, movie.year)

Step 4: Tools and create_agent

Decorate a typed, documented function with @tool. The docstring is the description the model reads. Pass the model, tools, and a system prompt to create_agent.

from langchain.tools import tool
from langchain.agents import create_agent

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return "It is always sunny in " + city + "!"

agent = create_agent(
    model=model,
    tools=[get_weather],
    system_prompt="You are a helpful assistant. Use tools when they help.",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in Paris?"}]}
)
print(result["messages"][-1].text)

Step 5: Memory with a checkpointer

Pass a checkpointer and a thread_id so the agent remembers the conversation. Use a different thread_id for each conversation or user.

from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(model=model, tools=[], checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "user-1"}}

agent.invoke({"messages": [{"role": "user", "content": "My name is Bob."}]}, config)
reply = agent.invoke({"messages": [{"role": "user", "content": "What is my name?"}]}, config)
print(reply["messages"][-1].text)

Step 6: Structured results and middleware

Pass a schema as response_format to get a validated object under structured_response. Middleware runs around model and tool calls, for example to summarize long histories.

agent = create_agent(model=model, response_format=Movie)
result = agent.invoke(
    {"messages": [{"role": "user", "content": "Tell me about Inception."}]}
)
print(result["structured_response"])

from langchain.agents.middleware import SummarizationMiddleware
agent = create_agent(
    model=model,
    tools=[get_weather],
    middleware=[SummarizationMiddleware(model="anthropic:claude-haiku-5-5",
                                        trigger=("tokens", 4000))],
)

Step 7: Retrieval basics (RAG)

Split documents, embed the chunks, store them in a vector store, and search. InMemoryVectorStore is fine for learning. Check the docs for the exact embedding package you choose.

from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore

# embeddings = <an embeddings object from your provider's package>
vector_store = InMemoryVectorStore(embeddings)
vector_store.add_documents([Document(page_content="Employees get 20 vacation days.")])

results = vector_store.similarity_search("How many vacation days?", k=3)
for doc in results:
    print(doc.page_content)

FAQs

What happened to the old LangChain chains and agent helpers?
Version 1 moved legacy functionality to the langchain-classic package and replaced older agent helpers with create_agent. Check the publication date of any tutorial before copying it.

Summary

Use init_chat_model for models, @tool for tools, create_agent for agents, a checkpointer with a thread_id for memory, response_format for structured results, and a vector store for retrieval.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.