Skip to content

Commit e72a964

Browse files
docs: Reflect and Retry Tool Plugin - Update ADK doc according to issue #796 - 3 (#802)
* docs: Add documentation for ReflectAndRetryToolPlugin * docs: add Reflect and Retry plugin page * docs: Reflect and Retry Plugin page * resolve review comments * final review comments * fix broken link --------- Co-authored-by: Joe Fernandez <[email protected]> Co-authored-by: Joe Fernandez <[email protected]>
1 parent 35f00f9 commit e72a964

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

docs/plugins/index.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ related callback functions together to be used across a workflow. This makes
5555
Plugins an ideal solution for implementing features that cut across your entire
5656
agent application.
5757

58+
## Prebuilt Plugins
59+
60+
ADK includes several plugins that you can add to your agent workflows
61+
immediately:
62+
63+
* [**Reflect and Retry Tools**](/adk-docs/plugins/reflect-and-retry/):
64+
Tracks tool failures and intelligently retries tool requests.
65+
* [**BigQuery Logging**](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/bigquery_logging_plugin.py):
66+
Enables agent logging and analysis with BigQuery.
67+
* [**Context Filter**](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/context_filter_plugin.py):
68+
Filters the generative AI context to reduce its size.
69+
* [**Global Instruction**](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/global_instruction_plugin.py):
70+
Plugin that provides global instructions functionality at the App level.
71+
* [**Save Files as Artifacts**](https://github.com/google/adk-python/blob/main/src/google/adk/plugins/save_files_as_artifacts_plugin.py):
72+
Saves files included in user messages as Artifacts.
73+
* [**Logging**](https://github.com/google/adk-python/blame/main/src/google/adk/plugins/logging_plugin.py):
74+
Log important information at each agent workflow callback point.
75+
5876
## Define and register Plugins
5977

6078
This section explains how to define Plugin classes and register them as part of
@@ -191,7 +209,7 @@ Hello world: query is [hello world]
191209

192210

193211
For more information on running ADK agents, see the
194-
[Quickstart](/get-started/quickstart/#run-your-agent)
212+
[Quickstart](/adk-docs/get-started/quickstart/#run-your-agent)
195213
guide.
196214

197215
## Build workflows with Plugins

docs/plugins/reflect-and-retry.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ nav:
231231
- events/index.md
232232
- Plugins:
233233
- plugins/index.md
234+
- Reflect and retry: plugins/reflect-and-retry.md
234235
- MCP:
235236
- mcp/index.md
236237
- A2A Protocol:

0 commit comments

Comments
 (0)