Build container-based Foundry Agents with Azure AI Projects SDK (HostedAgentDefinition). Use when creating hosted agents with custom container images in Azure AI Foundry.
72%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
Fix and improve this skill with Tessl
tessl review fix ./skills/agents-v2-py/SKILL.mdCompatibility alias of hosted-agents-v2-py; use that ID for new references when no existing contract requires this one. The instruction-only procedure remains local; no executable support files are bundled so existing installations
continue to work offline. This compatibility guide documents the hosted-agent procedure, not an additional capability; older copies may use retired preview names.
Preserve the callable ID when an existing manifest or client configuration uses it.
Modified in AAS on 2026-09-05; original metadata and license notices are retained.
Build container-based hosted agents using HostedAgentDefinition from the Azure AI Projects SDK.
pip install 'azure-ai-projects==2.3.0' azure-identityThese are SDK 2.3.0 instruction fragments aligned with the current Microsoft hosted-agent guide. Validate the installed schema and service support before provisioning; documentation inspection is not a cloud integration test.
export AZURE_AI_PROJECT_ENDPOINT="https://<resource>.services.ai.azure.com/api/projects/<project>"Before creating hosted agents:
azure-ai-projects==2.3.0Use the approved Azure credential flow for the intended tenant/subscription; this sketch uses DefaultAzureCredential:
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
import os
credential = DefaultAzureCredential()
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
)import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
HostedAgentDefinition,
ContainerConfiguration,
ProtocolVersionRecord,
AgentEndpointProtocol,
)client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="my-hosted-agent",
definition=HostedAgentDefinition(
protocol_versions=[
ProtocolVersionRecord(protocol=AgentEndpointProtocol.RESPONSES, version="2.0.0")
],
cpu="1",
memory="2Gi",
container_configuration=ContainerConfiguration(image=os.environ["REVIEWED_CONTAINER_IMAGE"]),
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": os.environ["MODEL_DEPLOYMENT_NAME"]
}
)
)
print(f"Created agent: {agent.name} (version: {agent.version})")versions = client.agents.list_versions(agent_name="my-hosted-agent")
for version in versions:
print(f"Version: {version.version}") # Inspect supported status fields via get_versionInspect the exact returned model for supported fields; a created version is not proof a running container is healthy. Close synchronous client/credential resources through their supported context managers or close() methods.
client.agents.delete_version(
agent_name="my-hosted-agent",
agent_version=agent.version
)| Parameter | Type | Required | Description |
|---|---|---|---|
protocol_versions | list[ProtocolVersionRecord] | Yes | Protocol versions the agent supports |
container_configuration | ContainerConfiguration | Yes for an image deployment | Reviewed registry image reference |
cpu | str | No | CPU allocation (e.g., "1", "2") |
memory | str | No | Memory allocation (e.g., "2Gi", "4Gi") |
environment_variables | dict[str, str] | No | Environment variables for the container |
The protocol_versions parameter specifies which protocols your agent supports:
from azure.ai.projects.models import ProtocolVersionRecord, AgentEndpointProtocol
# RESPONSES protocol - standard agent responses
protocol_versions=[
ProtocolVersionRecord(protocol=AgentEndpointProtocol.RESPONSES, version="2.0.0")
]Available Protocols:
| Protocol | Description |
|---|---|
AgentEndpointProtocol.RESPONSES | Standard response protocol for agent interactions |
Specify CPU and memory for your container:
definition=HostedAgentDefinition(
protocol_versions=[...],
container_configuration=ContainerConfiguration(image=os.environ["REVIEWED_CONTAINER_IMAGE"]),
cpu="2", # 2 CPU cores
memory="4Gi" # 4 GiB memory
)Illustrative resource sizes; verify regional/SKU limits before provisioning:
| Resource | Min | Max | Default |
|---|---|---|---|
| CPU | 0.5 | 4 | 1 |
| Memory | 1Gi | 8Gi | 2Gi |
Configure tool support inside the selected container runtime/framework. The following tool dictionaries describe application-specific intent; do not assume a tools field exists on HostedAgentDefinition or that defining it provisions a tool or grants permission:
tools=[{"type": "code_interpreter"}]tools=[
{"type": "code_interpreter"},
{
"type": "mcp",
"server_label": "my-mcp-server",
"server_url": "https://my-mcp-server.example.com"
}
]tools=[
{"type": "code_interpreter"},
{"type": "file_search"},
{
"type": "mcp",
"server_label": "custom-tool",
"server_url": "https://custom-tool.example.com"
}
]Pass configuration to your container:
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": os.environ["MODEL_DEPLOYMENT_NAME"],
"LOG_LEVEL": "INFO",
"CUSTOM_CONFIG": "value"
}Best Practice: Keep non-secret deployment names and a reviewed immutable image reference in configuration; secrets need the platform's actual protected injection mechanism. Arbitrary environment dictionaries do not establish Key Vault integration.
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
HostedAgentDefinition,
ContainerConfiguration,
ProtocolVersionRecord,
AgentEndpointProtocol,
)
def create_hosted_agent():
"""Create a hosted agent with custom container image."""
client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
agent = client.agents.create_version(
agent_name="data-processor-agent",
definition=HostedAgentDefinition(
protocol_versions=[
ProtocolVersionRecord(
protocol=AgentEndpointProtocol.RESPONSES,
version="2.0.0"
)
],
container_configuration=ContainerConfiguration(image=os.environ["REVIEWED_CONTAINER_IMAGE"]),
cpu="2",
memory="4Gi",
environment_variables={
"AZURE_AI_PROJECT_ENDPOINT": os.environ["AZURE_AI_PROJECT_ENDPOINT"],
"MODEL_NAME": os.environ["MODEL_DEPLOYMENT_NAME"],
"MAX_RETRIES": "3"
}
)
)
print(f"Created hosted agent: {agent.name}")
print(f"Version: {agent.version}")
# Poll get_version status with a deadline before treating deployment as active.
return agent
if __name__ == "__main__":
create_hosted_agent()import os
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from azure.ai.projects.models import (
HostedAgentDefinition,
ContainerConfiguration,
ProtocolVersionRecord,
AgentEndpointProtocol,
)
async def create_hosted_agent_async():
"""Create a hosted agent asynchronously."""
async with DefaultAzureCredential() as credential:
async with AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=credential
) as client:
agent = await client.agents.create_version(
agent_name="async-agent",
definition=HostedAgentDefinition(
protocol_versions=[
ProtocolVersionRecord(
protocol=AgentEndpointProtocol.RESPONSES,
version="2.0.0"
)
],
container_configuration=ContainerConfiguration(image=os.environ["REVIEWED_CONTAINER_IMAGE"]),
cpu="1",
memory="2Gi"
)
)
return agent| Error | Cause | Solution |
|---|---|---|
ImagePullBackOff | ACR pull permission denied | Verify registry permission mode and scoped reader role for the project identity |
InvalidContainerImage | Image not found | Verify image path and tag exist in ACR |
CapabilityHostNotFound | No capability host configured | Create account-level capability host |
ProtocolVersionNotSupported | Invalid protocol version | Use AgentEndpointProtocol.RESPONSES with version "2.0.0" |
latest in productionUse for reviewing or creating an explicitly requested container-based Foundry hosted agent. First confirm image digest, subscription/tenant, region, service availability, permissions and cost scope. Creating agents, granting roles and deleting versions are cloud writes; do them only within the user's authorization.
Given a pinned container image and test project, check SDK model fields and registry pull access, then prepare the create request. Provision only if authorized and record the exact returned version and observed health. Do not delete unrelated versions as routine cleanup. Expected result is a version-specific receipt, not an assumed deploy.
sickn33/agentic-awesome-skills
e8d346b
Also appears in
last in sync Apr 19, 2026
last in sync Apr 19, 2026
last in sync Apr 19, 2026
last in sync Mar 6, 2026
Renamed to: hosted-agents-v2-py
last in sync Mar 6, 2026
last in sync Mar 6, 2026
Renamed to: hosted-agents-v2-py
last in sync Mar 6, 2026
last in sync Mar 6, 2026
Renamed to: hosted-agents-v2-py
last in sync Mar 6, 2026
since Feb 24, 2026
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.