Skip to main content
Use 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 login if 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 the startupCommand property in azure.yaml.
azd ai agent run automatically injects environment variables from your default azd environment, the one you set with azd env select or created during azd ai agent init. This means variables like FOUNDRY_PROJECT_ENDPOINT, AZURE_SUBSCRIPTION_ID, and any values you’ve set with azd env set are available to your agent without manual configuration.See Configure environment variables for a hosted agent for the full list.

Start on a custom port

  • Pass --port when 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-command to override the command in azure.yaml:
    This overrides the startupCommand defined in your azure.yaml service configuration. The startupCommand is 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 the env 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.
  1. Set the value in the active azd environment:
    azd stores environment values in .azure/<env>/.env. The .azure directory is gitignored by default, so these values stay out of source control.
  2. Reference the variable in the azure.ai.agent service in azure.yaml so the local run injects it:
For secrets that shouldn’t live in a local .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 --local flag routes the request to localhost:8088 instead of the deployed endpoint.

Test protocols with invoke

The protocol your agent uses is defined in the protocols 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:
For 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