azd ai agent run to start your Microsoft Foundry hosted agent on your local machine and azd ai agent invoke --local to test it without deploying to Azure. You also learn how to set ports, choose an agent in a multi-agent project, override startup commands, and pass local runtime secrets.
Prerequisites
- An initialized hosted agent project. To create one, see Initialize an agent project.
- The azd Foundry extensions installed. For installation steps, see Install the azd Foundry extensions.
- An authenticated Azure Developer CLI session. Run
azd auth loginif needed. - Required language runtimes for your agent, such as Python 3.10+, .NET 8+, or Node.js.
Start the agent
-
Start the agent from your project directory:
This command auto-detects the project type (Python, .NET, Node.js), installs dependencies, and starts the agent server on
localhost:8088. The startup command is read from thestartupCommandproperty inazure.yaml.
Start on a custom port
-
Pass
--portwhen the default port is unavailable:
Start a specific agent
-
If your project defines multiple agents, specify which one to run:
Override the startup command
-
Pass
--start-commandto override the command inazure.yaml:This overrides thestartupCommanddefined in yourazure.yamlservice configuration. ThestartupCommandis the default command used both for local development (azd ai agent run) and for container startup when deployed. See azure.yaml service configuration for details.
Pass environment variables and secrets
A local run reads theenv map declared in the azure.ai.agent service in azure.yaml and resolves any ${VAR} placeholders from the active azd environment. To provide a value, such as an API key, set it as an azd environment variable and reference it in azure.yaml.
-
Set the value in the active
azdenvironment:azdstores environment values in.azure/<env>/.env. The.azuredirectory is gitignored by default, so these values stay out of source control. -
Reference the variable in the
azure.ai.agentservice inazure.yamlso the local run injects it:
.env file at all, store them in a Foundry project connection and reference them with a ${{connections.<name>.credentials.<field>}} placeholder in the env map. The platform resolves the placeholder at runtime. For more information, see Configure environment variables for a hosted agent.
Test with invoke
-
Open a separate terminal and send a message to your running agent:
The
--localflag routes the request tolocalhost:8088instead of the deployed endpoint.
Test protocols with invoke
The protocol your agent uses is defined in theprotocols field of the azure.ai.agent service in azure.yaml. If your agent implements the responses protocol, invoke sends a standard Responses API request. If your agent uses the invocations protocol, the payload is whatever your agent code expects. Use --input-file (-f) to send a custom JSON body:
invocations agents, refer to the sample’s README or inspect the handler or entry point to understand the expected payload structure.
If your agent implements multiple protocols, invoke uses the responses protocol by default. Pass -p (--protocol) with responses or invocations to select one explicitly:
Test with curl
-
Test directly with
curl:
Troubleshoot local development
Related content
- Invoke a hosted agent with the Azure Developer CLI for invoke options, sessions, and file input.
- Inspect a local agent with the Agent Inspector for a browser-based local test UI.
- Configure environment variables for a hosted agent for local and deployed configuration.