Skip to main content
This article shows you how to deploy a Hosted agent in Foundry Agent Service from Python or .NET source code, without building or pushing a container image. You upload a .zip of your code (and optionally your dependencies), and Agent Service either runs it as-is or builds your dependencies for you in the cloud.
For most scenarios, deploy with the Azure Developer CLI (azd) or the Foundry Toolkit for VS Code. These tools do the heavy lifting for you: they package your source, upload it, poll for active, and configure role-based access control automatically. To get started, follow the Quickstart: Deploy your first hosted agent and choose Code (or Source Code (ZIP upload)) when prompted for a deployment method.
Use the SDK and REST procedures in this article when you need to deploy source-code agents programmatically—from the Python SDK or .NET SDK in your own applications, or directly over the REST API for custom tooling, language-agnostic automation, or integration with existing continuous-delivery systems. In this article, you complete the following tasks:
  • Pick a dependency-resolution mode and package your source.
  • Create the agent, wait for it to reach active, and invoke it.
  • Update, version, download, and stream logs for the deployed agent.
If you need full control of the runtime image or you already have a working Dockerfile, use the container-based path: Deploy a hosted agent.

Prerequisites

  • pip from Python 3.13 or later, to package your source locally.
  • The azure-ai-projects version 2.2.0 or later and azure-identity packages.

Supported runtimes

The code_configuration.runtime field in the agent definition accepts the following values. Pick the runtime that matches the binaries in your zip—Linux x86_64 wheels for Python, or the TargetFramework of your dotnet publish output for .NET.

Language version support policy

The Agent Service runtime includes the platform-built container image for each value of code_configuration.runtime. To keep your deployed agents fully supported, Foundry aligns hosted agent language support with end-of-life support for each language. Support ends on the community end-of-support date for the language version. Microsoft might retire a code_configuration.runtime value earlier when platform constraints (such as the underlying base image) require it. For upstream end-of-support schedules, see:

Retirement phase

After a language end-of-life date, you can still create, update, and run hosted agents that use the retired runtime value. However, those agents aren’t eligible for support, new features, or security patches until you upgrade them to a supported runtime by setting a current code_configuration.runtime value and redeploying.

Required permissions

You need the Foundry Project Manager role at the project scope to deploy a hosted agent. This role grants the data-plane permissions to create and update agents, plus the ability to create role assignments for the platform-created agent identity if needed. For a detailed breakdown of the permissions involved, see Hosted agent permissions reference.
The Foundry RBAC roles were recently renamed. Foundry User, Foundry Owner, Foundry Account Owner, and Foundry Project Manager were previously named Azure AI User, Azure AI Owner, Azure AI Account Owner, and Azure AI Project Manager. You might still see the previous names in some places while the rename rolls out. The role IDs and core permissions are unchanged by the rename.
Your agent runs as a platform-assigned managed identity that’s separate from your user identity. This identity can access model inferencing through the project endpoint and session storage by default. For external resources (for example, your own Azure Storage), assign RBAC roles manually to the agent’s Microsoft Entra ID. For more information, see Agent access beyond defaults.

Deployment lifecycle

Every source-code deployment follows the same sequence: package -> create or update -> poll until active -> invoke. The source-code path uses code_configuration in the agent definition. The image-based path uses container_configuration instead. These two options are mutually exclusive on a single version. Choose the path that fits your workflow. If you’re not sure, start with the Azure Developer CLI or VS Code—it’s the recommended path for most customers.

Choose how dependencies are resolved

Before you start, pick a value for code_configuration.dependency_resolution. This choice affects what you put in the zip. For bundled mode, see Package the zip manually for the local build commands.

Firewall requirements for private virtual networks

If you secure your project with a private virtual network, update your network policy to allow outbound connections to the following endpoints before you deploy. All source-code deployments require outbound access to:
  • mcr.microsoft.com
  • *.login.microsoft.com
For network configuration, see Deploy a hosted agent in a virtual network.

Deploy using the Azure Developer CLI or VS Code

The Azure Developer CLI (azd) and the Foundry Toolkit for VS Code automate the full source-code deployment lifecycle—they package your source into a zip, compute the SHA-256, upload it, poll for active, and configure role-based access control for you. These tools are the recommended path for most customers, and the fastest inner loop. For a step-by-step walkthrough, see the Quickstart: Deploy your first hosted agent. Choose Code (or Source Code (ZIP upload)) when the quickstart asks for a deployment method.

Select source-code deployment

When you run azd ai agent init interactively, the tool prompts you to choose a deployment mode. Choose code to deploy from source as a ZIP upload instead of building a container image. Code deployment is the default mode for Python and .NET hosted agents. The Foundry Toolkit for VS Code prompts you for the deployment method in the same way. To select source-code deployment non-interactively, for example, in a CI/CD pipeline, pass --deploy-mode code. This mode requires --runtime and --entry-point, and accepts an optional --dep-resolution value of remote_build (default) or bundled:
After initialization, azd writes the source-code deployment settings to the codeConfiguration field on the azure.ai.agent service in azure.yaml:
Run azd up to provision and deploy. Use --deploy-mode container only when you want to build or reference a container image instead. Use the SDK or REST paths in the following sections when you need to deploy programmatically from your own application or integrate with existing tooling.

Deploy from source code

Select your language or interface. Each tab walks through the same lifecycle: create the agent, poll until it reaches active, invoke it, and download the deployed code.
Use the Python SDK to deploy source-code agents from your own applications or automation. You build the zip yourself and pass its bytes and SHA-256 to the SDK, which uploads it and exposes the same create, poll, invoke, and download operations as the REST API. Code-deployment requires azure-ai-projects version 2.2.0 or later.

Build the zip

The Python SDK uploads a zip that you build. Use the same layout and dependency-resolution rules described in Package the zip manually. The minimal remote_build payload is a flat zip with main.py and requirements.txt at the root.

Create the agent

For the Invocations protocol, set the protocol_versions entry to ProtocolVersionRecord(protocol="invocations", version="1.0.0"). For the Invocations (WebSocket) protocol, use ProtocolVersionRecord(protocol="invocations_ws", version="1.0.0"). For bundled mode, set dependency_resolution="bundled" and ship prebuilt dependencies in the zip. For more information, see Build Linux dependencies locally.

Poll for active

See Poll for active for the full list of status values and how to read the error object on failure.

Invoke the agent

After the version reaches active, bind an OpenAI client to the agent endpoint and call it. This example uses the Responses protocol:
For the Invocations protocol, call the invoke endpoint directly with a bearer token, as shown in Invoke the agent.

Download the deployed zip

Verify exactly what’s deployed by downloading the zip and comparing its SHA-256 against the value you uploaded:
For a complete runnable example, see the Python hosted-agent samples.

Package the zip manually

If you use azd, skip this section—azd builds the zip for you. Read it if you use the REST API, if you switch to bundled dependency resolution, or if you need full control over the upload contents. The zip must be flat at the root—no top-level wrapper folder. Select the tab for your agent’s language.

Python layout (remote build mode)

The service installs dependencies in the cloud from requirements.txt.

Python layout (bundled mode)

You ship prebuilt Linux dependencies in packages/.

Build Linux dependencies locally (bundled, Python)

Use the manylinux2014_x86_64 platform tag so pip downloads Linux wheels even from Windows or macOS.Bash
PowerShell / Windows cmd
--only-binary=:all: forces wheels (no source builds). The --python-version must match the runtime value in the agent definition.
Common packaging mistakes that cause session_creation_failed or ModuleNotFoundError:
  • Wrapping the source in a folder (my-agent/main.py instead of main.py at the root).
  • Including raw .whl files in packages/ instead of extracted modules.
  • Bundling Windows binaries (.pyd, .dll) for a Linux runtime.

Limits

For the supported cpu and memory combinations, see Sandbox sizes.

Troubleshooting

Clean up resources

If you scaffolded the project from the Quickstart with azd, run azd down from the project root to remove the entire provisioned environment. To delete an agent you deployed with the SDK or REST API, use the matching path below.
Deleting an agent removes all of its versions and terminates active sessions. This action can’t be undone.

Next steps

Manage Hosted agent lifecycle