LangGraph: Conditional Edge — Basic Routing and Looping

📰 Medium · Python

Learn to implement conditional edges in LangGraph for basic routing and looping, enabling flow-based program decision-making

intermediate Published 18 Apr 2026
Action Steps
  1. Create a RouterState class to define the state for routing
  2. Implement the classify_query function to determine the query type
  3. Use the handle_question and handle_command functions to handle different query types
  4. Apply conditional edges to route the flow based on the query type
  5. Test and refine the routing logic to ensure correct flow decisions
Who Needs to Know This

Software engineers and developers can benefit from this article to improve their skills in implementing conditional edges in LangGraph, while data scientists and AI engineers can apply this knowledge to enhance their AI models

Key Insight

💡 Conditional edges in LangGraph enable programs to make decisions based on the current state, allowing for more dynamic and flexible flow control

Share This
Implement conditional edges in #LangGraph for basic routing and looping! #AI #SoftwareEngineering

Key Takeaways

Learn to implement conditional edges in LangGraph for basic routing and looping, enabling flow-based program decision-making

Full Article

Title: LangGraph: Conditional Edge — Basic Routing and Looping

URL Source: https://medium.com/@chanarachlimbanjerdkul/langgraph-conditional-edge-basic-routing-and-looping-13e7d459aef2?source=rss------python-5

Published Time: 2026-04-18T16:38:01Z

Markdown Content:
# LangGraph: Conditional Edge — Basic Routing and Looping | by Chanarach Limbanjerdkul | Apr, 2026 | Medium

[Sitemap](https://medium.com/sitemap/sitemap.xml)

[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40chanarachlimbanjerdkul%2Flanggraph-conditional-edge-basic-routing-and-looping-13e7d459aef2&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)

Get app

[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)

[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40chanarachlimbanjerdkul%2Flanggraph-conditional-edge-basic-routing-and-looping-13e7d459aef2&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

![Image 1](https://miro.medium.com/v2/resize:fill:32:32/1*dmbNkD5D-u45r44go_cf0g.png)

# LangGraph: Conditional Edge — Basic Routing and Looping

[![Image 2: Chanarach Limbanjerdkul](https://miro.medium.com/v2/resize:fill:32:32/0*PzCC-hVRGwsPIPtd.)](https://medium.com/@chanarachlimbanjerdkul?source=post_page---byline--13e7d459aef2---------------------------------------)

[Chanarach Limbanjerdkul](https://medium.com/@chanarachlimbanjerdkul?source=post_page---byline--13e7d459aef2---------------------------------------)

Follow

4 min read

·

1 hour ago

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F13e7d459aef2&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40chanarachlimbanjerdkul%2Flanggraph-conditional-edge-basic-routing-and-looping-13e7d459aef2&user=Chanarach+Limbanjerdkul&userId=e9b9fba671fd&source=---header_actions--13e7d459aef2---------------------clap_footer------------------)

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F13e7d459aef2&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40chanarachlimbanjerdkul%2Flanggraph-conditional-edge-basic-routing-and-looping-13e7d459aef2&source=---header_actions--13e7d459aef2---------------------bookmark_footer------------------)

Share

ในบทความนี้ เราจะเริ่มเจาะลึกความสามารถที่ “ทรงพลังที่สุด” อย่างหนึ่งของ LangGraph นั่นคือ **Conditional Edge**หรือการที่ Flow ของโปรแกรม “ตัดสินใจเส้นทางเองได้” จาก state ปัจจุบัน

Press enter or click to view image in full size

![Image 3](https://miro.medium.com/v2/resize:fit:700/1*tQyuoHjMEj2D-NG5t6OZog.png)

## 1. Basic Routing (แยกเส้นทางพื้นฐาน)

Basic Routing คือการ “แยก flow” ตามเงื่อนไขบางอย่าง เช่น

* user ถามคำถาม (question) → ไป node A
* user สั่งงาน (command) → ไป node B
* user แค่พูด (statement) → ไป node C

สร้าง State สำหรับใช้งาน

class RouterState(TypedDict):

query: str

query_type: str

response: str
และเตรียม Node

def classify_query(state: RouterState) -> dict:

response = llm.invoke(

f"Classify this query as 'question', 'command', or 'statement'. "

f"Reply with just the word.\n\n{state['query']}"

)

return {"query_type": response.content.lower().strip()}

def handle_question(state: RouterState) -> dict:

response = llm.invoke(f"Answer this question: {state['query']}")

return {"response": f"[Answer] {response.content}"}

def handle_command(
Read full article → ← Back to Reads