ExamplescriptintermediateRunnableintent-router
Control: Provides deterministic decision-making and process flow control.
This component handles if/then logic, routing based on conditions, and process orchestration for predictable behavior.
Key Facts
- Level
- intermediate • Agent Building Blocks
- Runtime
- Python • OpenAI API
- Pattern
- Model classification with code-owned routing
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
5-control.py
python
"""
Control: Provides deterministic decision-making and process flow control.
This component handles if/then logic, routing based on conditions, and process orchestration for predictable behavior.
"""
from openai import OpenAI
from pydantic import BaseModel
from typing import Literal
class IntentClassification(BaseModel):
intent: Literal["question", "request", "complaint"]
confidence: float
reasoning: str
def route_based_on_intent(user_input: str) -> tuple[str, IntentClassification]:
client = OpenAI()
response = client.responses.parse(
model="gpt-4o",
input=[
{
"role": "system",
"content": "Classify user input into one of three categories: question, request, or complaint. Provide your reasoning and confidence level.",
},
{"role": "user", "content": user_input},
],
text_format=IntentClassification,
)
classification = response.output_parsed
intent = classification.intent
if intent == "question":
result = answer_question(user_input)
elif intent == "request":
result = process_request(user_input)
elif intent == "complaint":
result = handle_complaint(user_input)
else:
result = "I'm not sure how to help with that."
return result, classification
def answer_question(question: str) -> str:
client = OpenAI()
response = client.responses.create(
model="gpt-4o", input=f"Answer this question: {question}"
)
return response.output[0].content[0].text
def process_request(request: str) -> str:
return f"Processing your request: {request}"
def handle_complaint(complaint: str) -> str:
return f"I understand your concern about: {complaint}. Let me escalate this."
if __name__ == "__main__":
# Test different types of inputs
test_inputs = [
"What is machine learning?",
"Please schedule a meeting for tomorrow",
"I'm unhappy with the service quality",
]
for user_input in test_inputs:
print(f"\nInput: {user_input}")
result, classification = route_based_on_intent(user_input)
print(
f"Intent: {classification.intent} (confidence: {classification.confidence})"
)
print(f"Reasoning: {classification.reasoning}")
print(f"Response: {result}")
Related principles
- P1delegationDesign for delegation rather than direct manipulationDesign experiences around the assignment of work, the expression of intent, the setting of constraints, and the review of results, rather than requiring users to execute each step manually.Open principle →
- P5delegationReplace implied magic with clear mental modelsThe product should help users understand what the system can do, what it is currently doing, what it cannot do, and what conditions govern its behaviour.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P10delegationOptimise for steering, not only initiatingThe system should support users not only in starting tasks, but also in guiding, refining, reprioritising, and correcting work while it is underway.Open principle →