Generative AI Apps and Agents in Foundry · Topic 3 of 4

A Chat Client with the Foundry SDK

AI-901 Foundry SDK: azure-ai-projects and azure-identity, AIProjectClient, calling a deployed model, handling secrets, and handling errors like 429.

  • 5 min
In a nutshell
  • Install azure-ai-projects and azure-identity. DefaultAzureCredential comes from azure-identity.
  • Create an AIProjectClient from the project endpoint and a credential, then get a model client.
  • Use the deployment name in the model argument.

What it is

A chat client is a small program that sends a prompt to a deployed model and prints the reply. With the Foundry SDK for Python, the pattern is the same each time: create a client for your project, get a client for calling models, make the request and read the answer.

Knowing this short pattern is what the exam tests, rather than long code.

Project endpointand a credentialAIProjectClientconnects to the projectModel calldeployment name and inputReplytext from the response

How it works

Packages and sign in

The SDK is in the azure-ai-projects package, and the credential class is in azure-identity.

pip install azure-ai-projects azure-identity
az login

az login signs you in with the Azure CLI, and DefaultAzureCredential then uses that sign in. The same code works later with a managed identity when it runs in Azure, so no key is stored.

A minimal chat call

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

# Format: https://<resource-name>.services.ai.azure.com/api/projects/<project-name>
FOUNDRY_PROJECT_ENDPOINT = "your_project_endpoint"

project = AIProjectClient(
    endpoint=FOUNDRY_PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)
openai = project.get_openai_client()

response = openai.responses.create(
    model="your-deployment-name",
    input="What is the size of France in square miles?",
)
print(response.output_text)

Reading it part by part:

PartWhat it does
AIProjectClient(endpoint=..., credential=...)Connects to your Foundry project
DefaultAzureCredential()Authenticates with your sign in or a managed identity
project.get_openai_client()Gives a client for calling the models deployed in the project
model="your-deployment-name"Chooses the deployment to call
input=The user's prompt
response.output_textThe text of the reply

The code in the quickstart uses the Responses API. Models can also be called with the Chat Completions API, which takes a list of messages with roles and returns the reply in choices[0].message.content.

completion = openai.chat.completions.create(
    model="your-deployment-name",
    messages=[
        {"role": "system", "content": "You answer in one sentence."},
        {"role": "user", "content": "Why is the sky blue?"},
    ],
    max_tokens=200,
)
print(completion.choices[0].message.content)
print(completion.usage.prompt_tokens, completion.usage.completion_tokens, completion.usage.total_tokens)

The usage object reports prompt_tokens, completion_tokens and total_tokens, which you can use to track cost.

Multi turn conversations

A model does not remember earlier calls. There are two ways to keep a conversation going: send the earlier messages again with every request, or use a conversation that the service stores for you and pass its identifier on each call.

Handling secrets

  • Do not put keys, tokens or endpoints with secrets in the source file.
  • Prefer DefaultAzureCredential with Microsoft Entra ID, so there is no key at all.
  • If a key is unavoidable, read it from an environment variable or an Azure Key Vault secret. Do not commit it to the repository, put it in a query string or leave it in a constant.

Handling errors

StatusLikely causeWhat to do
401Missing or wrong credentialCheck sign in, role and key
404The deployment name does not existFix the name or deploy the model
400A malformed requestCheck the body and parameters
429Over the rate limit or quotaWait and retry with exponential backoff

For long jobs, a client that polls for a result in a tight loop causes throttling. Poll at a sensible interval instead.

Checking the output

Test the client with several prompts, including one that is empty or very long. Check that the reply is not empty before you use it, and log token usage so you notice cost growing.

A worked scenario

A developer builds a command line tool that explains error messages. She installs the two packages, signs in with az login and writes a short script. The script creates the project client, gets the model client and sends the user's error text with a system message that asks for a two sentence explanation. She uses the deployment name, not the catalog name, and reads the reply from the response. The endpoint comes from an environment variable. When the tool is later deployed to Azure, it uses a managed identity, and the code does not change.

What to remember for the exam

  • Install azure-ai-projects and azure-identity. DefaultAzureCredential comes from azure-identity.
  • Create an AIProjectClient from the project endpoint and a credential, then get a model client.
  • Use the deployment name in the model argument.
  • Chat Completions returns choices[0].message.content and a usage object with token counts.
  • Keep secrets in environment variables or Key Vault. Handle 429 with retry and backoff.
Exam tip

A question that asks which package provides DefaultAzureCredential is answered with azure-identity.

How the exam asks about this

  • "Which Python package provides DefaultAzureCredential?" azure-identity.
  • "What does the app supply to create the project client?" The project endpoint and a credential.
  • "Where should the API key be read from?" An environment variable or an Azure Key Vault secret.
  • "Which three values does the usage object report?" prompt_tokens, completion_tokens and total_tokens.

Common mistakes

Using the model's catalog name in model=. Use the deployment name.

Hard coding the endpoint and key. Read them from the environment or use a managed identity.

Polling in a tight loop. It causes throttling.

Expecting memory between calls. The app must resend history or use a stored conversation.

Check yourself

  • What two things create an AIProjectClient?

    The project endpoint and a credential, such as DefaultAzureCredential.

  • Which argument selects the model in the call?

    The model argument, set to the deployment name.

  • How should a client respond to HTTP 429?

    Wait and retry with exponential backoff.

Topic 3 of 4 in Generative AI Apps and Agents in Foundry

Test yourself on Generative AI Apps and Agents in Foundry

The full Microsoft AI-901 question bank has 511 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.