- Environment setup
- Task overview
- Task 1 — Imports & minimal state
- Task 2 — Simple nodes
- Task 3 — Wiring nodes with edges
- Task 4 — Multi-step flow (outline → draft → review)
- Task 5 — Conditional routing (routers)
- Task 6 — Tool integration (calculator)
- Task 7 — Research agent: combining tools (DDGS + calculator + LLM)
- Architecture diagrams
- Integrating external systems with self-describing interfaces
- Further exploration
- Links & references
ddgs). After installation, optionally run a verification script if you have one.
Activate the virtual environment in every new shell where you run these examples. Use a requirements file or pinned versions in production to ensure reproducible installs.
Task 1 — Understanding imports and basic state definition
Start by importing the core classes from LangGraph and creating a minimal
State type used by the graph runtime.
StateGraphrepresents the workflow and enforces the shape of the shared state.ENDis used to mark termination nodes in more advanced flows.TypedDicthelps document and type-check the keys passed across nodes.
state and return only the partial state updates they produce. Below are two example nodes: greet_node and enhance_node. We also show how to merge returned partial state with the running state (the graph runtime normally handles this merge).
- Nodes return only the fields they update (partial state).
- The graph runtime merges these partial updates into the running state.
StateGraph to compose nodes into directed workflows. The graph runtime invokes nodes following the topology you define via edges and entry points.
greet → enhance. The graph runtime handles ordering and state merging.
Task 4 — Multi-step flow (draft & review)
Workflows often have several transformation steps. The following example shows an outline → draft → review pipeline, where each node adds or refines pieces of the document.
- Encourages single-responsibility nodes.
- Easier debugging and targeted retries.
- State captures intermediate artifacts useful for observability.
Never use
eval on untrusted input in production. Replace it with a safe mathematical expression evaluator or sandboxed execution environment.- Classification of queries (heuristic or LLM-based)
- Conditional routing to specialized tools
- Integration with external search (DuckDuckGo via
ddgs) - Orchestration of tools and LLMs in a single
StateGraph

- Easier discovery of available actions and required inputs
- Reduced brittle, hand-coded adapter logic
- Safer orchestration across heterogeneous systems

- Use machine-readable schemas (OpenAPI, JSON Schema) to let agents discover capabilities.
- Implement authentication, role-based access control, and audit logging.
- Provide clear error semantics so agents can retry or escalate correctly.
- Validate and sanitize inputs; never run untrusted code directly.
- Replace simple heuristics with LLM-based classifiers to improve routing decisions.
- Use safe math parsers (e.g.,
asteval,numexpr, or a dedicated math library) rather thaneval. - Add caching or vector search (RAG) to improve performance and relevance for search-oriented tools.
- Implement observability (tracing, logs, per-node metrics) for reliability and debugging.
- Experiment with multi-agent orchestration and cross-graph communication patterns.
- LangGraph (project) — LangGraph docs (replace with the official docs link as available)
- LangChain — https://langchain.dev/
- DuckDuckGo Search (ddgs) — https://pypi.org/project/ddgs/
- OpenAI API and Chat Models — https://platform.openai.com/docs/