Framework cheatsheet
LangChain 1 Cheatsheet
Python snippets for LangChain 1: models, prompts, agents, tools, structured output, retrieval and tracing.
Setup
- Install LangChain — Install the core package. Add a provider package for the model you use.
- Install a model provider — Install the integration package for Anthropic. Other providers have packages such as langchain-openai.
- Set the provider API key — The integration reads the key from an environment variable. Use your own key and keep it out of source control.
- Turn on LangSmith tracing — Send traces of every run to LangSmith to inspect prompts, tool calls and latency.
Chat models
- Create a chat model — Create a model from a provider:model string, so switching providers is a one-line change.
- Call a model — Send a prompt and get a message back. Read the text with .content.
- Send a conversation — Pass a list of messages, including a system message, instead of a single string.
- Stream the response — Print the answer as it is generated.
- Run several prompts in parallel — Process a list of inputs concurrently.
- Set model parameters — Pass settings such as temperature and a token limit when you create the model.
Prompts and chains
- Create a prompt template — Build a reusable prompt with placeholders.
- Chain a prompt, model and parser — Combine steps with the | operator, then call the chain like any other runnable.
- Get structured output — Make the model return data that matches a Pydantic class.
Agents and tools
- Define a tool — Turn a function into a tool. The docstring tells the model when to use it.
- Create an agent — Create an agent that calls tools in a loop until it can answer. This replaces the older create_react_agent.
- Run an agent — Send a conversation to the agent. The result holds the full list of messages.
- Stream agent steps — Print each step as the agent calls tools and answers.
- Remember the conversation — Add a checkpointer and a thread ID so the agent keeps context between calls.
- Return structured output from an agent — Pass a Pydantic class as response_format and read the parsed result.
Retrieval (RAG)
- Split documents into chunks — Break long text into overlapping chunks that fit an embedding model.
- Create an embeddings model — Create an embedding model from a provider:model string. Needs that provider's package and API key.
- Store chunks in a vector store — Use the in-memory store to experiment. Swap in Chroma, pgvector or another store for real data.
- Search by meaning — Return the four chunks closest to the query.
- Turn a vector store into a retriever — Get a retriever that fits into chains and agents.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- Django 6 cheatsheet — Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- Express 5 cheatsheet — Build HTTP servers and APIs with Express 5 on Node.js.
- FastAPI 0.115 cheatsheet — Install, run, test and deploy FastAPI apps from the command line.