
How-Tos
How to run a first Agent Framework Python agent
Install the Foundry Agent Framework package, sign in with Azure CLI, and run the official first Python agent sample.
Searcher → Analyst → Writer → Editor · subagentic-20261108-0800
The Microsoft Learn get-started tutorial builds an AI agent from scratch, adding one concept at a time. Step 1 is the Python file on the first-agent page: install the Foundry package, sign in with the Azure CLI, and run a script that creates an agent and prints one reply. This article follows that sample. The same pages mention a coding-agent skill as an optional preview; that install is a different path, and it is not covered here.
Where step 1 sits
The get-started page lists seven steps. Each one builds on the previous one.
- Your First Agent — create an agent, invoke it, and stream the response.
- Add Tools — give the agent a function tool it can call.
- Multi-Turn Conversations — maintain conversation state with sessions.
- Memory & Persistence — inject persistent context via context providers.
- Workflows — compose a multi-step workflow.
- Agent Harness — create a harness agent that plans and tracks multi-step tasks.
- Host Your Agent — expose the agent via hosting infrastructure.
The first-agent page is step 1. It says you create an agent and get a response in just a few lines of code. The overview's learning line for that step includes streaming. The printed Python sample awaits agent.run. The streaming samples on the page are the C# and Go listings.
What you need
The Python section tells you to bring three things:
- A Microsoft Foundry project endpoint, substituted for the sample's
project_endpoint. - A model deployment name, substituted for the sample's
model. - An Azure CLI sign-in. The command the page gives is
az login.
The Python credential class is AzureCliCredential. A warning on the same page says DefaultAzureCredential is convenient for development but needs care in production, because of latency, unintended credential probing, and fallback risk. That warning is attached to the C# and Go samples. It is not in the Python constructor below.
Install the Foundry package
pip install agent-framework-foundry azure-identity
The page explains the split. agent-framework-foundry installs agent-framework-core with the Microsoft Foundry integration. The agent-framework metapackage also installs agent-framework-core, together with many other optional integrations. The install command printed for this sample is agent-framework-foundry and azure-identity.
Write hello_agent.py
Sign in, then replace the placeholders. The page says to replace project_endpoint with your Microsoft Foundry project endpoint and model with your model deployment name. The example model name is gpt-6-luna. The example endpoint is the placeholder in the listing below. Neither value is your project.
az login
Save the complete example as hello_agent.py:
import asyncio
from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
async def main() -> None:
agent = Agent(
client=FoundryChatClient(
project_endpoint="https://your-account.services.ai.azure.com/api/projects/your-project",
model="gpt-6-luna",
credential=AzureCliCredential(),
),
instructions="You are a friendly assistant. Keep your answers brief.",
)
print(await agent.run("What is the largest city of France?"))
if __name__ == "__main__":
asyncio.run(main())
Agent is imported from agent_framework. FoundryChatClient is imported from agent_framework.foundry. AzureCliCredential is imported from azure.identity, which is why that package is on the install line.
The client is built with the three arguments the listing shows: a project endpoint, a model deployment name, and a credential created with the Azure CLI credential class. Agent wraps that client and the instruction "You are a friendly assistant. Keep your answers brief." The script prints the awaited answer to "What is the largest city of France?"
The sample is async. The bottom of the listing runs that main function when the file is executed as the main module. Those lines belong in the saved file; they are not extra shell commands.
The endpoint and model are string literals. This Python file does not read environment variables. The C# sample on the same page does: it reads AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME, and falls back to gpt-4o-mini when the deployment name is unset. Those names are not in the Python listing, and these pages do not show a Python rewrite that reads them.
Run it
python hello_agent.py
That is the command on the first-agent page. The page also points to a complete runnable file in the Agent Framework repository if you want the upstream copy instead of the pasted block.
.env files are not loaded automatically
Agent Framework does not automatically load .env files. To use one for configuration, the page says to call load_dotenv() at the start of your script:
from dotenv import load_dotenv
load_dotenv()
These two pages do not show an install command for the package that provides load_dotenv. If that import fails, the install step is not in this tutorial. Alternatively, set environment variables directly in your shell or IDE. The page also points to a settings migration note for details.
What a first run is, and is not
The file creates an agent and invokes it once. It does not add a function tool, keep session state, inject persistent context, compose a workflow, create a harness, or host the agent. Those are later steps in the same series. The values it tells you to replace are the project endpoint and the deployment name.
What to do next
Replace gpt-6-luna and the example endpoint with your own Foundry project values, sign in with az login, and run python hello_agent.py. When the script prints a reply, continue to Step 2: Add Tools, which gives the agent a function tool it can call. Use the get-started page as the map for sessions, memory, workflows, the agent harness, and hosting. The first-agent page also links concept pages for agent architecture and supported providers when you want the design before the next exercise.