Tool Composition Patterns
Orchestrate multiple tools into sophisticated, powerful agent workflows
Your Progress
0 / 5 completedConditional Logic: Adaptive Tool Selection
Not every workflow is fixed. Sometimes you need to choose different tools based on runtime conditionsβuser permissions, data quality, resource availability, or previous results.
Conditional composition makes agents intelligentβthey adapt their behavior based on context instead of blindly executing the same sequence every time.
Interactive: Conditional Tool Selection
Change user type to see how the agent adapts its tool choices
Select User Type:
Common Conditional Patterns
If-Then-Else
Choose between two tool paths based on condition
Pseudocode:
if (condition) { use_tool_A } else { use_tool_B }
Example: If premium user β advanced_search, else β basic_search
Switch/Case
Select from multiple tools based on categorical value
Pseudocode:
switch (type) { case A: tool1; case B: tool2; default: tool3 }
Example: Payment method: credit β stripe, paypal β paypal_api, crypto β blockchain
Guard Clauses
Skip tools if conditions aren't met
Pseudocode:
if (!precondition) return; run_tool()
Example: If no auth token β skip API call, return cached data
Fallback Chain
Try tools in sequence until one succeeds
Pseudocode:
try tool_A catch β try tool_B catch β use_default
Example: Try premium API β try free API β use cached data
What to Condition On
π€
User Context
β Subscription tier
β User location
β Language preference
β Device type
π
Data Quality
β Completeness
β Freshness
β Confidence score
β Source reliability
βοΈ
System State
β API availability
β Resource usage
β Queue length
β Time of day
π
Previous Results
β Tool success/failure
β Result quality
β Execution time
β Error type
Best Practices for Conditional Logic
Make conditions explicit
Clear logic is easier to debug and maintain
β
Do:
if (user.tier === "premium" && credits > 0)
β Avoid:
if (canAccess())
Provide default fallbacks
Prevents workflow failures from unexpected states
β
Do:
switch (type) { ... default: use_safe_default }
β Avoid:
Unhandled switch cases
Log decision paths
Essential for understanding agent behavior
β
Do:
log("Using premium_tool because: user_tier=premium")
β Avoid:
Silent decision making
Test all branches
Ensure every conditional path works
β
Do:
Test: free, premium, enterprise, expired
β Avoid:
Only testing happy path