$npx -y skills add sickn33/agentic-awesome-skills --skill agents-v2-pyBuild container-based Foundry Agents with Azure AI Projects SDK (ImageBasedHostedAgentDefinition). Use when creating hosted agents with custom container images in Azure AI Foundry.
| 1 | # Azure AI Hosted Agents (Python) |
| 2 | |
| 3 | Build container-based hosted agents using `ImageBasedHostedAgentDefinition` from the Azure AI Projects SDK. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | pip install azure-ai-projects>=2.0.0b3 azure-identity |
| 9 | ``` |
| 10 | |
| 11 | **Minimum SDK Version:** `2.0.0b3` or later required for hosted agent support. |
| 12 | |
| 13 | ## Environment Variables |
| 14 | |
| 15 | ```bash |
| 16 | AZURE_AI_PROJECT_ENDPOINT=https://<resource>.services.ai.azure.com/api/projects/<project> |
| 17 | ``` |
| 18 | |
| 19 | ## Prerequisites |
| 20 | |
| 21 | Before creating hosted agents: |
| 22 | |
| 23 | 1. **Container Image** - Build and push to Azure Container Registry (ACR) |
| 24 | 2. **ACR Pull Permissions** - Grant your project's managed identity `AcrPull` role on the ACR |
| 25 | 3. **Capability Host** - Account-level capability host with `enablePublicHostingEnvironment=true` |
| 26 | 4. **SDK Version** - Ensure `azure-ai-projects>=2.0.0b3` |
| 27 | |
| 28 | ## Authentication |
| 29 | |
| 30 | Always use `DefaultAzureCredential`: |
| 31 | |
| 32 | ```python |
| 33 | from azure.identity import DefaultAzureCredential |
| 34 | from azure.ai.projects import AIProjectClient |
| 35 | |
| 36 | credential = DefaultAzureCredential() |
| 37 | client = AIProjectClient( |
| 38 | endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], |
| 39 | credential=credential |
| 40 | ) |
| 41 | ``` |
| 42 | |
| 43 | ## Core Workflow |
| 44 | |
| 45 | ### 1. Imports |
| 46 | |
| 47 | ```python |
| 48 | import os |
| 49 | from azure.identity import DefaultAzureCredential |
| 50 | from azure.ai.projects import AIProjectClient |
| 51 | from azure.ai.projects.models import ( |
| 52 | ImageBasedHostedAgentDefinition, |
| 53 | ProtocolVersionRecord, |
| 54 | AgentProtocol, |
| 55 | ) |
| 56 | ``` |
| 57 | |
| 58 | ### 2. Create Hosted Agent |
| 59 | |
| 60 | ```python |
| 61 | client = AIProjectClient( |
| 62 | endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], |
| 63 | credential=DefaultAzureCredential() |
| 64 | ) |
| 65 | |
| 66 | agent = client.agents.create_version( |
| 67 | agent_name="my-hosted-agent", |
| 68 | definition=ImageBasedHostedAgentDefinition( |
| 69 | container_protocol_versions=[ |
| 70 | ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1") |
| 71 | ], |
| 72 | cpu="1", |
| 73 | memory="2Gi", |
| 74 | image="myregistry.azurecr.io/my-agent:latest", |
| 75 | tools=[{"type": "code_interpreter"}], |
| 76 | environment_variables={ |
| 77 | "AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"], |
| 78 | "MODEL_NAME": "gpt-4o-mini" |
| 79 | } |
| 80 | ) |
| 81 | ) |
| 82 | |
| 83 | print(f"Created agent: {agent.name} (version: {agent.version})") |
| 84 | ``` |
| 85 | |
| 86 | ### 3. List Agent Versions |
| 87 | |
| 88 | ```python |
| 89 | versions = client.agents.list_versions(agent_name="my-hosted-agent") |
| 90 | for version in versions: |
| 91 | print(f"Version: {version.version}, State: {version.state}") |
| 92 | ``` |
| 93 | |
| 94 | ### 4. Delete Agent Version |
| 95 | |
| 96 | ```python |
| 97 | client.agents.delete_version( |
| 98 | agent_name="my-hosted-agent", |
| 99 | version=agent.version |
| 100 | ) |
| 101 | ``` |
| 102 | |
| 103 | ## ImageBasedHostedAgentDefinition Parameters |
| 104 | |
| 105 | | Parameter | Type | Required | Description | |
| 106 | |-----------|------|----------|-------------| |
| 107 | | `container_protocol_versions` | `list[ProtocolVersionRecord]` | Yes | Protocol versions the agent supports | |
| 108 | | `image` | `str` | Yes | Full container image path (registry/image:tag) | |
| 109 | | `cpu` | `str` | No | CPU allocation (e.g., "1", "2") | |
| 110 | | `memory` | `str` | No | Memory allocation (e.g., "2Gi", "4Gi") | |
| 111 | | `tools` | `list[dict]` | No | Tools available to the agent | |
| 112 | | `environment_variables` | `dict[str, str]` | No | Environment variables for the container | |
| 113 | |
| 114 | ## Protocol Versions |
| 115 | |
| 116 | The `container_protocol_versions` parameter specifies which protocols your agent supports: |
| 117 | |
| 118 | ```python |
| 119 | from azure.ai.projects.models import ProtocolVersionRecord, AgentProtocol |
| 120 | |
| 121 | # RESPONSES protocol - standard agent responses |
| 122 | container_protocol_versions=[ |
| 123 | ProtocolVersionRecord(protocol=AgentProtocol.RESPONSES, version="v1") |
| 124 | ] |
| 125 | ``` |
| 126 | |
| 127 | **Available Protocols:** |
| 128 | | Protocol | Description | |
| 129 | |----------|-------------| |
| 130 | | `AgentProtocol.RESPONSES` | Standard response protocol for agent interactions | |
| 131 | |
| 132 | ## Resource Allocation |
| 133 | |
| 134 | Specify CPU and memory for your container: |
| 135 | |
| 136 | ```python |
| 137 | definition=ImageBasedHostedAgentDefinition( |
| 138 | container_protocol_versions=[...], |
| 139 | image="myregistry.azurecr.io/my-agent:latest", |
| 140 | cpu="2", # 2 CPU cores |
| 141 | memory="4Gi" # 4 GiB memory |
| 142 | ) |
| 143 | ``` |
| 144 | |
| 145 | **Resource Limits:** |
| 146 | | Resource | Min | Max | Default | |
| 147 | |----------|-----|-----|---------| |
| 148 | | CPU | 0.5 | 4 | 1 | |
| 149 | | Memory | 1Gi | 8Gi | 2Gi | |
| 150 | |
| 151 | ## Tools Configuration |
| 152 | |
| 153 | Add tools to your hosted agent: |
| 154 | |
| 155 | ### Code Interpreter |
| 156 | |
| 157 | ```python |
| 158 | tools=[{"type": "code_interpreter"}] |
| 159 | ``` |
| 160 | |
| 161 | ### MCP Tools |
| 162 | |
| 163 | ```python |
| 164 | tools=[ |
| 165 | {"type": "code_interpreter"}, |
| 166 | { |
| 167 | "type": "mcp", |
| 168 | "server_label": "my-mcp-server", |
| 169 | "server_url": "https://my-mcp-server.example.com" |
| 170 | } |
| 171 | ] |
| 172 | ``` |
| 173 | |
| 174 | ### Multiple Tools |
| 175 | |
| 176 | ```python |
| 177 | tools=[ |
| 178 | {"type": "code_interpreter"}, |
| 179 | {"type": "file_search"}, |
| 180 | { |
| 181 | "type": "mcp", |
| 182 | "server_label": "custom-tool", |
| 183 | "server_url": "https://custom-tool.example.com" |
| 184 | } |
| 185 | ] |
| 186 | ``` |
| 187 | |
| 188 | ### Environment Variables |
| 189 | |
| 190 | Pass configuration to y |