LangChain
LangChain chains, prompts, memory, RAG pipelines, agents, and tool use patterns.
Chains & LCEL
1 topic
LangChain Expression Language
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Define components
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("human", "{question}"),
])
parser = StrOutputParser()
# Chain with | operator (LCEL)
chain = prompt | llm | parser
# Invoke
response = chain.invoke({"question": "What is LangChain?"})
# Stream
for chunk in chain.stream({"question": "Explain RAG"}):
print(chunk, end="", flush=True)
# Batch
responses = chain.batch([
{"question": "What is Python?"},
{"question": "What is TypeScript?"},
])💡 LCEL pipes (|) compose components lazily — nothing runs until .invoke()
⚡ .stream() enables token-by-token output for better UX
RAG Pipeline
1 topic
Retrieval Augmented Generation
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain_core.runnables import RunnablePassthrough
# 1. Load documents
loader = WebBaseLoader("https://docs.example.com")
docs = loader.load()
# 2. Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)
# 3. Embed and store
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
# 4. RAG chain
def format_docs(docs):
return "\n\n".join(d.page_content for d in docs)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
answer = rag_chain.invoke("What is the rate limit?")💡 chunk_overlap ensures context isn't lost at chunk boundaries
⚡ k=4 retrieves 4 most relevant chunks — tune based on context window size
Related Cheat Sheets
More hands-on references connected to this topic.
Practical Generative AI playbook for building reliable LLM features: prompting, context design, RAG, evaluation, safety, and production operations.
shared topics, same difficulty
Complete guide to building AI agents — architecture, tool use, memory, planning patterns, multi-agent systems, frameworks, evaluation, and production best practices.
shared topics
LangGraph stateful agents, graph nodes, edges, checkpointing, and multi-agent workflows.
shared topics
Essential PyTorch patterns for tensor ops, autograd, model building, training loops, and deployment.
shared topics, same difficulty
Related Articles
Background reading and deeper explanations for this sheet.
RAG vs Fine-Tuning: How to Choose the Right Strategy for Real-World AI Systems
Should you use Retrieval-Augmented Generation (RAG) or fine-tuning for your AI product? This practical guide breaks down when each approach wins, where they fail, and how to combine them effectively. You’ll get decision frameworks, architecture patterns, and real examples you can apply immediately.
keyword overlap
How to Create an Agent Using LangGraph: A Practical Developer Cheat Sheet
Want to build reliable AI agents instead of brittle prompt chains? This practical guide shows you how to create an agent using LangGraph step by step, from architecture and state design to tools, memory, and production hardening. You’ll get beginner-friendly explanations, real-world examples, and runnable code patterns you can adapt immediately.
keyword overlap
Memory in AI Agents: Vector DB vs Structured State (and When to Use Both)
AI agents feel smart only when they can remember the right things at the right time. In this practical guide, you’ll learn the difference between vector database memory and structured state, when each approach shines, and how to combine them in real-world agent workflows. We’ll walk through concrete examples, implementation patterns, and pitfalls to avoid so you can build reliable, context-aware agents.
keyword overlap
Build an Agentic App with FastAPI and Azure AI Foundry: A Practical Beginner-to-Pro Guide
Learn how to design, build, and deploy an agentic application using FastAPI and Azure AI Foundry with practical, real-world examples. This guide walks you from architecture and setup to tool-calling, memory, observability, and production deployment patterns.
keyword overlap