Task 3 — Call your agent from a client app
Part of the Build and extend AI agents lab. New here? Start with Getting started.
Set up (start here): This task needs a Foundry project and the starter code. If you haven’t already, complete Getting started to create your project, clone the code, and set
PROJECT_ENDPOINTinPython/.env.
This task drives a grounded agent. The quickest way to get one is to create it in code —
from the Labfiles/A-build-and-extend-ai-agents folder, run:
python setup/bootstrap_agent.py
That creates and grounds tailwind-agent — including the Code Interpreter tool with the
sales data already attached — and writes AGENT_NAME into your .env. Then verify you’re ready:
python setup/check_env.py --task 3
Already built the agent in Task 1? Use it instead of the script: open your
tailwind-agentin the portal, add the Code interpreter tool with the sales data (step 1 below), and setAGENT_NAME=tailwind-agentin.env.
Goal: Interact with the grounded portal agent from a small web chat app instead of the playground — including charts the agent produces (from code interpreter), which render inline in the chat window.
Concept reinforced: consuming an agent programmatically with the Foundry SDK — loading
an existing agent by name and driving it with the Responses API. A provided UI shell
(tailwind_ui.py) turns your agent into a browser chat app, so you focus on the agent code,
not the interface.
Set up:
If you ran python setup/bootstrap_agent.py above, your agent, its Code Interpreter tool,
and AGENT_NAME are already configured — activate your virtual environment
(.\labenv\Scripts\Activate.ps1) and skip to Try it first.
If you built the agent yourself in Task 1, finish wiring it up:
-
In the portal, open your
tailwind-agent, add a Code interpreter tool, and upload a data file so there’s something to analyze. Download and attach:https://raw.githubusercontent.com/MicrosoftLearning/mslearn-ai-agents/main/Labfiles/A-build-and-extend-ai-agents/Python/weekly_sales.csvSave the agent.
-
In the
Labfiles/A-build-and-extend-ai-agents/Pythonfolder, activate the virtual environment (.\labenv\Scripts\Activate.ps1). Then open .env and addAGENT_NAME=tailwind-agentalongside thePROJECT_ENDPOINTyou already set. Save the file.
Try it first: The
agent_with_functions.pyfile already contains a complete client that launches a web chat window. Before running it, predict: which SDK call loads your existing portal agent by name? How does the client tell the Responses API to use that agent? How does arespond()function turn one message into a reply the UI can show?
Show a solution
The provided agent_with_functions.py already implements the client and hands its
respond() function to the shared run_chat_app() shell. The lines that matter are:
-
Load your portal agent by name (using
AGENT_NAMEfrom .env):agent = project_client.agents.get(agent_name=agent_name) -
Route each request to that agent through the Responses API (inside
respond()):response = openai_client.responses.create( conversation=conversation.id, extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, input="", ) -
Inline charts: helper functions detect image outputs and
container_file_citationannotations, save them underagent_outputs/, and return them in anAgentReplyso the UI renders them inline in the chat. -
Launch the app: the file ends by starting the browser chat window:
run_chat_app(respond, title="Tailwind Traders Assistant")
Sign in and run it:
az login
python agent_with_functions.py
Your browser opens a chat window at http://localhost:7860. Ask for something that uses
code interpreter:
Analyze the weekly sales data and create a chart of revenue over time.
The agent’s analysis appears in the chat and the chart is shown inline. Close the browser tab and press Ctrl+C in the terminal to stop the app.
Stretch: display the agent’s token usage after each response.
Next (optional): Task 4 — Add custom function tools