Lesson 16 of 26 · see all lessons
Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)
AI models: how they work, choosing and deploying (skill area 1)
AI workloads: text, speech, vision and extraction (skill area 1)
Generative AI apps and agents in Foundry (skill area 2, 55-60%)
Text, speech, vision and extraction apps in Foundry (skill area 2)
- 17. Building a text analysis app
- 18. Building speech apps: spoken prompts and Azure Speech
- 19. Building vision apps: images in prompts and generated images
- 20. Extracting information with Content Understanding: documents and forms
- 21. Content Understanding for images, audio and video, and a small extraction app
Final review and four mock exams
18 min
Building a lightweight client app for an agent
Read and complete a short Python app that talks to your Foundry agent.
By the end of this lesson you can
- Connect a Python client to an existing agent by name
- Create a conversation and send messages to the agent
- Read the agent's reply and its tool calls
- Explain how an agent client differs from a model chat client
From earlier lessons · 1 of 2
A travel agent must answer from a 300-page supplier contract that the team uploads. Which tool should the agent use?
Pick one answer.
From earlier lessons · 2 of 2
An older tutorial creates a thread, adds messages and starts a run. What do current Foundry agents call a thread?
Pick one answer.
Fernhill's agent, fernhill-support, passes every test in the playground. The web team wants to add it to the booking page. They don't want to copy its instructions or tools into their code. They want to call the agent by name and keep each customer's chat separate.
The flow: agent client in five moves
Exam depth: know it cold.
The agent already exists in your project, with its model, instructions and tools. Your client only needs its name.
- Create an AIProjectClient with the project endpoint and
DefaultAzureCredential(), as in lesson 14. - Call
get_openai_client(agent_name=...). This gives an OpenAI client already bound to that agent. - Call
conversations.create()to start a conversation, the stored history for this customer. - Call
responses.create(conversation=..., input=...)to send each message. - Read
response.output_text.
The service adds each turn to the conversation. Your code keeps only the conversation's ID.
Read the code: a chat client for an agent
import osfrom azure.identity import DefaultAzureCredentialfrom azure.ai.projects import AIProjectClientproject = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],credential=DefaultAzureCredential(),)openai = project.get_openai_client(agent_name=os.environ["FOUNDRY_AGENT_NAME"])conversation = openai.conversations.create()while True:question = input("You: ")if question.lower() == "quit":breakresponse = openai.responses.create(conversation=conversation.id,input=question,)print("Agent:", response.output_text)openai.conversations.delete(conversation_id=conversation.id)
- The imports and
AIProjectClient(...)lines are the same as lesson 14. project.get_openai_client(agent_name=...)binds the OpenAI client to your agent. The agent name comes from an environment variable.openai.conversations.create()creates an empty conversation on the service. It returns an object with anid.- In the loop,
responses.create(conversation=conversation.id, input=question)sends the new message. There's nomodel=and no instructions: the agent supplies both. response.output_textis the agent's final text, after any tool calls.openai.conversations.delete(conversation_id=...)removes the stored history when the chat ends.
A developer's agent client answers each question but forgets the previous one. Here is the call:
response = openai.responses.create(
input=question,
)
What is missing?
response = openai.responses.create(
input=question,
)
Pick one answer.
Agent client vs model chat client
Exam depth: know it cold.
| Chat client (lesson 14) | Agent client (this lesson) | |
|---|---|---|
| Get the client | Use get_openai_client(). | Use get_openai_client(agent_name=...). |
| What the call names | model= the deployment name. | Nothing; the client is bound to the agent. |
| Instructions and tools | In your code, each call. | Saved in the agent. |
| History | A list you resend, or previous_response_id. | A conversation on the service. |
| Read the reply | Use response.output_text. | Use response.output_text. |
Decision rule: one model, no tools, simple chat: the chat client. Tools, saved behavior or a stable agent others manage: the agent client.
An agent client can also chain with previous_response_id. A conversation is better when a user returns later, since it lasts across sessions.
See what the agent did
Exam depth: recognize it.
response.output_text gives only the final text. response.output is the full list of output items. Each item has a type:
message: the text the user sees.file_search_call,web_search_call: the agent searched files or the web.function_call: the agent asked your code to run a function, with its arguments.
Loop over response.output to log which tools the agent used. It's the code version of the playground's traces.
You loop over response.output and print each item.type. One item's type is web_search_call. What does that tell you?
Pick one answer.
Creating and cleaning up agents in code
from azure.ai.projects.models import PromptAgentDefinitionagent = project.agents.create_version(agent_name="fernhill-support",definition=PromptAgentDefinition(model=os.environ["FOUNDRY_MODEL_NAME"],instructions="You are Fernhill Travel's support agent. Answer in under 80 words.",),)print(agent.name, agent.version)
Exam depth: recognize it.
PromptAgentDefinition(model=..., instructions=...)holds the prompt agent's settings. Heremodelis needed: it's the deployment the agent will use.project.agents.create_version(agent_name=..., definition=...)creates the agent, or a new version if the name exists.- Create an agent once and reuse it. Don't create a new one on every run.
- Cleanup:
openai.conversations.delete(conversation_id=...)removes a conversation.project.agents.delete_version(agent_name=..., agent_version=...)removes one agent version.
In basic setup, conversations are kept for 60 days, then deleted.
Exam-style practice
Complete the code so the client talks to the existing agent named hotel-helper.
openai = project.get_openai_client(____)
conv = openai.conversations.create()
openai = project.get_openai_client(____)
conv = openai.conversations.create()
Pick one answer.
For the statement, select Yes if it is true. Otherwise, select No. "When a client sends messages with the same conversation ID, the agent can use earlier turns to answer."
Pick one answer.
A hotel chain's web app calls an agent that has file search and a booking API tool. Guests return days later and expect the chat to continue. What should the app store for each guest?
Pick one answer.
Worth keeping
- get_openai_client(agent_name=...) picks the agent; no model= on the call.
- Create one conversation per chat and pass its ID to every responses.create.
- output_text is the reply; output lists tool calls too.
- The agent holds instructions and tools; the chat client of lesson 14 holds them in code.
- Create agents once, reuse them, and delete conversations and versions you no longer need.
On the job
Store each customer's conversation ID next to their session or ticket. Then a returning customer picks up where they left off, and support staff can review the chat later. Keep the agent name in configuration, so you can point the site at a test agent first.
Fernhill's agent now talks to customers on the website. Meanwhile, 4,000 new reviews landed this month, and nobody has read them. Next lesson you build an app that finds the key phrases, names and moods in all of them.
Leaderboard
Loading…
50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.
Embed this practice on your site
Free for classrooms, blogs and study groups. Paste this into any page that accepts HTML.
Report a copyright problem with this lesson
Sources for this lesson (8)
Each fact in the lesson is marked with the number of the page it comes from.
- [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
- [18]Microsoft Learn: Quickstart: Get started with Microsoft Foundry SDK
- [51]Microsoft Learn: What is Microsoft Foundry?
- [104]Microsoft Learn: Build with agents, conversations, and responses
- [105]Microsoft Learn: Agent development lifecycle
- [106]Microsoft Learn: What is Toolbox in Foundry?
- [108]Microsoft Learn: Quickstart: Create a prompt agent
- [109]Microsoft Learn: Foundry Agent Service feature availability in Azure Government