Build a Chat App with the Foundry SDK
30-second answer
A lightweight chat client uses the Foundry SDK (azure-ai-projects) in a few lines of Python. You create an AIProjectClient with your project endpoint (read from an environment variable) and DefaultAzureCredential, call get_openai_client(), then send prompts with responses.create, passing your deployment name as model. To keep a multi-turn chat, you create a conversation and pass its id each turn, or pass previous_response_id.
Exam objective: 2.1.3 Create a lightweight chat client application by using the Foundry SDK. (Skills measured as of April 15, 2026)
Simulations
Feel it before you read it. No account needed, nothing leaves this page.
Copy the <strong>project endpoint</strong> from the project overview.
👆 Click the glowing item in the portal to continue
Read the steps as text
- Copy the project endpoint from the project overview.
- In a terminal, install the SDK and sign in.
- The whole app: create the client, get an OpenAI-compatible client, call responses.create with the deployment name, and pass previous_response_id to remember earlier turns.
- Run it. The second question works because the app sent the first response's id.
🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.
🖱️ Drag to rotate · scroll or pinch to zoom
Call it from your app
Your Python app uses the Foundry SDK (azure-ai-projects): connect to the project endpoint with Microsoft Entra ID, get an OpenAI-compatible client and call the deployment by its name.
🤔 What happens if…
Read this tour as text
- Resource and project. A Foundry resource is the Azure resource that holds your models, tools, security and billing. Inside it, a project is the workspace where a team builds one app: its deployments, agents, files and evaluations.
- The model catalog. The model catalog lists thousands of models: OpenAI GPT models, Microsoft Phi, Meta Llama, Mistral, DeepSeek and more. Each has a model card, benchmarks and the deployment types it supports.
- Deploy a model. Before an app can call a model you deploy it and give the deployment a name. Global Standard (pay per token, the default), Data Zone (keeps processing in the US or EU), Provisioned (reserved throughput) and Batch (cheaper, within 24 hours) trade price, speed and data location.
- Test in the playground. The chat playground lets you try a deployment with no code. The system message sets the rules, the user message asks the question, and parameters such as temperature tune the answer.
- Call it from your app. Your Python app uses the Foundry SDK (
azure-ai-projects): connect to the project endpoint with Microsoft Entra ID, get an OpenAI-compatible client and call the deployment by its name. - An agent: model + instructions + tools. An agent is a model with instructions and tools that can decide what to do next. Foundry Agent Service keeps the conversation and runs the tools for you.
- Tools in action. Ask "What is our refund limit, and what is 15% of ₹4,200?" The agent uses file search for the policy and the code interpreter for the maths, then answers in one reply. That loop of plan, act and observe is what makes it agentic.
- Guardrails and monitoring. Every request passes guardrails (content filters) on the way in and out. Tracing and evaluations show what the agent did, how long it took and whether answers are grounded and safe.
Run a real-looking Python chat client from Cloud Shell: install the Foundry SDK, point it at your project endpoint and call your deployment by name.
🎓 Education-only simulation. Your lab is saved in this browser, so you can leave and continue later.
😟 THE PROBLEM
The playground works, but customers need it inside your app.
😊 THE SOLUTION
A 15-line Python client with the Foundry SDK.
- Secrets in code✓ DefaultAzureCredential
- Which URL to call?✓ The project endpoint
- Forgets the last question✓ previous_response_id
Start with a question
The model works in the playground. How do you put it inside your own app?
The playground is great for testing, but your customers won’t log in to ai.azure.com. They’ll use your website, your WhatsApp bot or your internal tool. Somewhere in that app, a few lines of code have to send the question to your model and show the answer.
That’s what the Foundry SDK is for. With about fifteen lines of Python you get a working chat client that remembers the conversation.
Explain it like I’m 10
The technical explanation
You need four things in place before the code runs:
- A Foundry project with a deployed model (for example a deployment named
gpt-4.1-mini). - The project endpoint, from the project’s overview page, saved in an environment variable.
- The packages:
pip install "azure-ai-projects>=2.3.0" azure-identity - A signed-in identity with the Foundry User role. Locally that’s
az login; in Azure it’s a managed identity.
export FOUNDRY_PROJECT_ENDPOINT="https://<resource-name>.services.ai.azure.com/api/projects/<project-name>"
az login
A one-shot question
import os
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
# Connect to the project with Microsoft Entra ID (no API key)
project = AIProjectClient(
endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
# An OpenAI-compatible client that already points at your project
openai = project.get_openai_client()
response = openai.responses.create(
model="gpt-4.1-mini", # the DEPLOYMENT name, not the catalog name
instructions="You are a friendly travel assistant. Keep answers short.",
input="What's a good time of year to visit Munnar?",
)
print(response.output_text)
instructions is the system prompt. You can also pass input as a list of messages with system and user roles if you prefer.
Making it multi-turn
On its own, each call forgets the last one. Create a conversation once and pass its id on every turn so the service keeps the history for you:
conversation = openai.conversations.create()
while True:
question = input("You: ")
if question.lower() in ("quit", "exit"):
break
response = openai.responses.create(
model="gpt-4.1-mini",
conversation=conversation.id, # earlier turns are included automatically
instructions="You are a friendly travel assistant. Keep answers short.",
input=question,
)
print("Assistant:", response.output_text)
The other option is to chain responses: store response.id and pass it as previous_response_id= on the next call.
-
1 Sign in
DefaultAzureCredential gets an Entra ID token.
-
2 Call
responses.create sends the input to your deployment through the project endpoint.
-
3 Remember
The conversation id adds earlier turns.
-
4 Reply
Your app prints response.output_text.
Architect level Why Entra ID instead of keys
Keys are shared secrets: anyone who copies one has full access until it’s rotated. With DefaultAzureCredential, the same code signs in as you on your laptop (Azure CLI), as a managed identity in Azure App Service or Container Apps, or as a workload identity in a pipeline, and access is controlled by the Foundry User role. Tokens expire on their own, and removing a role assignment cuts access instantly.
Real-world business scenario
Tailspin Travel (fictional)
- The problem
- The team had tested a trip-planning assistant in the playground, but the web app kept forgetting what the traveller said one message earlier.
- What they used
- A Python backend using AIProjectClient with DefaultAzureCredential and a managed identity, calling responses.create with one conversation per traveller session.
- Why that choice
- The conversation keeps history on the service side, and Entra ID removed the API key the developers had been passing around in chat.
- The result
- Travellers can ask follow-ups like "and what about hotels there?" and get answers that remember the destination.
When to use it and when not to
✓ Use it when…
- You're building your own app, script or backend that needs to call a Foundry model.
- You want Entra ID sign-in and role-based access instead of keys.
- You need multi-turn chat with history kept by the service.
✕ Think twice when…
- You only want to test prompts. The playground is quicker.
- You need tools like file search or web search. Build an agent and call it with the same SDK.
↔ Alternatives to consider
- Foundry agents when the assistant needs tools and fixed instructions stored in Foundry.
- Microsoft Agent Framework for larger code-first agent apps.
Test yourself with exercises
5 original practice questions in the AI-901 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.
Interview preparation
Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.
Beginner What do you need before your Python app can chat with a Foundry model?
Model answer
A Foundry project with a deployed model, the project endpoint, the azure-ai-projects and azure-identity packages, and a signed-in identity with the Foundry User role. Locally I run az login so DefaultAzureCredential can pick it up.
Interviewer follows up
Why not just use an API key?
You
The AIProjectClient only supports Microsoft Entra ID. That's a good thing: no secret sits in my code or config, and access can be removed by changing a role.
Intermediate What does the model parameter in responses.create refer to?
Model answer
The deployment name I chose in Foundry, not the catalog model name. If I deployed gpt-4.1-mini as "chat-prod", I pass model="chat-prod".
Experienced developer How do you make the chat remember earlier messages?
Model answer
Models are stateless, so history has to be supplied. With the Responses API I either create a conversation once and pass conversation=conversation.id on every call, or pass previous_response_id=response.id from the last turn. Both let the service send the earlier turns for me, instead of me rebuilding the message list.
Scenario The app works on your laptop but fails with an authorisation error on a colleague's machine. What would you check?
Model answer
That they've run az login with an account that has the Foundry User role on the project, and that FOUNDRY_PROJECT_ENDPOINT is set to the right project. DefaultAzureCredential finds their identity, but the role assignment is what grants access.
Quick revision
Key points
- Install azure-ai-projects and azure-identity.
- Read the project endpoint from an environment variable such as FOUNDRY_PROJECT_ENDPOINT.
- AIProjectClient plus DefaultAzureCredential signs in with Microsoft Entra ID (no keys).
- get_openai_client() then responses.create, with model set to the deployment name, returns output_text.
- Multi-turn chat uses a conversation id or previous_response_id.
Important terminology
- Foundry SDK
- The Azure AI Projects client library (azure-ai-projects) used to work with a Foundry project from code.
- AIProjectClient
- The SDK class that connects to a Foundry project using its endpoint and a credential.
- DefaultAzureCredential
- An azure-identity class that signs in with Microsoft Entra ID, using az login locally or a managed identity in Azure.
- get_openai_client()
- An AIProjectClient method that returns an OpenAI-compatible client already pointed at your project.
- Responses API
- The API called with responses.create to send input to a model and get output_text back.
- Conversation
- A server-side object that stores chat history so each new request remembers earlier turns.
Common mistakes
- Passing the catalog model name instead of the deployment name.
- Expecting the model to remember the last message without a conversation or previous_response_id.
Exam tips
- "Authenticate the Foundry SDK without storing secrets" = DefaultAzureCredential (Entra ID).
- "Read the model's reply" = response.output_text.
- "Keep context across turns" = conversation or previous_response_id.
Five quick revision questions
-
1.Which package is the Foundry SDK?Show
azure-ai-projects.
-
2.Which credential class does the sample use?Show
DefaultAzureCredential.
-
3.Which method returns an OpenAI-compatible client?Show
get_openai_client().
-
4.Which property holds the reply text?Show
output_text.
-
5.Name two ways to keep a multi-turn chat.Show
Pass a conversation id, or pass previous_response_id.
Official references
Finished this lesson? Tick it off. Your progress is saved in this browser.
Next up: Create and Test an Agent in the Foundry Portal
Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges.