Syllabus Lesson 125 of 239 · Prompt Engineering for AI Engineers
Prompt Engineering for AI Engineers

Task Decomposition & Routing

One giant prompt that tries to do everything does each part worse. The production pattern is to route each request to a specialized handler, and to decompose a big task into ordered sub-steps you run in sequence. Both are plain control flow you write around the model.

Routing classifies an incoming request so the right specialized prompt handles it. Build route(request) that reads free text and returns one of "billing", "technical", "account", or "general" using keyword sets, with general as the fallback when nothing matches:

  • billing: words like refund, charge, invoice, payment, bill
  • technical: words like crash, error, bug, broken
  • account: words like cancel, password, login, account, delete
  • general: anything else

Decomposition breaks a goal into ordered steps. Build decompose(goal) that returns a list of sub-step dicts for the fixed plan plan -> draft -> review:

decompose("write a blog post")
# [{"step": 1, "action": "plan",   "of": "write a blog post"},
#  {"step": 2, "action": "draft",  "of": "write a blog post"},
#  {"step": 3, "action": "review", "of": "write a blog post"}]

Each sub-step is numbered from 1, carries its action, and references the original goal in of so a later stage knows what it is working on. Match on lowercased text so "REFUND" still routes to billing. Press Run to grade.

Your turn

Write route(request) classifying free text into "billing", "technical", "account", or "general" (the fallback) by keyword sets, matching case-insensitively. Write decompose(goal) returning [{"step": n, "action": a, "of": goal}] for the ordered plan -> draft -> review, numbered from 1, each carrying the original goal in of.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output