Edit

Share via


Quickstart: Create a new agent

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:

  1. 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.

  2. Use the Agent getting started creation flow for the fastest experience. Click Create an agent.

    A screenshot of the Azure AI Foundry portal.

  3. Enter a name for the project. If you want to customize the default values, select Advanced options.

    A screenshot of the advanced options for creating a project.

  4. Select Create.

  5. Wait for your resources to be provisioned.

    1. An account and project (child resource of your account) will be created.
    2. The gpt-4o model will automatically be deployed
    3. A default agent will be created
  6. Once complete, you will land directly in the agent playground and you can start creating agents.

    Screenshot of the agent playground.

    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.

dotnet add package Azure.AI.Agents.Persistent
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 endpoint in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry. A screenshot showing the endpoint in the Azure AI Foundry portal.

For example, your endpoint may look something like:

https://myresource.services.ai.azure.com/api/projects/myproject

Set this endpoint 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 endpoint in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry. A screenshot showing the endpoint in the Azure AI Foundry portal.

For example, your endpoint may look something like:

https://myresource.services.ai.azure.com/api/projects/myproject

Set this endpoint 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:
        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 | 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 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 which uploads a CSV file of data then generates a bar chart from that data. To run this code, you'll 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 endpoint in the overview for your project in the Azure AI Foundry portal, under Libraries > Azure AI Foundry. A screenshot showing the endpoint in the Azure AI Foundry portal.

For example, your endpoint looks something like:

https://myresource.services.ai.azure.com/api/projects/myproject

Set this endpoint 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 following code:

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
 * This sample demonstrates how to use agent operations with code interpreter from the Azure Agents service.
 *
 * @summary demonstrates how to use agent operations with code interpreter.
 */
// @ts-nocheck
import type {
  MessageDeltaChunk,
  MessageDeltaTextContent,
  MessageImageFileContent,
  MessageTextContent,
  ThreadRun,
} from "@azure/ai-agents";
import {
  RunStreamEvent,
  MessageStreamEvent,
  DoneEvent,
  ErrorEvent,
  AgentsClient,
  isOutputOfType,
  ToolUtility,
} from "@azure/ai-agents";
import { DefaultAzureCredential } from "@azure/identity";

import * as fs from "fs";
import path from "node:path";
import "dotenv/config";

const projectEndpoint = process.env["PROJECT_ENDPOINT"] || "<project endpoint>";
const modelDeploymentName = process.env["MODEL_DEPLOYMENT_NAME"] || "gpt-4o";

export async function main(): Promise<void> {
  // 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 as ThreadRun).status}`);
        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";
              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
// Get most recent message from the assistant
  const assistantMessage = messagesArray.find((msg) => msg.role === "assistant");
  if (assistantMessage) {
    // Look for an image file in the assistant's message
    const imageFileOutput = assistantMessage.content.find(content => 
      content.type === "image_file" && content.imageFile?.fileId);
    
    if (imageFileOutput) {
      try {
        // Save the newly created file
        console.log(`Saving new files...`);
        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 client.files.getContent(imageFile).asNodeStream();
        if (fileContent && fileContent.body) {
          const chunks = [];
          for await (const chunk of fileContent.body) {
            chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
          }
          const buffer = Buffer.concat(chunks);
          fs.writeFileSync(imageFileName, buffer);
          console.log(`Successfully saved image to ${imageFileName}`);
        } else {
          console.error("No file content available in the response");
        }
      } catch (error) {
        console.error("Error saving image file:", error);
      }
    } else {
      console.log("No image file found in assistant's message");
    }
  } else {
    console.log("No assistant message found");
  }

  // 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<MessageTextContent>(m.content[0], "text")) {
      const textContent = m.content[0] as MessageTextContent;
      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);
});

Run the code using node index.js. This code generates a bar chart PNG image file in the TRANSPORTATION sector for the operating profit from the uploaded CSV file and provided the file to you. Full sample source code available.

| Reference documentation |

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 is 2025-05-01 and the latest preview API version is 2025-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"