Task 6 — Promote your assistant to a hosted agent
Part of the Build and extend AI agents lab. New here? Start with Getting started.
Set up (start here): This task deploys code, so it needs a Foundry project, a deployed model, and the Azure Developer CLI (
azd). If you haven’t already, complete Getting started to create your project and setPROJECT_ENDPOINTandMODEL_DEPLOYMENT_NAMEinPython/.env. Then, from thePythonfolder you opened in VS Code, verify:
python ../setup/check_env.py --task 6
You also need
azd1.25.3 or later and the Foundry extension. Install it once with:azd ext install microsoft.foundry
Continuing from a previous task? The hosted agent lives in its own folder (
Python/hosted_agent/) with its own dependencies, so it doesn’t reuse the sharedlabenv. Everything you need is below — you can start here without having finished the earlier tasks.
Goal: implement and deploy the Caldova assistant as a hosted agent. Your own code runs in Foundry Agent Service and can be invoked by reference like the prompt agents you built earlier.
This is the same assistant role and business scenario, implemented in a different way. The hosted version keeps the Caldova instructions and conversation history while this task focuses on request handling and deployment. Policy grounding and tools from the earlier tasks are outside the scope of this hosted implementation.
A prompt agent is defined with a model, instructions, and tools. A hosted agent runs your own code in a managed container.
What is the difference between prompt agents and hosted agents?
A prompt agent is declarative: you provide a model, instructions, and tools, and Foundry runs the agent. A hosted agent packages your own framework or Python code in a container, giving you more control over runtime behavior. Foundry manages deployment, scaling, session state, identity, and the endpoint. Hosted agents are useful when the logic no longer fits in a prompt-and-tools definition.
In this task you use the Responses protocol, so your hosted agent stays OpenAI-compatible — the same client code that called your prompt agents can call this one.
Review the agent code
-
In the
Labfiles/A-build-and-extend-ai-agents/Python/hosted_agentfolder, open main.py. Theazure-ai-agentserver-responseshosting library runs the web server, health checks, and conversation history for you — you only write the handler that answers one turn. -
Two blocks are marked
TODO: create the Responses client, and call the model from the handler. Fill them in.
Try it first: the handler already has the user’s message (
user_input) and the conversation history assembled intoinput_items. How would you send that to your model deployment and return the reply? (Hint: the Responses client’screate(...)is synchronous, so the sample runs it off the event loop withrun_in_executorto avoid blocking the server.)
Show a solution
Create the Responses client near the top of the file:
_responses_client = (
AIProjectClient(endpoint=_endpoint, credential=DefaultAzureCredential())
.get_openai_client()
.responses
)
Then complete the handler to call the model with the Caldova system prompt and return the reply:
response = await asyncio.get_running_loop().run_in_executor(
None,
lambda: _responses_client.create(
model=_model,
instructions=_SYSTEM_PROMPT,
input=input_items,
store=False,
),
)
return TextResponse(context, request, text=response.output_text)
The complete file is in Solution/Python/hosted_agent/main.py.
Configure the model
- Copy
hosted_agent/.env.exampletohosted_agent/.envand setAZURE_AI_MODEL_DEPLOYMENT_NAMEto your deployed model name (for example,gpt-4o). In a hosted containerFOUNDRY_PROJECT_ENDPOINTis injected for you;azd ai agent runsets it automatically when you test locally.
Initialize the azd project
-
From the
hosted_agentfolder, scaffold the agent definition. This generates anazure.yamldescribing a hostedazure.ai.agentservice:azd ai agent init --protocol responses --deploy-mode codeAnswer the prompts: pick an agent name (for example,
caldova-hosted-agent), select Use an existing Foundry project (the one from Getting started), and choose your subscription and location.
A completed
azure.yamlis included inSolution/Python/hosted_agent/so you can see what the tool produces.--deploy-mode codemeans Foundry builds the container for you (a remote build) — you don’t need Docker installed locally.
Provision and test locally
-
Provision the supporting resources (such as Application Insights):
azd provision -
Run the agent locally. This creates a virtual environment, installs
requirements.txt, launches your handler, and opens the agent inspector in your browser:azd ai agent run -
Chat with it in the inspector, or invoke it from a second terminal:
azd ai agent invoke --local "What information should a planner gather before moving production to another factory?"
Deploy to Foundry Agent Service
-
Build and deploy the container to Foundry:
azd deployWhen it finishes, the output includes an agent playground link and an agent endpoint. Your hosted agent now has its own dedicated endpoint and identity.
-
Invoke the deployed agent:
azd ai agent invoke "Caldova can move work between factories or hire an approved manufacturing partner. Summarize the factors the planning team should consider."
Same reference, your code now: a hosted agent is invoked exactly like the prompt agents you built — by name, through the OpenAI-compatible client. An app that uses
agent_referencecan point at this hosted agent. The logic answering each turn is now your code running in a container, not a prompt definition.
✅ Checkpoint: You implemented the Caldova assistant as a hosted agent, tested it locally with
azd ai agent run, deployed it withazd deploy, and invoked the deployed agent by name.
Clean up
When you’re done, remove everything this task created:
azd down
Warning:
azd downdeletes every resource in the resource group, including the Foundry project and hosted agent. If the group holds other resources, those are deleted too.
Back to the lab overview.