Let’s Get Certified
Module 05 / 08 · Implement AI solutions by using Microsoft Foundry

Connect a Foundry model and a single agent

Deploy a model, test its instructions and trace how a Python client carries a conversation.

Connect a Foundry model and a single agent

Work through the decision

A travel-desk assistant works in the Foundry playground. In Python the first request reports an unknown model. After that is fixed, follow-up questions lose the visitor’s chosen city. Which identifiers should the developer trace?

Check your reasoning

Copy the project endpoint from the intended project and the model deployment name from its deployments. Try one minimal request with the authorized identity. For a stateful agent, select the intended agent version and carry the same visitor’s conversation identifier into the next turn. Test with a second visitor to verify separation.

Watch the source lesson at 35:17

Compare the choices

System instructions
Keep durable behavior, scope and output rules in system instructions. Enforce access separately in application code.
User message
Put the visitor’s current question and relevant task input in the user turn.
Deployment name
The inference call identifies the deployment configured in the project. Its name can differ from the underlying model family.
Project endpoint
Connect the project client to the intended Foundry project and use an identity authorized there.
Conversation identifier
Keep the history of one session associated with that session. Continue a conversation only for its intended user.
Agent name and version
An agent reference selects a configured assistant. Track the version tested, its instructions, model and tools.
Playground test
Run a controlled prompt against the chosen portal deployment or agent. Compare its result with the same case through the app.

Test the decision

In the portal test one ordinary question, one missing fact and one tool action that requires approval. Record the deployment, agent version, available tools and expected behavior. Repeat those inputs through the client so a portal configuration change can be distinguished from an application error.

Trace a Foundry model call

In a project with an authorized Azure identity, install azure-ai-projects and azure-identity. Set FOUNDRY_PROJECT_ENDPOINT to the project endpoint and MODEL_DEPLOYMENT to its deployed text model. The instruction controls the standing behavior and the input carries this turn’s evidence.

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

project = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
                          credential=DefaultAzureCredential())
with project.get_openai_client() as client:
    answer = client.responses.create(
        model=os.environ["MODEL_DEPLOYMENT"],
        instructions="Use the supplied notice. Say when it has no answer.",
        input="Notice: the east wing opens at noon. When does it open?",
    )
    print(answer.output_text)

Foundry Python client

Continue a prompt agent conversation

In Foundry, create a prompt agent, choose its deployment, give it the exhibit instructions and test a two-turn exchange in the playground. Record the version you evaluated. With the current SDK, use the same project client as above and bind the OpenAI client to your agent’s name. The conversation carries history between turns.

with project.get_openai_client(agent_name=os.environ["AGENT_NAME"]) as agent:
    visit = agent.conversations.create()
    for question in ["I am visiting the map room.", "Which room did I choose?"]:
        answer = agent.responses.create(conversation=visit.id, input=question)
        print(answer.output_text)

Microsoft prompt agent quickstart

Practice the guide skills

  1. Write standing instructions to answer from museum policy and a separate visitor question. Change the question and preserve the behavior rule. Watch 35:17
  2. In a Foundry project, select an available model in the catalog, deploy it under a named deployment, open the playground and test one answerable and one unanswerable prompt. Watch 36:47
  3. Trace the Python request from identity and project endpoint to deployment name and response text. Predict the failure if code uses a deployment from another project. Watch 41:06
  4. Create one prompt agent with bounded instructions and approved tools in the portal. Exercise one valid task and one request outside the agent’s remit. Watch 47:03
  5. Choose the tested agent revision, create a conversation and send two related turns. Start a separate conversation for another visitor and check the histories. Watch 48:40

Official objectives for this lesson

Lab · Keep two agent conversations separate

Trace this offline Python 3 fixture before running it. Two people share one deployed agent. Predict what its follow-up will resolve for each visitor. Change the version or delete the second visitor’s initial turn and inspect the resulting behavior.

agent = ("gallery-guide", "2")
sessions = {}

def respond(reference, conversation, exhibit=None):
    if reference != agent:
        return "unknown agent version"
    if exhibit is not None:
        sessions[conversation] = exhibit
        return "saved " + exhibit
    previous = sessions.get(conversation)
    return "follow up on " + previous if previous else "ask which exhibit"

requests = [
    (agent, "visit-a", "map room"),
    (agent, "visit-b", "bronze horse"),
    (agent, "visit-a", None),
    (agent, "visit-c", None),
    (("gallery-guide", "1"), "visit-b", None),
    (agent, "visit-b", None),
]
for reference, conversation, exhibit in requests:
    print(conversation, respond(reference, conversation, exhibit))
Check the result
visit-a saved map room
visit-b saved bronze horse
visit-a follow up on map room
visit-c ask which exhibit
visit-b unknown agent version
visit-b follow up on bronze horse

The reference selects the configured agent. The conversation distinguishes one visitor’s history from another’s. Replace visit-b with visit-a on the second request and explain the incorrect follow-up. In a real client, associate conversation IDs server-side with the authorized user and use Foundry’s conversation service.

Related lesson · Build and publish a grounded service agent

Knowledge check

Connect a Foundry model and a single agent quiz

Answer 3 questions. Check each answer for an explanation.

Worked example · Connect a Foundry model and a single agent