Lesson 1 of 2 Building Agent Client Applications

Calling a Foundry Agent from a Python Client

This lesson explains how a lightweight Python client connects to an agent defined in Foundry and holds a conversation, using only the project endpoint and the agent's name.

Learning outcomes

After completing this lesson, you will be able to:

  • Connect a client to an agent with get_openai_client(agent_name=...).
  • Explain how a conversation keeps context across turns.
  • Explain the benefits of keeping the agent definition in Foundry.

Connecting to the agent

The client creates an AIProjectClient for the project endpoint with DefaultAzureCredential, as for a model chat client. It then calls project.get_openai_client(agent_name="<agent-name>"), which returns an OpenAI client bound to the agent. Requests through this client use the agent's model, instructions and tools, so the client does not specify a model or instructions. The identity running the client needs permission to use the agent, such as the Foundry User role, or Foundry Agent Consumer for a client that only interacts with agents.

True or false

Connecting to an agent

Exam skills 2.1.5

Check four statements about connecting a client application to an agent.

Log in to try this activity Interactive activities are free with an account.

Holding a conversation

A conversation stores the messages and tool results of a multi-turn exchange. The client creates one with conversations.create() and passes its ID in each responses.create() call, so follow-up questions keep their context without resending history.

import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

project = AIProjectClient(endpoint=os.environ["PROJECT_ENDPOINT"], credential=DefaultAzureCredential())
agent_client = project.get_openai_client(agent_name="trail-guide")    # the agent's name in Foundry
conversation = agent_client.conversations.create()

while (question := input("You: ")).lower() not in ("quit", "exit"):
    response = agent_client.responses.create(conversation=conversation.id, input=question)
    print("Agent:", response.output_text)
Spot the bug

An agent that forgets

Exam skills 2.1.5

Find the line that makes the agent lose track of the conversation.

Log in to try this activity Interactive activities are free with an account.

Why keep the definition in Foundry

Separating the agent definition from the client has practical benefits. The instructions and tools can be improved in the portal, creating new versions, without redeploying the application; several clients, such as a web app and a Teams bot, share one definition; and testing in the playground exercises exactly the configuration that the client will use.

A complete exchange

A short exchange with an order support agent shows how the pieces fit together. The client creates a conversation and sends the user's first message, Where is order 1042?. The agent uses its tools to look up the order and replies that it was dispatched yesterday. The user then asks When will it arrive?. Because the client sends the same conversation ID, the agent knows that it means order 1042 and answers without the user repeating the number.

The client never sent the agent's instructions or chose a model; both came from the agent definition in Foundry. If the support team later improves the instructions in the portal, the next conversation uses the new version without any change to the client code.

Worked example

What the client must supply

A developer's agent client fails with an error saying that the model parameter is missing. The code creates the OpenAI client with project.get_openai_client() and calls responses.create(input=question).

Without an agent binding, the call is a plain model request and needs a deployment name. Creating the client with get_openai_client(agent_name="...") makes the request run the agent, whose definition supplies the model, instructions and tools. Passing the deployment name would remove the error but bypass the agent's instructions and tools.

Check yourself

Why keep an agent's definition in Foundry rather than in the client code?

Show answer

Changes to the agent's instructions, model or tools take effect without changing or redeploying the client.

Exam Tips

Remember for the exam

  • get_openai_client(agent_name=...) binds requests to the agent definition.
  • conversations.create() plus passing the conversation ID with conversation= keeps multi-turn context.
  • Agent changes in Foundry take effect without changing client code.