Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

langgraph-example

A single runnable script (main.py) that tours LangGraph's core concepts by building one small graph -- a trip-planning assistant -- and exercising it through five scenarios.

Setup

uv sync
cp .env.example .env   # then edit .env and add your ANTHROPIC_API_KEY

Get a key from https://console.anthropic.com/settings/keys.

Run

uv run main.py          # interactive chat -- type messages yourself
uv run main.py --tour   # scripted walkthrough of all 5 concepts, no typing needed

The default mode is a REPL: type a message, get a reply, and get a real Approve? [y/N] prompt in your terminal if you ask it to book a trip. Try "What's the weather in Tokyo?" or "Book me a trip to Paris."

--tour instead runs five labeled demo scenarios back to back with canned messages, each isolated on its own thread_id -- useful for seeing every concept's raw output without typing anything. Read main.py top to bottom -- it's organized into numbered sections that mirror the concepts below, and section 7 is the interactive chat loop.

What each part of the graph demonstrates

State & reducers -- The graph's shared state is a TypedDict with a messages field annotated with LangGraph's add_messages reducer. Nodes return only the new messages they produce; the reducer appends them to history (and replaces messages that share an ID, e.g. streamed chunks).

Tools -- get_weather, search_activities, and book_trip are plain functions decorated with @tool and bound to the model via .bind_tools(). The model decides when to call them and with what arguments.

Nodes -- A node is just a function State -> partial state update. chatbot calls the LLM; tools is LangGraph's prebuilt ToolNode, which executes whatever tool calls are on the last message.

Conditional edges (routing) -- route_after_chatbot inspects the last message and returns the name of the next node ("tools", "human_approval", or END). There's no special branching primitive in LangGraph beyond this -- just a function and a map of possible destinations.

Human-in-the-loop -- book_trip is treated as a sensitive action. Instead of executing immediately, the router sends it to a human_approval node that calls interrupt(), which pauses the graph and surfaces a payload to the caller. The graph resumes only when re-invoked with Command(resume=...) against the same thread_id -- this is what "approve before this tool runs" looks like in LangGraph. Demo 4 shows both an approval and a denial path.

Persistence / memory -- The graph is compiled with an InMemorySaver checkpointer. Every step is saved against a thread_id passed in config. Reusing a thread_id across invoke() calls gives the graph its full prior history automatically (demo 1); a fresh thread_id starts a clean conversation. graph.get_state(config) (demo 5) reads back what's persisted for a thread, including which node(s) would run next -- handy for debugging or building your own "resume later" / time-travel features.

Streaming -- Demo 3 shows two of LangGraph's stream_mode options: "updates" emits one event per node step (good for showing graph progress), and "messages" emits token-level chunks straight from the LLM (good for a typing-effect UI). Note: the interactive chat (section 7) deliberately does not use stream_mode="messages" -- see the comment above run_chat for why that combination currently corrupts multi-turn tool-calling history with this model's always-on extended thinking.

Swapping the model

By default the script uses claude-sonnet-5. Override it with:

LANGGRAPH_EXAMPLE_MODEL=claude-haiku-4-5-20251001 uv run main.py

Project layout

main.py          the entire example
pyproject.toml   dependencies (langgraph, langchain-anthropic, python-dotenv)
.env.example     template for your ANTHROPIC_API_KEY

About

A single-file tour of LangGraph's core features: state/reducers, tools, conditional routing, persistence, streaming, and human-in-the-loop.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages