RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /LangGraph for Local Agents
  6. /Ch. 6
LangGraph for Local Agents

06. Conditional Edges

Chapter 6 of 18 · 15 min
KEY INSIGHT

Conditional edges are the primary mechanism for implementing dynamic workflows. The routing function is a pure state-to-string mapper, not a workflow step itself—keep it focused on routing logic only.

Conditional edges route execution based on the current state rather than running the same next node every time. They require two things: a routing function that receives the state and returns a string (node name), and a mapping dict from possible return values to destination nodes.

from typing import Literal

def route_based_on_confidence(state: AgentState) -> Literal["high_confidence_node", "low_confidence_node"]:
    confidence = state.get("confidence", 0.5)
    if confidence >= 0.7:
        return "high_confidence_node"
    return "low_confidence_node"

builder.add_conditional_edges(
    "evaluator",
    route_based_on_confidence,
    {
        "high_confidence_node": "high_confidence_node",
        "low_confidence_node": "low_confidence_node",
    }
)

The routing function can return any string—it does not have to match a defined node name, but if it returns a name with no node attached, compile() raises InvalidNodeError. Multiple conditional edges can originate from the same node, each with its own routing function—this is how you implement a dispatcher that looks at one field and routes to many possible workers.

A common pattern is to combine conditional edges with a routing function that returns END to conditionally terminate:

def route_or_stop(state: AgentState) -> Literal["continue", "__end__"]:
    if len(state["messages"]) > 10:
        return "__end__"
    return "continue"

A failure mode: conditional edge routing functions must be pure—reading from the state is fine, but writing to it (mutating the state dict) is not and causes non-deterministic behavior. Also, routing functions that raise exceptions leave the graph in an undefined state. Always return a valid node name or __end__.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Build a three-step pipeline: start → conditional branch to path_a or path_b (based on whether a mode field is "fast" or "deep") → aggregator → END. Run both branches from a single invoke() call and confirm the correct path executes each time.

← Chapter 5
Tool-Calling Agent in LangGraph
Chapter 7 →
Human-in-the-Loop