Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Azure AI Foundry Agent Service allows you to create AI agents tailored to your needs through custom instructions and augmented by advanced tools like code interpreter, and custom functions.
Prerequisites
- An Azure subscription - Create one for free.
- Ensure that the individual creating the account and project has the Azure AI Account Owner role at the subscription scope
- Alternatively, having the Contributor or Cognitive Services Contributor role at the subscription level also satisfies this requirement.
Important
The Azure AI Foundry portal only supports basic agent set at this time. If you want to perform a standard agent setup, see the Environment setup article to learn about more.
Create a Foundry account and project in Azure AI Foundry portal
To create an account and project in Azure AI Foundry, follow these steps:
Go to Azure AI Foundry. If you are in a project, select Azure AI Foundry at the top left of the page to go to the Home page.
Use the Agent getting started creation flow for the fastest experience. Click Create an agent.
Enter a name for the project. If you want to customize the default values, select Advanced options.
Select Create.
Wait for your resources to be provisioned.
- An account and project (child resource of your account) will be created.
- The gpt-4o model will automatically be deployed
- A default agent will be created
Once complete, you will land directly in the agent playground and you can start creating agents.
Note
If you are getting permission error when trying to configure or create agents ensure you have the Azure AI User on the project.
| Reference documentation | Samples | Library source code | Package (NuGet) |
Prerequisites
- A set up agent environment
- Assign the Azure AI User RBAC role to each team member who needs to create or edit agents using the SDK or Agent Playground
- This role must be assigned at the project scope
- Minimum required permissions: agents/*/read, agents/*/action, agents/*/delete
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Create a .NET Console project.
dotnet new console
Install the .NET package to your project. For example if you're using the .NET CLI, run the following command.
Note
Azure.AI.Agents.Persistent is only available as a prelease version. Please use the "-prerelease" flag to add the package until a release version becomes available.
dotnet add package Azure.AI.Agents.Persistent --prerelease
dotnet add package Azure.Identity
Next, to authenticate your API requests and run the program, use the az login command to sign into your Azure subscription.
az login
Use the following code to create and run an agent. To run this code, you will need to get the endpoint for your project. This string is in the format:
https://<AIFoundryResourceName>.services.ai.azure.com/api/projects/<ProjectName>
Tip
You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry.
For example, your connection string may look something like:
https://myresource.services.ai.azure.com/api/projects/myproject
Set this connection string in an appsetting variable named ProjectEndpoint
.
using Azure;
using Azure.AI.Agents.Persistent;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using System.Diagnostics;
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var projectEndpoint = configuration["ProjectEndpoint"];
var modelDeploymentName = configuration["ModelDeploymentName"];
//Create a PersistentAgentsClient and PersistentAgent.
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
//Give PersistentAgent a tool to execute code using CodeInterpreterToolDefinition.
PersistentAgent agent = client.Administration.CreateAgent(
model: modelDeploymentName,
name: "My Test Agent",
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
tools: [new CodeInterpreterToolDefinition()]
);
//Create a thread to establish a session between Agent and a User.
PersistentAgentThread thread = client.Threads.CreateThread();
//Ask a question of the Agent.
client.Messages.CreateMessage(
thread.Id,
MessageRole.User,
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
//Have Agent beging processing user's question with some additional instructions associated with the ThreadRun.
ThreadRun run = client.Runs.CreateRun(
thread.Id,
agent.Id,
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
//Poll for completion.
do
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
run = client.Runs.GetRun(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress
|| run.Status == RunStatus.RequiresAction);
//Get the messages in the PersistentAgentThread. Includes Agent (Assistant Role) and User (User Role) messages.
Pageable<PersistentThreadMessage> messages = client.Messages.GetMessages(
threadId: thread.Id,
order: ListSortOrder.Ascending);
//Display each message and open the image generated using CodeInterpreterToolDefinition.
foreach (PersistentThreadMessage threadMessage in messages)
{
foreach (MessageContent content in threadMessage.ContentItems)
{
switch (content)
{
case MessageTextContent textItem:
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
break;
case MessageImageFileContent imageFileContent:
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
BinaryData imageContent = client.Files.GetFileContent(imageFileContent.FileId);
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
client.Files.DeleteFile(imageFileContent.FileId);
ProcessStartInfo psi = new()
{
FileName = tempFilePath,
UseShellExecute = true
};
Process.Start(psi);
break;
}
}
}
//Clean up test resources.
client.Threads.DeleteThread(threadId: thread.Id);
client.Administration.DeleteAgent(agentId: agent.Id);
| Reference documentation | Samples | Library source code | Package (PyPi) |
Prerequisites
- A set up agent environment
- Assign the Azure AI User RBAC role to each team member who needs to create or edit agents using the SDK or Agent Playground
- This role must be assigned at the project scope
- Minimum required permissions: agents/*/read, agents/*/action, agents/*/delete
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
Run the following commands to install the python packages.
pip install azure-ai-projects
pip install azure-identity
Next, to authenticate your API requests and run the program, use the az login command to sign into your Azure subscription.
az login
Use the following code to create and run an agent. To run this code, you will need to get the endpoint for your project. This string is in the format:
https://<AIFoundryResourceName>.services.ai.azure.com/api/projects/<ProjectName>
Tip
You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry.
For example, your connection string may look something like:
https://myresource.services.ai.azure.com/api/projects/myproject
Set this connection string as an environment variable named PROJECT_ENDPOINT
.
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import CodeInterpreterTool
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
# You need to login to Azure subscription via Azure CLI and set the environment variables
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
# Create an AIProjectClient instance
project_client = AIProjectClient(
endpoint=project_endpoint,
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
api_version="latest",
)
code_interpreter = CodeInterpreterTool()
with project_client:
# Create an agent with the Bing Grounding tool
agent = project_client.agents.create_agent(
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
name="my-agent", # Name of the agent
instructions="You are a helpful agent", # Instructions for the agent
tools=code_interpreter.definitions, # Attach the tool
)
print(f"Created agent, ID: {agent.id}")
# Create a thread for communication
thread = project_client.agents.threads.create()
print(f"Created thread, ID: {thread.id}")
# Add a message to the thread
message = project_client.agents.messages.create(
thread_id=thread.id,
role="user", # Role of the message sender
content="What is the weather in Seattle today?", # Message content
)
print(f"Created message, ID: {message['id']}")
# Create and process an agent run
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
print(f"Run finished with status: {run.status}")
# Check if the run failed
if run.status == "failed":
print(f"Run failed: {run.last_error}")
# Fetch and log all messages
messages = project_client.agents.messages.list(thread_id=thread.id)
for message in messages.data:
print(f"Role: {message.role}, Content: {message.content}")
# Delete the agent when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
| Reference documentation | Library source code | Package (PyPi) |
Prerequisites
- A set up agent environment
- Assign the Azure AI User RBAC role to each team member who needs to create or edit agents using the SDK or Agent Playground
- This role must be assigned at the project scope
- Minimum required permissions: agents/*/read, agents/*/action, agents/*/delete
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
Run the following commands to install the python packages.
pip install azure-ai-projects
pip install azure-identity
pip install openai
Next, to authenticate your API requests and run the program, use the az login command to sign into your Azure subscription.
az login
Use the following code to create and run an agent. To run this code, you will need to create a connection string using information from your project. This string is in the format:
<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>
Tip
You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry.
HostName
can be found by navigating to your discovery_url
and removing the leading https://
and trailing /discovery
. To find your discovery_url
, run this CLI command:
az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
For example, your connection string may look something like:
eastus.api.azureml.ms;12345678-abcd-1234-9fc6-62780b3d3e05;my-resource-group;my-project-name
Set this connection string as an environment variable named PROJECT_CONNECTION_STRING
.
import os, time
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from openai import AzureOpenAI
with AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(),
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
) as project_client:
# Explicit type hinting for IntelliSense
client: AzureOpenAI = project_client.inference.get_azure_openai_client(
# The latest API version is 2024-10-01-preview
api_version = os.environ.get("AZURE_OPENAI_API_VERSION"),
)
with client:
agent = client.beta.assistants.create(
model="gpt-4o-mini", name="my-agent", instructions="You are a helpful agent"
)
print(f"Created agent, agent ID: {agent.id}")
thread = client.beta.threads.create()
print(f"Created thread, thread ID: {thread.id}")
message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
print(f"Created message, message ID: {message.id}")
run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=agent.id)
# Poll the run while run status is queued or in progress
while run.status in ["queued", "in_progress", "requires_action"]:
time.sleep(1) # Wait for a second
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
print(f"Run status: {run.status}")
client.beta.assistants.delete(agent.id)
print("Deleted agent")
messages = client.beta.threads.messages.list(thread_id=thread.id)
print(f"Messages: {messages}")
| Reference documentation | Samples | Library source code | Package (npm) |
Prerequisites
- A set up agent environment
- Assign the Azure AI User RBAC role to each team member who needs to create or edit agents using the SDK or Agent Playground
- This role must be assigned at the project scope
- Minimum required permissions: agents/*/read, agents/*/action, agents/*/delete
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
Key objects in this code include:
First, initialize a new project by running:
npm init -y
Run the following commands to install the npm packages required.
npm install @azure/ai-agents @azure/identity
npm install dotenv
Next, to authenticate your API requests and run the program, use the az login command to sign into your Azure subscription.
az login
Use the following code to create and run an agent. To run this code, you will need to get the endpoint for your project. This string is in the format:
https://<AIFoundryResourceName>.services.ai.azure.com/api/projects/<ProjectName>
Tip
You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry.
For example, your connection string may look something like:
https://myresource.services.ai.azure.com/api/projects/myproject
Set this connection string as an environment variable named PROJECT_ENDPOINT
in a .env
file.
Important
- This quickstart code uses environment variables for sensitive configuration. Never commit your
.env
file to version control by making sure.env
is listed in your.gitignore
file. - Remember: If you accidentally commit sensitive information, consider those credentials compromised and rotate them immediately.
Next, create an index.js
file and paste in the code below:
const {
RunStreamEvent,
MessageStreamEvent,
DoneEvent,
ErrorEvent,
AgentsClient,
isOutputOfType,
ToolUtility,
} = require("@azure/ai-agents");
const { DefaultAzureCredential } = require("@azure/identity");
const fs = require("fs");
const path = require("node:path");
require("dotenv/config");
const projectEndpoint = process.env["PROJECT_ENDPOINT"] || "<project connection string>";
const modelDeploymentName = process.env["MODEL_DEPLOYMENT_NAME"] || "gpt-4o";
async function main() {
// Create an Azure AI Client
const client = new AgentsClient(projectEndpoint, new DefaultAzureCredential());
// Upload file and wait for it to be processed
const filePath = "./data/nifty500QuarterlyResults.csv";
const localFileStream = fs.createReadStream(filePath);
const localFile = await client.files.upload(localFileStream, "assistants", {
fileName: "myLocalFile",
});
console.log(`Uploaded local file, file ID : ${localFile.id}`);
// Create code interpreter tool
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([localFile.id]);
// Notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment
const agent = await client.createAgent(modelDeploymentName, {
name: "my-agent",
instructions: "You are a helpful agent",
tools: [codeInterpreterTool.definition],
toolResources: codeInterpreterTool.resources,
});
console.log(`Created agent, agent ID: ${agent.id}`);
// Create a thread
const thread = await client.threads.create();
console.log(`Created thread, thread ID: ${thread.id}`);
// Create a message
const message = await client.messages.create(
thread.id,
"user",
"Could you please create a bar chart in the TRANSPORTATION sector for the operating profit from the uploaded CSV file and provide the file to me?",
);
console.log(`Created message, message ID: ${message.id}`);
// Create and execute a run
const streamEventMessages = await client.runs.create(thread.id, agent.id).stream();
for await (const eventMessage of streamEventMessages) {
switch (eventMessage.event) {
case RunStreamEvent.ThreadRunCreated:
console.log(`ThreadRun status: ${eventMessage.data.status}`);
break;
case MessageStreamEvent.ThreadMessageDelta:
{
const messageDelta = eventMessage.data;
messageDelta.delta.content.forEach((contentPart) => {
if (contentPart.type === "text") {
const textContent = contentPart;
const textValue = textContent.text?.value || "No text";
console.log(`Text delta received:: ${textValue}`);
}
});
}
break;
case RunStreamEvent.ThreadRunCompleted:
console.log("Thread Run Completed");
break;
case ErrorEvent.Error:
console.log(`An error occurred. Data ${eventMessage.data}`);
break;
case DoneEvent.Done:
console.log("Stream completed.");
break;
}
}
// Delete the original file from the agent to free up space (note: this does not delete your version of the file)
await client.files.delete(localFile.id);
console.log(`Deleted file, file ID : ${localFile.id}`);
// Print the messages from the agent
const messagesIterator = client.messages.list(thread.id);
const messagesArray = [];
for await (const m of messagesIterator) {
messagesArray.push(m);
}
console.log("Messages:", messagesArray);
// Get most recent message from the assistant
const assistantMessage = messagesArray.find((msg) => msg.role === "assistant");
if (assistantMessage) {
const textContent = assistantMessage.content.find((content) => isOutputOfType(content, "text"));
if (textContent) {
// Save the newly created file
console.log(`Saving new files...`);
const imageFileOutput = messagesArray[0].content[0];
const imageFile = imageFileOutput.imageFile.fileId;
const imageFileName = path.resolve(
"./data/" + (await client.files.get(imageFile)).filename + "ImageFile.png",
);
console.log(`Image file name : ${imageFileName}`);
const fileContent = await (await client.files.getContent(imageFile).asNodeStream()).body;
if (fileContent) {
const chunks = [];
for await (const chunk of fileContent) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
const buffer = Buffer.concat(chunks);
fs.writeFileSync(imageFileName, buffer);
} else {
console.log("No file content available");
}
}
}
// Iterate through messages and print details for each annotation
console.log(`Message Details:`);
messagesArray.forEach((m) => {
console.log(`File Paths:`);
console.log(`Type: ${m.content[0].type}`);
if (isOutputOfType(m.content[0], "text")) {
const textContent = m.content[0];
console.log(`Text: ${textContent.text.value}`);
}
console.log(`File ID: ${m.id}`);
// firstId and lastId are properties of the paginator, not the messages array
// Removing these references as they don't exist in this context
});
// Delete the agent once done
await client.deleteAgent(agent.id);
console.log(`Deleted agent, agent ID: ${agent.id}`);
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
module.exports = { main };
Run the code using node index.js
and observe.
The output contains the prompt, and answers.
Type: text
Text: Sure, I can help you solve the equation \(3x + 11 = 14\).
To solve for \(x\), we need to isolate \(x\) on one side of the equation. Here are the steps:
1. Subtract 11 from both sides of the equation:
\[3x + 11 - 11 = 14 - 11\]
\[3x = 3\]
2. Divide both sides of the equation by 3:
\[\frac{3x}{3} = \frac{3}{3}\]
\[x = 1\]
So the solution to the equation \(3x + 11 = 14\) is \(x = 1\).
Type: text
Text: I need to solve the equation `3x + 11 = 14`. Can you help me?
| Reference documentation | Samples | Library source code | Package (npm) |
Prerequisites
- An Azure subscription - Create one for free.
- Node.js LTS
- TypeScript 5.x
- Ensure that each team member who wants to use the Agent Playground or SDK to create or edit agents has been assigned the built-in Azure AI Developer RBAC role for the project.
- Note: assign these roles after the template has been deployed
- The minimum set of permissions required is: agents/*/read, agents/*/action, agents/*/delete
- Make sure you have the Azure AI Developer RBAC role assigned at the appropriate level.
- Install the Azure CLI and the machine learning extension. If you have the CLI already installed, make sure it's updated to the latest version.
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
Key objects in this code include:
Run the following commands to install the npm packages.
npm install @azure/ai-projects
npm install @azure/identity
Next, to authenticate your API requests and run the program, use the az login command to sign into your Azure subscription.
az login
Use the following code to create and run an agent. To run this code, you will need to create a connection string using information from your project. This string is in the format:
<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>
Tip
You can also find your connection string in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry.
HostName
can be found by navigating to your discovery_url
and removing the leading https://
and trailing /discovery
. To find your discovery_url
, run this CLI command:
az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
For example, your connection string may look something like:
eastus.api.azureml.ms;12345678-abcd-1234-9fc6-62780b3d3e05;my-resource-group;my-project-name
Set this connection string as an environment variable named PROJECT_CONNECTION_STRING
.
// index.ts
import {
AIProjectsClient,
DoneEvent,
ErrorEvent,
isOutputOfType,
MessageStreamEvent,
RunStreamEvent,
ToolUtility
} from "@azure/ai-projects";
import type {
MessageDeltaChunk,
MessageDeltaTextContent,
MessageTextContentOutput
} from "@azure/ai-projects";
import { DefaultAzureCredential } from "@azure/identity";
import dotenv from 'dotenv';
dotenv.config();
// Set the connection string from the environment variable
const connectionString = process.env.PROJECT_CONNECTION_STRING;
const model = "gpt-4o";
// Throw an error if the connection string is not set
if (!connectionString) {
throw new Error("Please set the PROJECT_CONNECTION_STRING environment variable.");
}
export async function main() {
const client = AIProjectsClient.fromConnectionString(
connectionString || "",
new DefaultAzureCredential(),
);
// Step 1 code interpreter tool
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([]);
// Step 2 an agent
const agent = await client.agents.createAgent(model, {
name: "my-agent",
instructions: "You are a helpful agent",
tools: [codeInterpreterTool.definition],
toolResources: codeInterpreterTool.resources,
});
// Step 3 a thread
const thread = await client.agents.createThread();
// Step 4 a message to thread
await client.agents.createMessage(
thread.id, {
role: "user",
content: "I need to solve the equation `3x + 11 = 14`. Can you help me?",
});
// Intermission is now correlated with thread
// Intermission messages will retrieve the message just added
// Step 5 the agent
const streamEventMessages = await client.agents.createRun(thread.id, agent.id).stream();
for await (const eventMessage of streamEventMessages) {
switch (eventMessage.event) {
case RunStreamEvent.ThreadRunCreated:
break;
case MessageStreamEvent.ThreadMessageDelta:
{
const messageDelta = eventMessage.data as MessageDeltaChunk;
messageDelta.delta.content.forEach((contentPart) => {
if (contentPart.type === "text") {
const textContent = contentPart as MessageDeltaTextContent;
const textValue = textContent.text?.value || "No text";
}
});
}
break;
case RunStreamEvent.ThreadRunCompleted:
break;
case ErrorEvent.Error:
console.log(`An error occurred. Data ${eventMessage.data}`);
break;
case DoneEvent.Done:
break;
}
}
// 6. Print the messages from the agent
const messages = await client.agents.listMessages(thread.id);
// Messages iterate from oldest to newest
// messages[0] is the most recent
const messagesArray = messages.data;
for (let i = messagesArray.length - 1; i >= 0; i--) {
const m = messagesArray[i];
console.log(`Type: ${m.content[0].type}`);
if (isOutputOfType<MessageTextContentOutput>(m.content[0], "text")) {
const textContent = m.content[0] as MessageTextContentOutput;
console.log(`Text: ${textContent.text.value}`);
}
}
// 7. Delete the agent once done
await client.agents.deleteAgent(agent.id);
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Build the TypeScript code then run the code using node index.js
and observe.
The output contains the prompt, and answers.
Type: text
Text: Sure, I can help you solve the equation \(3x + 11 = 14\).
To solve for \(x\), we need to isolate \(x\) on one side of the equation. Here are the steps:
1. Subtract 11 from both sides of the equation:
\[3x + 11 - 11 = 14 - 11\]
\[3x = 3\]
2. Divide both sides of the equation by 3:
\[\frac{3x}{3} = \frac{3}{3}\]
\[x = 1\]
So the solution to the equation \(3x + 11 = 14\) is \(x = 1\).
Type: text
Text: I need to solve the equation `3x + 11 = 14`. Can you help me?
Prerequisites
- A set up agent environment
- Assign the Azure AI User RBAC role to each team member who needs to create or edit agents using the SDK or Agent Playground
- This role must be assigned at the project scope
- Minimum required permissions: agents/*/read, agents/*/action, agents/*/delete
Configure and run an agent
Component | Description |
---|---|
Agent | Custom AI that uses AI models in conjunction with tools. |
Tool | Tools help extend an agent’s ability to reliably and accurately respond during conversation. Such as connecting to user-defined knowledge bases to ground the model, or enabling web search to provide current information. |
Thread | A conversation session between an agent and a user. Threads store Messages and automatically handle truncation to fit content into a model’s context. |
Message | A message created by an agent or a user. Messages can include text, images, and other files. Messages are stored as a list on the Thread. |
Run | Activation of an agent to begin running based on the contents of Thread. The agent uses its configuration and Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the agent appends Messages to the Thread. |
Run Step | A detailed list of steps the agent took as part of a Run. An agent can call tools or create Messages during its run. Examining Run Steps allows you to understand how the agent is getting to its results. |
API call information
To authenticate your API requests, use the az login command to sign into your Azure subscription.
az login
Next, you will need to fetch the Entra ID token to provide as authorization to the API calls. Fetch the token using the CLI command:
az account get-access-token --resource 'https://ai.azure.com' | jq -r .accessToken | tr -d '"'
Set the access token as an environment variable named AGENT_TOKEN
.
To successfully make REST API calls to Azure AI Foundry Agent Service, you will need to use the endpoint as below:
https://<your_ai_service_name>.services.ai.azure.com/api/projects/<your_project_name>
For example, your endpoint may look something like:
https://exampleaiservice.services.ai.azure.com/api/projects/project
Set this endpoint as an environment variable named AZURE_AI_FOUNDRY_PROJECT_ENDPOINT
.
Note
- For
api-version
parameter, the GA API version is2025-05-01
and the latest preview API version is2025-05-15-preview
. You must use the preview API for tools that are in preview. - Consider making your API version an environment variable, such as
$API_VERSION
.
Create an agent
Note
With Azure AI Agents Service the model
parameter requires model deployment name. If your model deployment name is different than the underlying model name then you would adjust your code to "model": "{your-custom-model-deployment-name}"
.
curl --request POST \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/assistants?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"instructions": "You are a helpful agent.",
"name": "my-agent",
"tools": [{"type": "code_interpreter"}],
"model": "gpt-4o-mini"
}'
Create a thread
curl --request POST \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN" \
-H "Content-Type: application/json" \
-d ''
Add a user question to the thread
curl --request POST \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"role": "user",
"content": "I need to solve the equation `3x + 11 = 14`. Can you help me?"
}'
Run the thread
curl --request POST \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"assistant_id": "asst_abc123",
}'
Retrieve the status of the run
curl --request GET \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs/run_abc123?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN"
Retrieve the agent response
curl --request GET \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=2025-05-01 \
-H "Authorization: Bearer $AGENT_TOKEN"