Lesson 16 of 26 · see all lessons
  1. Start here: the exam, Python basics and responsible AI (skill area 1, 40-45%)

    1. 1. Start here: the AI-901 exam, the plan and what you already know
    2. 2. The Python and Azure basics AI-901 expects
    3. 3. Responsible AI: the six principles in practice
  2. AI models: how they work, choosing and deploying (skill area 1)

    1. 4. How generative AI models work
    2. 5. Choosing the right AI model for the job
    3. 6. Deploying a model: deployment options and configuration parameters
  3. AI workloads: text, speech, vision and extraction (skill area 1)

    1. 7. Common AI workloads: from chat and agents to vision and extraction
    2. 8. Text analysis: key phrases, entities, sentiment and summaries
    3. 9. Speech: recognition and synthesis
    4. 10. Computer vision and image generation
    5. 11. Extracting information from documents, images, audio and video
  4. Generative AI apps and agents in Foundry (skill area 2, 55-60%)

    1. 12. Writing system and user prompts
    2. 13. Deploying a model and chatting with it in the Foundry portal
    3. 14. Building a lightweight chat client with the Foundry SDK
    4. 15. Creating and testing a single agent in the Foundry portal
    5. 16. Building a lightweight client app for an agent
  5. Text, speech, vision and extraction apps in Foundry (skill area 2)

    1. 17. Building a text analysis app
    2. 18. Building speech apps: spoken prompts and Azure Speech
    3. 19. Building vision apps: images in prompts and generated images
    4. 20. Extracting information with Content Understanding: documents and forms
    5. 21. Content Understanding for images, audio and video, and a small extraction app
  6. Final review and four mock exams

    1. 22. Final review and exam day: every decision on one page
    2. 23. Mock exam 1
    3. 24. Mock exam 2
    4. 25. Mock exam 3
    5. 26. Mock exam 4
See the leaderboard

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.

[2]

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.

  1. Create an AIProjectClient with the project endpoint and DefaultAzureCredential(), as in lesson 14.
  2. Call get_openai_client(agent_name=...). This gives an OpenAI client already bound to that agent.
  3. Call conversations.create() to start a conversation, the stored history for this customer.
  4. Call responses.create(conversation=..., input=...) to send each message.
  5. Read response.output_text.

The service adds each turn to the conversation. Your code keeps only the conversation's ID.

[18][104][108]

Rendering diagram…
The client is bound to the agent by name. Each response uses the same conversation ID, so the agent sees every earlier turn.

[18][104]

Read the code: a chat client for an agent

python
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
project = 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":
break
response = openai.responses.create(
conversation=conversation.id,
input=question,
)
print("Agent:", response.output_text)
openai.conversations.delete(conversation_id=conversation.id)
Output: a terminal chat with fernhill-support. Ask about Lisbon's pet rules, then "What about trains?", and the agent answers in context, using its tools. Typing quit ends the chat and deletes the conversation.

[18][108][109]

  • 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 an id.
  • In the loop, responses.create(conversation=conversation.id, input=question) sends the new message. There's no model= and no instructions: the agent supplies both.
  • response.output_text is the agent's final text, after any tool calls.
  • openai.conversations.delete(conversation_id=...) removes the stored history when the chat ends.

[18][104][109]

In the code above, which line sends the customer's message to the agent?

Pick one answer.

[18][104]

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?

Pick one answer.

[104]

Lesson 14 code: carries the whole chat history in a list. Lesson 16 code: carries one conversation ID. Picture: Movie-poster parody style: dramatic poster with bold lighting.

Agent client vs model chat client

Exam depth: know it cold.

Chat client (lesson 14)Agent client (this lesson)
Get the clientUse get_openai_client().Use get_openai_client(agent_name=...).
What the call namesmodel= the deployment name.Nothing; the client is bound to the agent.
Instructions and toolsIn your code, each call.Saved in the agent.
HistoryA list you resend, or previous_response_id.A conversation on the service.
Read the replyUse 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.

[18][104]

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.

[104]

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.

[104]

Creating and cleaning up agents in code

python
from azure.ai.projects.models import PromptAgentDefinition
agent = 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)
Output: fernhill-support 2, if a version 1 already existed. create_version makes the agent, or a new version of it. It continues from the client setup above.

[18][108]

Exam depth: recognize it.

  • PromptAgentDefinition(model=..., instructions=...) holds the prompt agent's settings. Here model is 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.

[18][104][105][108][109]

[18][51][104][105]

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()

Pick one answer.

[18][108]

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.

[104]

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.

[104]

FlashcardsIndex cardsFlashcards ⚡

Card 1 of 13

[18][104][105][109]

Worth keeping

  1. get_openai_client(agent_name=...) picks the agent; no model= on the call.
  2. Create one conversation per chat and pass its ID to every responses.create.
  3. output_text is the reply; output lists tool calls too.
  4. The agent holds instructions and tools; the chat client of lesson 14 holds them in code.
  5. 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.

  1. [2]Microsoft Learn: Study guide for Exam AI-901: Microsoft Azure AI Fundamentals
  2. [18]Microsoft Learn: Quickstart: Get started with Microsoft Foundry SDK
  3. [51]Microsoft Learn: What is Microsoft Foundry?
  4. [104]Microsoft Learn: Build with agents, conversations, and responses
  5. [105]Microsoft Learn: Agent development lifecycle
  6. [106]Microsoft Learn: What is Toolbox in Foundry?
  7. [108]Microsoft Learn: Quickstart: Create a prompt agent
  8. [109]Microsoft Learn: Foundry Agent Service feature availability in Azure Government