Skip to main content
This guide walks through building stateful AI workflows using LangGraph and related tools. You’ll set up a Python environment, create nodes that transform shared state, connect them into directed graphs, add routers for conditional routing, integrate tools (calculator and web search), and combine everything into a simple research agent. Table of contents
  • 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
Environment setup Prepare a virtual environment and install the runtime dependencies used in these examples: LangGraph, LangChain, an OpenAI wrapper, and the DuckDuckGo search client (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 overview 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.
What to remember
  • StateGraph represents the workflow and enforces the shape of the shared state.
  • END is used to mark termination nodes in more advanced flows.
  • TypedDict helps document and type-check the keys passed across nodes.
Task 2 — Creating simple nodes Nodes are plain Python functions that accept the global 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).
Key points
  • Nodes return only the fields they update (partial state).
  • The graph runtime merges these partial updates into the running state.
Task 3 — Wiring nodes with edges Use StateGraph to compose nodes into directed workflows. The graph runtime invokes nodes following the topology you define via edges and entry points.
This constructs a simple linear workflow: greetenhance. 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 outlinedraftreview pipeline, where each node adds or refines pieces of the document.
Benefits of multi-step flows
  • Encourages single-responsibility nodes.
  • Easier debugging and targeted retries.
  • State captures intermediate artifacts useful for observability.
Task 5 — Conditional routing (routers) Routers enable state-driven branching: inspect the state and return the next node name. This pattern supports dynamic workflows such as choosing between a quick answer or a detailed response.
Routers let you build flexible, branchable workflows driven by runtime state. Task 6 — Tool integration (calculator) Tools are nodes that encapsulate specialized capabilities. The example below demonstrates a simple calculator tool and a detector that decides whether to use it.
Never use eval on untrusted input in production. Replace it with a safe mathematical expression evaluator or sandboxed execution environment.
Task 7 — Research Agent: combining tools (DDGS + calculator + LLM) Combine classification, routing, a calculator tool, and a DuckDuckGo search client to build a small research agent. This example shows how to integrate external tools and orchestrate them with LangGraph.
This research agent demonstrates:
  • 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
A hand-drawn blackboard-style diagram titled "Tech Corp's AI Application" with a central node and arrows pointing to components like Large Language Model, R.A.G. (Retrieval Augmented Generation), vector database, LangChain, LangGraph, prompt engineering, and related modules. The sketch uses white and blue handwriting and simple icons to represent each component.
Integrating external systems with self-describing interfaces TechCorp’s internal AI document assistant works well for internal content, but real-world deployments need access to external systems such as customer databases, ticketing systems, inventory, and third-party APIs. Building a custom adapter for each system quickly becomes costly and brittle. A model-facing, self-describing interface reduces this friction. Instead of exposing raw endpoints tied to low-level implementation details, these interfaces expose machine-readable capability descriptions that agents can query and invoke. Advantages include:
  • Easier discovery of available actions and required inputs
  • Reduced brittle, hand-coded adapter logic
  • Safer orchestration across heterogeneous systems
In practice, a self-describing interface might provide an OpenAPI-like schema, examples of usage, and type-safe I/O contracts the agent can read at runtime to plan its interactions.
A hand-drawn architecture diagram showing a user chatting with "Tech Corp's AI assistant" through a chat app. The assistant's agent links to a vector database and external systems (customer DB, inventory management, APIs) via an intermediary labeled "MCP."
Best practices when integrating external tools
  • 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.
Further exploration
  • 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 than eval.
  • 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.
Links and references Happy building!

Watch Video

Practice Lab