Tool Composition Patterns

Orchestrate multiple tools into sophisticated, powerful agent workflows

Conditional 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