Skip to content

Commit 902994e

Browse files
feat: implement early exit mechanism for SequentialAgent using escalate action
1 parent ba63176 commit 902994e

File tree

2 files changed

+335
-144
lines changed

2 files changed

+335
-144
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from .tool_context import ToolContext
18+
19+
20+
def exit_sequence(tool_context: ToolContext):
21+
"""Exits the sequential execution of agents immediately.
22+
23+
Call this function when you encounter a terminal condition and want to
24+
prevent subsequent agents in the sequence from executing. This will also
25+
stop any remaining events from the current agent.
26+
27+
This tool is specifically designed for use within SequentialAgent contexts.
28+
When called, it sets the escalate flag, which causes the SequentialAgent
29+
to terminate the sequence immediately, preventing both:
30+
- Subsequent events from the current sub-agent
31+
- All remaining sub-agents in the sequence
32+
33+
Use cases:
34+
- A blocking error is encountered that makes further processing impossible
35+
- A definitive answer is found early, making subsequent agents unnecessary
36+
- A security or validation check fails and the workflow must stop
37+
- Resource limits are reached and safe termination is required
38+
39+
Example:
40+
If you're in a sequence of [validator, processor, finalizer] agents,
41+
and the validator finds invalid data, it can call exit_sequence() to
42+
prevent the processor and finalizer from running on bad data.
43+
44+
Args:
45+
tool_context: The context of the current tool invocation.
46+
"""
47+
tool_context.actions.escalate = True
48+
tool_context.actions.skip_summarization = True

0 commit comments

Comments
 (0)