Framework cheatsheet
LangGraph 1 Cheatsheet
Python snippets and CLI commands for LangGraph 1: state graphs, routing, memory, human approval, streaming and local dev.
Setup
- Install LangGraph — Install the LangGraph library.
- Install the LangGraph CLI — Install the CLI, including the in-memory server used by langgraph dev.
- Create a project from a template — Scaffold a LangGraph app. Choose a template when prompted, or name one with --template.
- Run the local dev server — Start the API server with hot reload and open LangGraph Studio. Run it from the folder with langgraph.json.
- Write langgraph.json — Tell the CLI which graphs your project exposes and where its dependencies and environment file are.
Building graphs
- Define the state — Describe the data that flows through the graph. add_messages appends new messages instead of replacing the list.
- Use the built-in messages state — Use MessagesState when you only need a list of messages.
- Add a node — A node is a function that takes the state and returns the parts of it to update.
- Connect nodes — Edges decide which node runs next. START and END mark the entry and exit.
- Branch with a conditional edge — Pick the next node with a function that returns the name of a node or END.
- Run tools with the prebuilt node — ToolNode runs the tool calls in the last message, and tools_condition routes to it or to the end.
- Update state and route in one step — Return a Command from a node to change the state and choose the next node together.
- Fan out to parallel workers — Return Send objects from a conditional edge to run one node per item, for example map-reduce.
- Compile the graph — Turn the builder into a runnable graph.
Running and streaming
- Run the graph — Run it to completion and get the final state.
- Stream each node's update — Print what every node returns as it finishes.
- Stream model tokens — Stream the model's output token by token, with the metadata about where it came from.
- Limit the number of steps — Stop loops from running forever. LangGraph raises an error when the limit is hit.
- Draw the graph — Print a Mermaid diagram you can paste into a viewer.
Memory and persistence
- Add short-term memory — Compile with a checkpointer to save state after every step. Use InMemorySaver for experiments only.
- Continue a conversation on a thread — Calls that share a thread_id share state.
- Read the saved state — Get the latest state of a thread.
- Browse state history — Iterate over past checkpoints of a thread, newest first. Useful for debugging and time travel.
- Persist to SQLite — Install the SQLite checkpointer. It keeps state across restarts for local use.
- Persist to PostgreSQL — Use PostgreSQL for production. Install the package, then create the tables once with setup().
- Add long-term memory — A store keeps data across threads, such as facts about a user.
Human in the loop
- Pause for human input — Call interrupt inside a node. The graph stops, saves its state and returns the value you passed. Needs a checkpointer.
- Resume after an interrupt — Resume the same thread with a value, which becomes the return value of interrupt().
Deployment
- Build a Docker image — Build an image of your app with the LangGraph CLI.
- Run the app with Docker — Start the API server and its dependencies locally using Docker. Needs a LangSmith license key or API key.
- Generate a Dockerfile — Write a Dockerfile you can customise and build yourself.
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.