Please read this first
Related Issues
This feature addresses the concerns raised in:
Both issues highlight that with parallel execution, tools may start executing before the guardrail triggers, leading to unnecessary token consumption and potential security concerns. The run_in_parallel=False option provides a solution by ensuring guardrails complete before any agent processing begins.
Describe the feature
Feature Request: Configurable execution mode for input guardrails
Currently, input guardrails run in parallel with the agent's execution. While this provides low latency, it can lead to unnecessary token costs when a guardrail triggers after the agent has already started processing and making tool calls (as reported in #889 and #991).
Proposed Solution:
Add a run_in_parallel boolean parameter to InputGuardrail that allows developers to choose between:
- Parallel execution (default,
run_in_parallel=True): Guardrail runs concurrently with the agent for minimal latency
- Blocking execution (
run_in_parallel=False): Guardrail runs and completes before the agent starts, preventing any token consumption or tool execution if the guardrail triggers
Use Cases:
Example Usage:
from agents import Agent, input_guardrail, InputGuardrail
# Using decorator with blocking execution
@input_guardrail(run_in_parallel=False)
async def content_policy_check(input_text: str):
if contains_prohibited_content(input_text):
raise TripwireTriggered("Content policy violation")
# Using InputGuardrail directly
guardrail = InputGuardrail(
guardrail_function=validate_input,
run_in_parallel=False # Block agent until validation completes
)
agent = Agent(
name="MyAgent",
input_guardrails=[guardrail],
tools=[FileSearchTool()], # Tools won't execute if guardrail triggers
# ...
)
Benefits:
Implementation Notes:
- Only applies to
InputGuardrail, not OutputGuardrail (which always runs after agent completion)
- Works in both streaming and non-streaming modes
- When blocking guardrails raise
TripwireTriggered, the agent never starts and no tools are executed
Please read this first
Related Issues
This feature addresses the concerns raised in:
Both issues highlight that with parallel execution, tools may start executing before the guardrail triggers, leading to unnecessary token consumption and potential security concerns. The
run_in_parallel=Falseoption provides a solution by ensuring guardrails complete before any agent processing begins.Describe the feature
Feature Request: Configurable execution mode for input guardrails
Currently, input guardrails run in parallel with the agent's execution. While this provides low latency, it can lead to unnecessary token costs when a guardrail triggers after the agent has already started processing and making tool calls (as reported in #889 and #991).
Proposed Solution:
Add a
run_in_parallelboolean parameter toInputGuardrailthat allows developers to choose between:run_in_parallel=True): Guardrail runs concurrently with the agent for minimal latencyrun_in_parallel=False): Guardrail runs and completes before the agent starts, preventing any token consumption or tool execution if the guardrail triggersUse Cases:
Example Usage:
Benefits:
True, maintaining current behaviorImplementation Notes:
InputGuardrail, notOutputGuardrail(which always runs after agent completion)TripwireTriggered, the agent never starts and no tools are executed