|
| 1 | +# Reflect and Retry Tool Plugin |
| 2 | + |
| 3 | +<div class="language-support-tag"> |
| 4 | + <span class="lst-supported">Supported in ADK</span><span class="lst-python">Python v1.16.0</span> |
| 5 | +</div> |
| 6 | + |
| 7 | +The Reflect and Retry Tool plugin can help your agent recover from error |
| 8 | +responses from ADK [Tools](/adk-docs/tools-custom/) and automatically retry the |
| 9 | +tool request. This plugin intercepts tool failures, provides structured guidance |
| 10 | +to the AI model for reflection and correction, and retries the operation up to a |
| 11 | +configurable limit. This plugin can help you build more resilience into your |
| 12 | +agent workflows, including the following capabilities: |
| 13 | + |
| 14 | +* **Concurrency safe**: Uses locking to safely handle parallel tool executions. |
| 15 | +* **Configurable scope**: Tracks failures per-invocation (default) or globally. |
| 16 | +* **Granular tracking**: Failure counts are tracked per-tool. |
| 17 | +* **Custom error extraction**: Supports detecting errors in normal tool responses. |
| 18 | + |
| 19 | +## Add Reflect and Retry Plugin |
| 20 | + |
| 21 | +Add this plugin to your ADK workflow by adding it to the plugins setting of your |
| 22 | +ADK project's App object, as shown below: |
| 23 | + |
| 24 | +```python |
| 25 | +from google.adk.apps.app import App |
| 26 | +from google.adk.plugins import ReflectAndRetryToolPlugin |
| 27 | + |
| 28 | +app = App( |
| 29 | + name="my_app", |
| 30 | + root_agent=root_agent, |
| 31 | + plugins=[ |
| 32 | + ReflectAndRetryToolPlugin(max_retries=3), |
| 33 | + ], |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +With this configuration, if any tool called by an agent returns an error, the |
| 38 | +request is updated and tried again, up to a maximum of 3 attempts, per tool. |
| 39 | + |
| 40 | +## Configuration settings |
| 41 | + |
| 42 | +The Reflect and Retry Plugin has the following configuration options: |
| 43 | + |
| 44 | +* **`max_retries`**: (optional) Total number of additional attempts the system |
| 45 | + makes to receive a non-error response. Default value is 3. |
| 46 | +* **`throw_exception_if_retry_exceeded`**: (optional) If set to `False`, the |
| 47 | + system does not raise an error if the final retry attempt fails. Default |
| 48 | + value is `True`. |
| 49 | +* **`tracking_scope`**: (optional) |
| 50 | + * **`TrackingScope.INVOCATION`**: Track tool failures across a single |
| 51 | + invocation and user. This value is the default. |
| 52 | + * **`TrackingScope.GLOBAL`**: Track tool failures across all invocations |
| 53 | + and all users. |
| 54 | + |
| 55 | +### Advanced configuration |
| 56 | + |
| 57 | +You can further modify the behavior of this plugin by extending the |
| 58 | +`ReflectAndRetryToolPlugin` class. The following code sample |
| 59 | +demonstrates a simple extension of the behavior by selecting |
| 60 | +responses with an error status: |
| 61 | + |
| 62 | +```python |
| 63 | +class CustomRetryPlugin(ReflectAndRetryToolPlugin): |
| 64 | + async def extract_error_from_result(self, *, tool, tool_args,tool_context, |
| 65 | + result): |
| 66 | + # Detect error based on response content |
| 67 | + if result.get('status') == 'error': |
| 68 | + return result |
| 69 | + return None # No error detected |
| 70 | + |
| 71 | +# add this modified plugin to your App object: |
| 72 | +error_handling_plugin = CustomRetryPlugin(max_retries=5) |
| 73 | +``` |
| 74 | + |
| 75 | +## Next steps |
| 76 | + |
| 77 | +For complete code samples using the Reflect and Retry plugin, see the following: |
| 78 | + |
| 79 | +* [Basic](https://github.com/google/adk-python/tree/main/contributing/samples/plugin_reflect_tool_retry/basic) |
| 80 | + code sample |
| 81 | +* [Hallucinating function name](https://github.com/google/adk-python/tree/main/contributing/samples/plugin_reflect_tool_retry/hallucinating_func_name) |
| 82 | + code sample |
0 commit comments