azd), the Python SDK, or the REST API. To learn what guardrails are, the risks they detect, and how to create one, see Guardrails and controls overview.
Prerequisites
- A Microsoft Foundry project.
- A hosted agent, or a container image ready to deploy as one. See Deploy a hosted agent.
-
A guardrail (RAI policy) already created on the Foundry resource, and its full Azure Resource Manager (ARM) resource ID. To create one, see Configure guardrails and controls. The ARM resource ID has this form:
-
For the Azure Developer CLI method: the
azd ai agentextension, version 0.1.38-preview or later. -
For the Python SDK method: the Azure AI Projects client library for Python, version 2.2.0 or later:
How guardrails apply to hosted agents
A hosted agent definition has an optionalrai_config setting with a rai_policy_name field. Set rai_policy_name to the full ARM resource ID of your guardrail’s RAI policy. The platform applies that policy to the agent’s prompts and responses.
When you omit rai_config, the agent runs without a content safety guardrail. When you include rai_config but omit rai_policy_name, the platform applies the default policy, Microsoft.DefaultV2. Provide a custom policy when you need stricter or organization-specific filtering.
Always use the full ARM resource ID for rai_policy_name, not the bare policy name.
Add a guardrail with the Azure Developer CLI
When you useazd, declare the guardrail on the azure.ai.agent service in azure.yaml. Set rai_config.rai_policy_name to the full ARM resource ID of the RAI policy.
-
In your
azure.yaml, addrai_configto the agent service: -
Deploy the agent:
Add a guardrail with the Python SDK
When you create an agent version with the SDK, pass aRaiConfig to the rai_config parameter of HostedAgentDefinition.
Add a guardrail with the REST API
When you create the agent over REST, include arai_config object in the definition.
Verify the guardrail is applied
Get the agent version and confirm thatrai_config.rai_policy_name holds your policy ID.
Test the guardrail at runtime
To confirm that the guardrail filters content, send a prompt that violates your safety policy to the agent’s Responses endpoint. The platform screens the prompt at the input stage and rejects it before the agent runs.HTTP 400 with a content_filter error:
HTTP 200 with the agent’s response. If a harmful prompt isn’t blocked, confirm that the policy referenced by rai_policy_name is configured to filter the relevant content category and severity.
Related content
- Guardrails and controls overview — what guardrails are, the risks they detect, and where they intervene.
- Configure guardrails and controls — create the RAI policy you reference here.
- Deploy a hosted agent — the full deployment workflow for hosted agents.