Session 7: Building Your First Agent
What makes an agent (model + instructions + tools + knowledge), how to create and test a single-agent solution in the Foundry portal, which tools to add (file search, code interpreter, custom functions), how to call the agent from a lightweight Python client, and how agents differ from chat apps.
Anatomy of an agent
┌──────────────── Agent ────────────────┐
User goal ─────► │ Model (reasoning engine) │
│ Instructions (role, rules, steps) │ ──► Answer
│ Tools (file search, code, │ + actions taken
│ functions, APIs, web) │
│ Knowledge (uploaded files, indexes)│
└────────────────────────────────────────┘
▲ conversation (thread) keeps state
| Part | Purpose | Example |
|---|---|---|
| Model | Decides what to do and writes the reply | gpt-5-mini or gpt-4.1-mini |
| Instructions | The agent’s system prompt: goal, persona, rules, when to use each tool | “Answer claims questions using the policy document. Always quote the clause.” |
| Tools | Capabilities beyond text | File search (RAG over uploaded files), code interpreter (runs Python for maths, charts, CSV analysis), function calling (your own code or APIs), Azure AI Search, web search, OpenAPI and MCP tools |
| Knowledge | Grounding data | A policy PDF in a vector store, an Azure AI Search index |
| Conversation | Stores the messages so the agent keeps context across turns | One conversation per customer chat |
The Foundry Agent Service hosts the agent: it runs the model–tool loop for you, stores conversations and applies content safety.
Create and test an agent in the portal
- In your Foundry project, open Agents → Create agent (or + New agent).
- Give it a name (for example
support-agent) and choose a model deployment. - Write the instructions:
You are the customer support agent for Contoso Bank.
- Answer questions about fees, cards and account rules using ONLY the attached policy document.
- Quote the relevant section name in your answer.
- If the policy does not cover the question, say so and offer to raise a ticket.
- Never ask for OTPs, PINs or full card numbers.
- Add tools / knowledge:
- File search: upload
contoso-bank-policy.pdf. Foundry chunks it, creates embeddings and stores them in a vector store that the agent searches. - Code interpreter: lets the agent run Python, for example to calculate EMI or summarise a CSV of transactions. - Function / OpenAPI / MCP tools: describe your own operations (for examplecreate_ticket(customer_id, issue)) so the agent can request them. - Select Try in playground and test: an answerable question, a calculation, an out-of-scope question and a prompt-injection attempt. Open the run details / trace to see which tool the agent used and why.
- Save. Each save creates an agent version, so you can change instructions safely and roll back.
🎯 Exam tip. File search = answer from your documents. Code interpreter = run code / do maths / analyse files. Function calling = the model asks your code to perform an action, and your code returns the result.
A lightweight Python client for the agent
Install pip install "azure-ai-projects>=2.3.0" azure-identity and sign in with az login.
Create (or update) the agent in code
import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition
load_dotenv()
project = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], credential=DefaultAzureCredential())
agent = project.agents.create_version(
agent_name="support-agent",
definition=PromptAgentDefinition(
model=os.environ["FOUNDRY_MODEL_NAME"],
instructions="You are Contoso Bank's support agent. Be brief and polite.",
),
)
print(f"Agent {agent.name}, version {agent.version}")
Chat with the agent (multi-turn)
openai = project.get_openai_client(agent_name="support-agent") # client bound to the agent
conversation = openai.conversations.create() # holds the chat history
for question in ["What is the annual fee on the Platinum card?",
"Can it be waived?"]:
response = openai.responses.create(conversation=conversation.id, input=question)
print("You: ", question)
print("Agent:", response.output_text)
The conversation stores every message, so the second question (“Can it be waived?”) is understood in context. The agent created in the portal, with its file search knowledge, can be called the same way by name.
Agents vs chat apps
| Chat app (Session 6) | Agent (Session 7) | |
|---|---|---|
| Who manages history | Your code (message list or previous_response_id) |
The service (conversation) |
| Tools | None by default | File search, code interpreter, functions, search, MCP… |
| Logic | One model call per turn | The service loops: model → tool → model → … until done |
| Configuration | In your code (system prompt, parameters) | Stored and versioned in Foundry (instructions, tools, knowledge) |
| Best for | Simple Q&A, drafting, summarising | Tasks needing data lookup, calculations or actions |
Check yourself
An agent must answer from a 200-page HR policy. Which tool do you add?
File search, with the policy uploaded to a vector store.Users ask an agent to compute loan EMIs and plot repayments. Which tool helps?
Code interpreter, which runs Python for calculations and charts.In the Python client, what object keeps the multi-turn history for an agent?
A conversation, created withopenai.conversations.create() and passed as conversation=conversation.id.