Edit

Quickstart: Deploy your own code as a hosted agent

Note

Hosted agents are currently in preview.

In Deploy your first hosted agent, you deployed a sample. In this quickstart, you deploy your own Python agent code to Foundry Agent Service.

Prerequisites

Before you begin, you need:

Your project directory should contain at minimum:

my-agent/
├── main.py              # Your agent entry point
└── requirements.txt     # Python dependencies

Choose your protocol

Select the tab that matches your agent's interaction pattern. Responses manages conversation history and is OpenAI-compatible. Invocations gives you full control over request and response schemas.

Tip

Not sure which protocol to use? Start with Responses.

Step 1: Add the hosting library

Add the protocol library to your requirements.txt. The library handles the HTTP server, health checks, and protocol compliance.

azure-ai-agentserver-responses>=1.0.0b7

Step 2: Add the hosting wrapper

Create or update main.py with the hosting wrapper. The examples show the minimal pattern—replace the marked block with your existing agent logic.

import asyncio
from azure.ai.agentserver.responses import (
    CreateResponse,
    ResponseContext,
    ResponsesAgentServerHost,
    TextResponse,
)

app = ResponsesAgentServerHost()


@app.response_handler
async def handler(
    request: CreateResponse,
    context: ResponseContext,
    _cancellation_signal: asyncio.Event,
):
    user_input = await context.get_input_text() or ""

    # ─── YOUR AGENT LOGIC HERE ───
    reply = f"Hello! You said: {user_input}"
    # ─────────────────────────────

    return TextResponse(context, request, text=reply)


app.run()

Note

These examples echo user input to demonstrate the hosting wrapper. Replace the marked block with your model calls, RAG logic, or any agent framework code. For production patterns, see the Python samples and C# samples.

Step 3: Initialize the project

Run azd ai agent init from your agent source directory:

azd ai agent init --protocol responses --deploy-mode code

The interactive flow prompts for:

  • Agent name: Customize the name or accept the default.
  • Foundry Project: Select Use an existing Foundry project or Create a new Foundry project.
  • Subscription: Select your Azure subscription.
  • Location: Select an Azure region.

When complete, you see: AI agent definition added to your azd project successfully!

Step 4: Provision Azure resources

azd provision

This creates the required Azure resources, such as Application Insights.

Step 5: Test the agent locally

azd ai agent run

This command creates a virtual environment, installs dependencies, and launches your agent. It also opens the agent inspector in your browser so you can chat with the agent.

You can also invoke from the CLI in a separate terminal:

azd ai agent invoke --local "Hello from my hosted agent"

Step 6: Deploy to Foundry Agent Service

azd deploy

When the command finishes, the output shows links to the agent playground and the agent endpoint:

Deploying services (azd deploy)

  Done: Deploying service my-agent
  - Agent playground (portal): https://ai.azure.com/.../build/agents/my-agent/build?version=1
  - Agent endpoint: https://ai-account-<name>.services.ai.azure.com/api/projects/<project>/agents/my-agent/versions/1

Step 7: Invoke the deployed agent

azd ai agent invoke "Hello from my hosted agent"

You should see a response within a few seconds.

Clean up resources

Warning

azd down permanently deletes every resource in the resource group, including the Foundry project and hosted agent. If the resource group contains other resources, those are also deleted.

azd down

Troubleshooting

Issue Solution
ModuleNotFoundError: azure.ai.agentserver Verify the protocol library is in requirements.txt and reinstall: pip install -r requirements.txt.
FOUNDRY_PROJECT_ENDPOINT not set Use azd ai agent run (sets it automatically) instead of python main.py. Or add it to your .env file.
Connection refused on local run Ensure no other process is using port 8088.
AuthorizationFailed during deploy You need Foundry Project Manager at project scope.
Agent stuck in provisioning Run azd ai agent show to check status. First deploys can take 2–3 minutes while dependencies install.
azd ai agent init fails Run azd version to verify 1.25.3 or later. Update the extension: azd ext upgrade microsoft.foundry.

For the full permission and role-assignment matrix, see Hosted agent permissions reference.

What you learned

In this quickstart, you:

  • Added one hosting library to your existing agent code.
  • Initialized an azd project from your source directory.
  • Tested locally with azd ai agent run and azd ai agent invoke --local.
  • Deployed to Foundry Agent Service with azd deploy.

Next steps