diff --git a/docs/user-guide/concepts/model-providers/amazon-bedrock.md b/docs/user-guide/concepts/model-providers/amazon-bedrock.md index 8d7ac628..31023a02 100644 --- a/docs/user-guide/concepts/model-providers/amazon-bedrock.md +++ b/docs/user-guide/concepts/model-providers/amazon-bedrock.md @@ -369,10 +369,6 @@ For a complete list of input types, please refer to the [API Reference](../../.. Amazon Bedrock supports guardrails to help ensure model outputs meet your requirements. Strands allows you to configure guardrails with your [`BedrockModel`](../../../api-reference/models.md#strands.models.bedrock): - ```python - from strands import Agent - from strands.models import BedrockModel - # Using guardrails with BedrockModel bedrock_model = BedrockModel( model_id="anthropic.claude-sonnet-4-20250514-v1:0", @@ -383,9 +379,10 @@ For a complete list of input types, please refer to the [API Reference](../../.. guardrail_redact_input=True, # Default: True guardrail_redact_input_message="Blocked Input!", # Default: [User input redacted.] guardrail_redact_output=False, # Default: False - guardrail_redact_output_message="Blocked Output!" # Default: [Assistant output redacted.] + guardrail_redact_output_message="Blocked Output!", # Default: [Assistant output redacted.] + guardrail_last_turn_only=False # Default: False - Set to True to evaluate only the last turn ) - + guardrail_agent = Agent(model=bedrock_model) response = guardrail_agent("Can you tell me about the Strands SDK?") @@ -398,10 +395,10 @@ For a complete list of input types, please refer to the [API Reference](../../.. - Input redaction (enabled by default): If a guardrail policy is triggered, the input is redacted - Output redaction (disabled by default): If a guardrail policy is triggered, the output is redacted - Custom redaction messages can be specified for both input and output redactions + - Last turn only (disabled by default): When enabled, only the last conversation turn (most recent user message and the assistant's response to it, if present) is sent to guardrails instead of the full conversation history. This allows conversations to recover after guardrail interventions. {{ ts_not_supported_code("Guardrails are not yet supported in the TypeScript SDK") }} - ### Caching Strands supports caching system prompts, tools, and messages to improve performance and reduce costs. Caching allows you to reuse parts of previous requests, which can significantly reduce token usage and latency. diff --git a/docs/user-guide/safety-security/guardrails.md b/docs/user-guide/safety-security/guardrails.md index 49a12214..25f6d4b1 100644 --- a/docs/user-guide/safety-security/guardrails.md +++ b/docs/user-guide/safety-security/guardrails.md @@ -35,6 +35,7 @@ bedrock_model = BedrockModel( guardrail_id="your-guardrail-id", # Your Bedrock guardrail ID guardrail_version="1", # Guardrail version guardrail_trace="enabled", # Enable trace info for debugging + guardrail_last_turn_only=False, # Set to True to evaluate only the last turn ) # Create agent with the guardrail-protected model @@ -53,6 +54,8 @@ if response.stop_reason == "guardrail_intervened": print(f"Conversation: {json.dumps(agent.messages, indent=4)}") ``` +**Performance Optimization**: Set `guardrail_last_turn_only=True` to evaluate only the most recent conversation turn (user message and assistant's response to it, if present) instead of the full history. This allows conversations to recover after guardrail interventions. See the [Amazon Bedrock documentation](../concepts/model-providers/amazon-bedrock.md#guardrails) for more details. + Alternatively, if you want to implement your own soft-launching guardrails, you can utilize Hooks along with Bedrock's ApplyGuardrail API in shadow mode. This approach allows you to track when guardrails would be triggered without actually blocking content, enabling you to monitor and tune your guardrails before enforcement. Steps: