4.0 KiB
4.0 KiB
Dynamic Instructions for Agent Transfers - TODO
Current Behavior
Currently, when the supervisor transfers control to an agent:
- ❌ No specific instructions are passed
- ❌ Agent only sees the original user query
- ❌ Agent uses its static, pre-defined prompt
Proposed Enhancement: Dynamic Instructions
Why It Matters
The supervisor often has context about WHY it's transferring to a specific agent. For example:
- "Transfer to network_diag because user mentioned DNS issues - focus on DNS diagnostics"
- "Transfer to cert_checker because certificates might be expiring - check all certs urgently"
Implementation Approach
1. Modify Transfer Tools
def transfer_to_network_diag(instructions: str = "") -> str:
"""Transfer control to network diagnostics agent.
Args:
instructions: Specific guidance for the agent
"""
return f"Successfully transferred to network_diag. Instructions: {instructions}"
2. Update State to Include Instructions
class State(BaseModel):
messages: List[AnyMessage]
next_agent: str = "supervisor"
supervisor_instructions: Optional[str] = None # NEW FIELD
3. Modify Agent Creation to Check for Instructions
def create_network_worker():
return create_react_agent(
model="openai:gpt-4o-mini",
tools=[get_shell_tool()],
prompt="""
{base_prompt}
SUPERVISOR INSTRUCTIONS (if any): {supervisor_instructions}
Always prioritize supervisor instructions when provided.
""",
name="network_diag"
)
4. Update Router Logic
def route_agent(state):
# Extract supervisor instructions from last ToolMessage
last_message = state["messages"][-1]
if isinstance(last_message, ToolMessage) and "Instructions:" in last_message.content:
# Parse and store instructions
instructions = extract_instructions(last_message.content)
state["supervisor_instructions"] = instructions
return state["next_agent"]
Example Flow
-
User Query: "My website is slow"
-
Supervisor Analysis:
"Website slowness could be DNS or certificate related. Let me transfer to network_diag with specific focus."
-
Supervisor Transfer:
transfer_to_network_diag( instructions="Focus on DNS resolution times and latency to common websites. Check if DNS servers are responding slowly." )
-
Network Agent Receives:
- Original query: "My website is slow"
- Supervisor instructions: "Focus on DNS resolution times..."
- Can now prioritize DNS diagnostics over general network checks
Benefits
- More Targeted Diagnostics: Agents focus on what matters
- Better Context Sharing: Supervisor's analysis isn't lost
- Efficient Execution: Avoid running unnecessary commands
- Improved Results: More relevant output for user's specific issue
Alternative: Context in Messages
Instead of modifying tools, append supervisor analysis to the message history:
# Before transfer, supervisor adds a system message
state["messages"].append(
SystemMessage(content=f"[SUPERVISOR GUIDANCE]: Focus on {specific_issue}")
)
Decision Points
- Tool Parameters vs State: Where to store instructions?
- Prompt Injection vs Message History: How to pass instructions?
- Optional vs Required: Should all transfers include instructions?
- Persistence: Should instructions carry through multiple agent hops?
Next Steps
- Decide on implementation approach
- Modify transfer tool signatures
- Update state model
- Enhance agent prompts to use instructions
- Test with various scenarios
- Document the new pattern
Example Test Cases
- "Check network" → No specific instructions needed
- "Website is slow" → "Focus on DNS and latency"
- "Certificate expiring?" → "Check all certs, prioritize those expiring soon"
- "Port 443 issues" → "Focus on HTTPS connectivity and certificate validation"