Exercise 4 graduates from notebooks to a production-style Python project. You will have three separate files—Documentation Index
Fetch the complete documentation index at: https://mintlify.com/temporalio/edu-ai-workshop-openai-agents-sdk/llms.txt
Use this file to discover all available pages before exploring further.
workflow.py, worker.py, and starter.py—and run two terminal processes simultaneously, exactly as you would in a real deployment. The workflow includes a deliberate 10-second pause so you can kill the worker mid-execution and prove that Temporal resumes from the pause point rather than re-running the triage agent.
Timebox: 15 minutes — Explore and run the complete implementation in
solutions/04_agent_routing/ during the workshop. Then build it yourself in exercises/04_agent_routing/ as optional homework.What You’ll Learn
Triage & Routing Pattern
Design a triage agent that never answers directly—it only detects intent and hands off to the right specialist.
Handoff Pattern
Use the
handoffs parameter in the OpenAI Agents SDK to enable seamless agent-to-agent transitions.Production File Structure
Separate
workflow.py, worker.py, and starter.py mirrors real Temporal applications and enables independent scaling.Two-Terminal Workflow
Run a long-lived worker process and a one-shot starter process as separate OS processes—the standard production pattern.
Goal
Build aRoutingWorkflow that routes any user message to the correct language specialist (French, Spanish, or English) via a triage agent, with the full execution wrapped in Temporal for durability and observability.
Architecture
File Structure
exercises/04_agent_routing/ with TODO markers for you to complete as homework.
Complete Solution Code
How to Run
Terminal 1 — Start the worker
Keep this terminal open. The worker runs indefinitely, polling for tasks.Expected output:
Terminal 2 — Start a workflow
In a new terminal, run the starter with a query in any supported language:Expected output:
Observe the handoff in the Temporal UI
Open the Temporal UI link printed by the starter. In the execution history you will see:
ActivityTaskScheduledandActivityTaskCompletedevents for the triage agent’s LLM call- A
TimerStarted/TimerFiredpair representing the 10-secondworkflow.sleep ActivityTaskScheduledandActivityTaskCompletedevents for the specialist agent’s LLM call
Demonstrate durability (optional, highly recommended)
This step makes Temporal’s durability guarantee concrete:
- Start a new workflow in Terminal 2.
- Watch Terminal 1 for the log line:
⏸️ Pausing for 10 seconds to demonstrate durability... - Press Ctrl+C in Terminal 1 to kill the worker during the pause.
- In the Temporal UI, observe the workflow is still Running—it is waiting at the timer.
- Restart the worker in Terminal 1:
python worker.py - The workflow resumes from the timer. The triage agent call is never re-executed.
- Terminal 2 receives the final response.
Language Routing Examples
- Spanish
- French
- English
- Mixed / Edge Cases
Stretch Goals
Add a German specialist agent
Add a German specialist agent
Create a Test with:
german_agent() function and add it to the triage agent’s handoffs list:python starter.py "Hallo! Wie geht es Ihnen heute?"Add conversation history
Add conversation history
Modify the workflow signature to accept an optional history list for multi-turn conversations:Update the starter to persist the returned history and pass it back on subsequent calls.
Add a fallback agent for unclear language
Add a fallback agent for unclear language
When the triage agent cannot confidently detect a language, route to a general-purpose agent:Update the triage agent’s instructions to include: “If language is unclear or mixed → General Agent” and add
general_agent() to handoffs.