Step 1
Run Your First Agent
On this page
Exercise 1 β Create and Run Your First Agent
| Phase | Difficulty | Time Estimate |
|---|---|---|
| 1 β Foundations | β Beginner | 15β20 min |
Learning Objectives
By the end of this exercise you will be able to:
- Create an
FoundryChatClientbacked by Entra ID credentials - Use
.as_agent()to define an agent with a name and instructions - Call
agent.run()with a user prompt and read the response viaresult.text
Prerequisites
| Requirement | How to verify |
|---|---|
| Environment setup complete | python3 -c "import agent_framework; print(agent_framework.__version__)" |
| Azure CLI logged in | az account show (should return your subscription) |
.env configured |
Ensure FOUNDRY_PROJECT_ENDPOINT and FOUNDRY_MODEL are set in the repo-root .env |
Background
The 3-Layer Architecture
ββββββββββββββββββββββββββββββββββββ
β Microsoft Foundry Project β β Cloud resource (models, connections, storage)
ββββββββββββββββββββββββββββββββββββ€
β Environment Variables (.env) β β FOUNDRY_PROJECT_ENDPOINT,
β β FOUNDRY_MODEL
ββββββββββββββββββββββββββββββββββββ€
β Your Python Code β β FoundryChatClient β agent β run()
ββββββββββββββββββββββββββββββββββββ
Key Concepts
FoundryChatClientβ The client that connects to your Microsoft Foundry project. It readsFOUNDRY_PROJECT_ENDPOINTandFOUNDRY_MODELfrom environment variables automatically..as_agent(name=..., instructions=...)β Creates a hosted agent with a persona. Thenameidentifies the agent;instructionsdefine its system-level behaviour.agent.run(prompt)β Sends a user message to the agent and returns a result object. Access the agentβs text reply viaresult.text.
Note: Both the credential and the agent client are async resources. Always use
async withto ensure they are properly closed.
Your Task
Open starter.py in this directory. You will see three TODO markers inside async def main(). Fill them in:
Step 1 β Create an AzureCliCredential (TODO 1)
Create an async context manager for AzureCliCredential:
async with AzureCliCredential() as cred:
This authenticates against Azure using the CLI token from az login.
Step 2 β Create the Agent (TODO 2)
Inside the credential block, create an agent using FoundryChatClient:
async with FoundryChatClient(credential=cred).as_agent(
name="venue_specialist",
instructions="You are the Venue Specialist, an expert in venue research and recommendation.",
) as agent:
credential=credpasses the Entra ID credential to the client..as_agent()creates a hosted agent on Foundry with the given name and instructions.- The
async withblock ensures the agent is cleaned up when done.
Step 3 β Run the Agent (TODO 3)
Call agent.run() with a prompt and print the result:
result = await agent.run(
"Plan a corporate holiday party for 50 people on December 6th, 2026 in Seattle"
)
print("Result:\n")
print(result.text)
Hints
Work through the hints progressively β try on your own first!
π‘ Hint 1 β High-level approach
Both `AzureCliCredential()` and the agent must be used as `async with` context managers. Nest them inside `main()`: ```python async withπ‘ Hint 2 β Client and agent creation
`FoundryChatClient` takes a `credential=` parameter. You then chain `.as_agent()` on it to create the agent: ```python FoundryChatClient(credential=cred).as_agent( name="venue_specialist", instructions="You are the Venue Specialist, ...", ) ``` No need to pass the endpoint or model name β those are read from environment variables automatically.π‘ Hint 3 β Near-complete solution
```python async with AzureCliCredential() as cred: async with FoundryChatClient(credential=cred).as_agent( name="venue_specialist", instructions="You are the Venue Specialist, an expert in venue research and recommendation.", ) as agent: result = await agent.run( "Plan a corporate holiday party for 50 people on December 6th, 2026 in Seattle" ) print("Result:\n") print(result.text) ```
β β to navigate between steps