|
| 1 | +# A2A Human-in-the-Loop Sample Agent |
| 2 | + |
| 3 | +This sample demonstrates the **Agent-to-Agent (A2A)** architecture with **Human-in-the-Loop** workflows in the Agent Development Kit (ADK). The sample implements a reimbursement processing agent that automatically handles small expenses while requiring remote agent to process for larger amounts. The remote agent will require a human approval for large amounts, thus surface this request to local agent and human interacting with local agent can approve the request. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The A2A Human-in-the-Loop sample consists of: |
| 8 | + |
| 9 | +- **Root Agent** (`root_agent`): The main reimbursement agent that handles expense requests and delegates approval to remote Approval Agent for large amounts |
| 10 | +- **Approval Agent** (`approval_agent`): A remote A2A agent that handles the human approval process via long-running tools (which implements asynchronous approval workflows that can pause execution and wait for human input), this agent is running on a separate A2A server |
| 11 | + |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +``` |
| 16 | +┌─────────────────┐ ┌────────────────────┐ ┌──────────────────┐ |
| 17 | +│ Human Manager │───▶│ Root Agent │───▶│ Approval Agent │ |
| 18 | +│ (External) │ │ (Local) │ │ (Remote A2A) │ |
| 19 | +│ │ │ │ │ (localhost:8001) │ |
| 20 | +│ Approval UI │◀───│ │◀───│ │ |
| 21 | +└─────────────────┘ └────────────────────┘ └──────────────────┘ |
| 22 | +``` |
| 23 | + |
| 24 | +## Key Features |
| 25 | + |
| 26 | +### 1. **Automated Decision Making** |
| 27 | +- Automatically approves reimbursements under $100 |
| 28 | +- Uses business logic to determine when human intervention is required |
| 29 | +- Provides immediate responses for simple cases |
| 30 | + |
| 31 | +### 2. **Human-in-the-Loop Workflow** |
| 32 | +- Seamlessly escalates high-value requests (>$100) to remote approval agent |
| 33 | +- Remote approval agent uses long-running tools to surface approval requests back to the root agent |
| 34 | +- Human managers interact directly with the root agent to approve/reject requests |
| 35 | + |
| 36 | +### 3. **Long-Running Tool Integration** |
| 37 | +- Demonstrates `LongRunningFunctionTool` for asynchronous operations |
| 38 | +- Shows how to handle pending states and external updates |
| 39 | +- Implements proper tool response handling for delayed approvals |
| 40 | + |
| 41 | +### 4. **Remote A2A Agent Communication** |
| 42 | +- The approval agent runs as a separate service that processes approval workflows |
| 43 | +- Communicates via HTTP at `http://localhost:8001/a2a/human_in_loop` |
| 44 | +- Surfaces approval requests back to the root agent for human interaction |
| 45 | + |
| 46 | +## Setup and Usage |
| 47 | + |
| 48 | +### Prerequisites |
| 49 | + |
| 50 | +1. **Start the Remote Approval Agent server**: |
| 51 | + ```bash |
| 52 | + # Start the remote a2a server that serves the human-in-the-loop approval agent on port 8001 |
| 53 | + adk api_server --a2a --port 8001 contributing/samples/a2a_human_in_loop/remote_a2a |
| 54 | + ``` |
| 55 | + |
| 56 | +2. **Run the Main Agent**: |
| 57 | + ```bash |
| 58 | + # In a separate terminal, run the adk web server |
| 59 | + adk web contributing/samples/ |
| 60 | + ``` |
| 61 | + |
| 62 | +### Example Interactions |
| 63 | + |
| 64 | +Once both services are running, you can interact with the root agent through the approval workflow: |
| 65 | + |
| 66 | +**Automatic Approval (Under $100):** |
| 67 | +``` |
| 68 | +User: Please reimburse $50 for meals |
| 69 | +Agent: I'll process your reimbursement request for $50 for meals. Since this amount is under $100, I can approve it automatically. |
| 70 | +Agent: ✅ Reimbursement approved and processed: $50 for meals |
| 71 | +``` |
| 72 | + |
| 73 | +**Human Approval Required (Over $100):** |
| 74 | +``` |
| 75 | +User: Please reimburse $200 for conference travel |
| 76 | +Agent: I'll process your reimbursement request for $200 for conference travel. Since this amount exceeds $100, I need to get manager approval. |
| 77 | +Agent: 🔄 Request submitted for approval (Ticket: reimbursement-ticket-001). Please wait for manager review. |
| 78 | +[Human manager interacts with root agent to approve the request] |
| 79 | +Agent: ✅ Great news! Your reimbursement has been approved by the manager. Processing $200 for conference travel. |
| 80 | +``` |
| 81 | + |
| 82 | +## Code Structure |
| 83 | + |
| 84 | +### Main Agent (`agent.py`) |
| 85 | + |
| 86 | +- **`reimburse(purpose: str, amount: float)`**: Function tool for processing reimbursements |
| 87 | +- **`approval_agent`**: Remote A2A agent configuration for human approval workflows |
| 88 | +- **`root_agent`**: Main reimbursement agent with automatic/manual approval logic |
| 89 | + |
| 90 | +### Remote Approval Agent (`remote_a2a/human_in_loop/`) |
| 91 | + |
| 92 | +- **`agent.py`**: Implementation of the approval agent with long-running tools |
| 93 | +- **`agent.json`**: Agent card of the A2A agent |
| 94 | + |
| 95 | +- **`ask_for_approval()`**: Long-running tool that handles approval requests |
| 96 | + |
| 97 | +## Long-Running Tool Workflow |
| 98 | + |
| 99 | +The human-in-the-loop process follows this pattern: |
| 100 | + |
| 101 | +1. **Initial Call**: Root agent delegates approval request to remote approval agent for amounts >$100 |
| 102 | +2. **Pending Response**: Remote approval agent returns immediate response with `status: "pending"` and ticket ID and serface the approval request to root agent |
| 103 | +3. **Agent Acknowledgment**: Root agent informs user about pending approval status |
| 104 | +4. **Human Interaction**: Human manager interacts with root agent to review and approve/reject the request |
| 105 | +5. **Updated Response**: Root agent receives updated tool response with approval decision and send it to remote agent |
| 106 | +6. **Final Action**: Remote agent processes the approval and completes the reimbursement and send the result to root_agent |
| 107 | + |
| 108 | +## Extending the Sample |
| 109 | + |
| 110 | +You can extend this sample by: |
| 111 | + |
| 112 | +- Adding more complex approval hierarchies (multiple approval levels) |
| 113 | +- Implementing different approval rules based on expense categories |
| 114 | +- Creating additional remote agent for budget checking or policy validation |
| 115 | +- Adding notification systems for approval status updates |
| 116 | +- Integrating with external approval systems or databases |
| 117 | +- Implementing approval timeouts and escalation procedures |
| 118 | + |
| 119 | +## Troubleshooting |
| 120 | + |
| 121 | +**Connection Issues:** |
| 122 | +- Ensure the local ADK web server is running on port 8000 |
| 123 | +- Ensure the remote A2A server is running on port 8001 |
| 124 | +- Check that no firewall is blocking localhost connections |
| 125 | +- Verify the agent.json URL matches the running A2A server |
| 126 | + |
| 127 | +**Agent Not Responding:** |
| 128 | +- Check the logs for both the local ADK web server on port 8000 and remote A2A server on port 8001 |
| 129 | +- Verify the agent instructions are clear and unambiguous |
| 130 | +- Ensure long-running tool responses are properly formatted with matching IDs |
| 131 | + |
| 132 | +**Approval Workflow Issues:** |
| 133 | +- Verify that updated tool responses use the same `id` and `name` as the original function call |
| 134 | +- Check that the approval status is correctly updated in the tool response |
| 135 | +- Ensure the human approval process is properly simulated or integrated |
0 commit comments