ExamplescriptintermediateRunnableguided-flow
Recovery: Manages failures and exceptions gracefully in agent workflows.
This component implements retry logic, fallback processes, and error handling to ensure system resilience.
Key Facts
- Level
- intermediate • Agent Building Blocks
- Runtime
- Python • OpenAI API
- Pattern
- Inspectable flow with visible system boundaries
- 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
6-recovery.py
python
"""
Recovery: Manages failures and exceptions gracefully in agent workflows.
This component implements retry logic, fallback processes, and error handling to ensure system resilience.
"""
from typing import Optional
from openai import OpenAI
from pydantic import BaseModel
class UserInfo(BaseModel):
name: str
email: str
age: Optional[int] = None # Optional field
def resilient_intelligence(prompt: str) -> str:
client = OpenAI()
# Get structured output
response = client.responses.parse(
model="gpt-4o",
input=[
{"role": "system", "content": "Extract user information from the text."},
{"role": "user", "content": prompt},
],
text_format=UserInfo,
temperature=0.0,
)
user_data = response.output_parsed.model_dump()
try:
# Try to access age field and check if it's valid
age = user_data["age"]
if age is None:
raise ValueError("Age is None")
age_info = f"User is {age} years old"
return age_info
except (KeyError, TypeError, ValueError):
print("❌ Age not available, using fallback info...")
# Fallback to available information
return f"User {user_data['name']} has email {user_data['email']}"
if __name__ == "__main__":
result = resilient_intelligence(
"My name is John Smith and my email is john@example.com"
)
print("Recovery Output:")
print(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 →