- Generate a response from a model
- Create an agent with a defined prompt
- Have a multi-turn conversation with the agent
Prerequisites
- A model deployed in Microsoft Foundry. If you don’t have a model, first complete Quickstart: Set up Microsoft Foundry resources.
Or, skip the deployment step and try an instant model (preview) instead. Create a project in West US 3 to use instant access models. Instant models have no deployment, so use the model name
gpt-5-mini wherever the samples ask for a deployment name.- The required language runtimes, global tools, and Visual Studio Code extensions as described in Prepare your development environment.
Get the code and set your values
- Python
- C#
- TypeScript
- Java
- REST API
- Foundry portal
The Python samples don’t read environment variables. In each file, replace these placeholder values:
your_project_endpoint: Your project endpoint, in the formathttps://<resource-name>.services.ai.azure.com/api/projects/<project-name>.your_agent_name: A name for your agent, such asMyAgent.
gpt-5-mini deployment you created in Set up Microsoft Foundry resources. If you deployed a model under a different name, update the model name in the sample code.Follow along below or get the code:Get the code
The C# samples don’t read environment variables. In each file, replace these placeholder values:
your_project_endpoint: Your project endpoint, in the formathttps://<resource-name>.services.ai.azure.com/api/projects/<project-name>.your_agent_name: A name for your agent, such asMyAgent.
gpt-5-mini deployment you created in Set up Microsoft Foundry resources. If you deployed a model under a different name, update the model name in the sample code.Follow along below or get the code:Get the code
The TypeScript samples don’t read environment variables. In each file, replace these values with your project endpoint and an agent name such as The samples use the
MyAgent:const FOUNDRY_PROJECT_ENDPOINT = "https://<resource-name>.services.ai.azure.com/api/projects/<project-name>";
const FOUNDRY_AGENT_NAME = "MyAgent";
gpt-5-mini deployment you created in Set up Microsoft Foundry resources. If you deployed a model under a different name, update the model name in the sample code.Follow along below or get the code:Get the code
The Java samples don’t read environment variables. In each file, replace these values with your project endpoint and an agent name such as The samples use the
MyAgent:String foundryProjectEndpoint = "https://<resource-name>.services.ai.azure.com/api/projects/<project-name>";
String foundryAgentName = "MyAgent";
gpt-5-mini deployment you created in Set up Microsoft Foundry resources. If you deployed a model under a different name, update the model name in the sample code.Follow along below or get the code:Get the code
-
In each request URL, replace
YOUR-FOUNDRY-RESOURCE-NAMEandYOUR-PROJECT-NAMEwith the values from your project endpoint, which has the formhttps://<resource-name>.services.ai.azure.com/api/projects/<project-name>. -
The chat-with-agent request reads the agent name from an environment variable:
FOUNDRY_AGENT_NAME=MyAgent
gpt-5-mini deployment you created in Set up Microsoft Foundry resources. If you deployed a model under a different name, update the model value in the request body.Follow along below or get the code:Get the code
No code is necessary when using the Foundry portal.
Install and authenticate
Make sure you install the correct version of the packages as shown here.- Python
- C#
- TypeScript
- Java
- REST API
- Foundry portal
-
Install the current version of
azure-ai-projects. This version uses the Foundry projects (new) API. The samples authenticate by usingDefaultAzureCredential, which comes fromazure-identity.pip install "azure-ai-projects>=2.3.0" azure-identity -
Sign in using the CLI
az logincommand to authenticate before running your Python scripts.
-
Install packages:
Add NuGet packages using the .NET CLI in the integrated terminal: These packages use the Foundry projects (new) API.
dotnet add package Azure.AI.Projects dotnet add package Azure.AI.Projects.Agents dotnet add package Azure.AI.Extensions.OpenAI dotnet add package Azure.Identity -
Sign in using the CLI
az logincommand to authenticate before running your C# scripts.
-
Install the current version of
@azure/ai-projects. This version uses the Foundry projects (new) API.:npm install @azure/ai-projects @azure/identity -
Sign in using the CLI
az logincommand to authenticate before running your TypeScript scripts.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-agents</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.57.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.1</version>
</dependency>
- Sign in using the CLI
az logincommand to authenticate before running your Java scripts.
-
Sign in using the CLI
az logincommand to authenticate before running the next command. -
Get a temporary access token. It will expire in 60-90 minutes, you’ll need to refresh after that.
az account get-access-token --scope https://ai.azure.com/.default -
Save the results as the environment variable
AZURE_AI_AUTH_TOKEN.
No installation is necessary to use the Foundry portal.
Code uses Azure AI Projects 2.x and is incompatible with Azure AI Projects 1.x. See the Foundry (classic) documentation for the Azure AI Projects 1.x version.
Chat with a model
Interacting with a model is the basic building block of AI applications. Send an input and receive a response from the model:- Python
- TypeScript
- Java
- REST API
- Foundry portal
import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
load_dotenv()
print(f"Using PROJECT_ENDPOINT: {os.environ['PROJECT_ENDPOINT']}")
print(f"Using MODEL_DEPLOYMENT_NAME: {os.environ['MODEL_DEPLOYMENT_NAME']}")
project_client = AIProjectClient(
endpoint=os.environ["PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
openai_client = project_client.get_openai_client()
response = openai_client.responses.create(
model=os.environ["MODEL_DEPLOYMENT_NAME"],
input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")
C#
using Azure.AI.Projects;
using Azure.AI.Projects.OpenAI;
using Azure.Identity;
using OpenAI.Responses;
#pragma warning disable OPENAI001
string projectEndpoint = Environment.GetEnvironmentVariable("PROJECT_ENDPOINT")
?? throw new InvalidOperationException("Missing environment variable 'PROJECT_ENDPOINT'");
string modelDeploymentName = Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME")
?? throw new InvalidOperationException("Missing environment variable 'MODEL_DEPLOYMENT_NAME'");
AIProjectClient projectClient = new(new Uri(projectEndpoint), new AzureCliCredential());
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
ResponseResult response = await responseClient.CreateResponseAsync("What is the size of France in square miles?");
Console.WriteLine(response.GetOutputText());
import { DefaultAzureCredential } from "@azure/identity";
import { AIProjectClient } from "@azure/ai-projects";
import "dotenv/config";
const projectEndpoint = process.env["PROJECT_ENDPOINT"] || "<project endpoint>";
const deploymentName = process.env["MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
async function main(): Promise<void> {
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
const openAIClient = project.getOpenAIClient();
const response = await openAIClient.responses.create({
model: deploymentName,
input: "What is the size of France in square miles?",
});
console.log(`Response output: ${response.output_text}`);
}
main().catch(console.error);
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.AgentsClientBuilder;
import com.azure.ai.agents.ResponsesClient;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class CreateResponse {
public static void main(String[] args) {
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
String foundryProjectEndpoint = "your_project_endpoint";
// Create responses client to call Foundry API
ResponsesClient responsesClient = new AgentsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(foundryProjectEndpoint)
.buildResponsesClient();
// Run a responses API call
ResponseCreateParams responseRequest = new ResponseCreateParams.Builder()
.input("What is the size of France in square miles?")
.model("gpt-5-mini")
.build();
Response response = responsesClient.getResponseService().create(responseRequest);
System.out.println(response.output());
}
}
Replace
YOUR-FOUNDRY-RESOURCE-NAME with your values:curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/openai/responses?api-version=2025-11-15-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1-mini",
"input": "What is the size of France in square miles?"
}'
- After the model deploys, you’re automatically moved from Home to the Build section. Your new model is selected and ready for you to try out.
If you skipped deployment, select Test in playground from the home page. Select the instant access model you want to use, such as
gpt-5-mini. (During preview, these instant access models are available only for projects in West US3.)- Start chatting with your model, for example, “Write me a poem about flowers.”
Code uses Azure AI Projects 2.x and is incompatible with Azure AI Projects 1.x. See the Foundry (classic) documentation for the Azure AI Projects 1.x version.
Create an agent
Create an agent using your deployed model. An agent defines core behavior. Once created, it ensures consistent responses in user interactions without repeating instructions each time. You can update or delete agents anytime.- Python
- C#
- TypeScript
- Java
- REST API
- Foundry portal
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_client = AIProjectClient(
endpoint=os.environ["PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
agent = project_client.agents.create_version(
agent_name=os.environ["AGENT_NAME"],
definition=PromptAgentDefinition(
model=os.environ["MODEL_DEPLOYMENT_NAME"],
instructions="You are a helpful assistant that answers general questions",
),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
using Azure.AI.Projects;
using Azure.AI.Projects.OpenAI;
using Azure.Identity;
string projectEndpoint = Environment.GetEnvironmentVariable("PROJECT_ENDPOINT")
?? throw new InvalidOperationException("Missing environment variable 'PROJECT_ENDPOINT'");
string modelDeploymentName = Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME")
?? throw new InvalidOperationException("Missing environment variable 'MODEL_DEPLOYMENT_NAME'");
string agentName = Environment.GetEnvironmentVariable("AGENT_NAME")
?? throw new InvalidOperationException("Missing environment variable 'AGENT_NAME'");
AIProjectClient projectClient = new(new Uri(projectEndpoint), new AzureCliCredential());
AgentDefinition agentDefinition = new PromptAgentDefinition(modelDeploymentName)
{
Instructions = "You are a helpful assistant that answers general questions",
};
AgentVersion newAgentVersion = projectClient.Agents.CreateAgentVersion(
agentName,
options: new(agentDefinition));
List<AgentVersion> agentVersions = [..projectClient.Agents.GetAgentVersions(agentName)];
foreach (AgentVersion agentVersion in agentVersions)
{
Console.WriteLine($"Agent: {agentVersion.Id}, Name: {agentVersion.Name}, Version: {agentVersion.Version}");
}
import { DefaultAzureCredential } from "@azure/identity";
import { AIProjectClient } from "@azure/ai-projects";
import "dotenv/config";
const projectEndpoint = process.env["PROJECT_ENDPOINT"] || "<project endpoint>";
const deploymentName = process.env["MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
async function main(): Promise<void> {
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
const agent = await project.agents.createVersion("my-agent-basic", {
kind: "prompt",
model: deploymentName,
instructions: "You are a helpful assistant that answers general questions",
});
console.log(`Agent created (id: ${agent.id}, name: ${agent.name}, version: ${agent.version})`);
}
main().catch(console.error);
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.AgentsClient;
import com.azure.ai.agents.AgentsClientBuilder;
import com.azure.ai.agents.models.AgentVersionDetails;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.identity.DefaultAzureCredentialBuilder;
public class CreateAgent {
public static void main(String[] args) {
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
String foundryProjectEndpoint = "your_project_endpoint";
String foundryAgentName = "your-agent-name";
// Create agents client to call Foundry API
AgentsClient agentsClient = new AgentsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(foundryProjectEndpoint)
.buildAgentsClient();
// Create an agent with a model and instructions
PromptAgentDefinition request = new PromptAgentDefinition("gpt-5-mini") // supports all Foundry direct models
.setInstructions("You are a helpful assistant that answers general questions");
AgentVersionDetails agent = agentsClient.createAgentVersion(foundryAgentName, request);
System.out.println("Agent ID: " + agent.getId());
System.out.println("Agent Name: " + agent.getName());
System.out.println("Agent Version: " + agent.getVersion());
}
}
Replace
YOUR-FOUNDRY-RESOURCE-NAME with your values:curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/agents?api-version=2025-11-15-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"name": "MyAgent",
"definition": {
"kind": "prompt",
"model": "gpt-4.1-mini",
"instructions": "You are a helpful assistant that answers general questions"
}
}'
Now create an agent and interact with it.
- Still in the Build section, select Agents in the left pane.
- Select Create agent and give it a name, such as “MyAgent”.
Code uses Azure AI Projects 2.x and is incompatible with Azure AI Projects 1.x. See the Foundry (classic) documentation for the Azure AI Projects 1.x version.
Chat with an agent
Use the previously created agent named “MyAgent” to interact by asking a question and a related follow-up. The conversation maintains history across these interactions.- Python
- C#
- TypeScript
- Java
- REST API
- Foundry portal
import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
load_dotenv()
project_client = AIProjectClient(
endpoint=os.environ["PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
agent_name = os.environ["AGENT_NAME"]
openai_client = project_client.get_openai_client()
# Optional Step: Create a conversation to use with the agent
conversation = openai_client.conversations.create()
print(f"Created conversation (id: {conversation.id})")
# Chat with the agent to answer questions
response = openai_client.responses.create(
conversation=conversation.id, #Optional conversation context for multi-turn
extra_body={"agent_reference": {"name": agent_name, "type": "agent_reference"}},
input="What is the size of France in square miles?",
)
print(f"Response output: {response.output_text}")
# Optional Step: Ask a follow-up question in the same conversation
response = openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent_name, "type": "agent_reference"}},
input="And what is the capital city?",
)
print(f"Response output: {response.output_text}")
using Azure.AI.Projects;
using Azure.AI.Projects.OpenAI;
using Azure.Identity;
using OpenAI.Responses;
#pragma warning disable OPENAI001
string projectEndpoint = Environment.GetEnvironmentVariable("PROJECT_ENDPOINT")
?? throw new InvalidOperationException("Missing environment variable 'PROJECT_ENDPOINT'");
string modelDeploymentName = Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME")
?? throw new InvalidOperationException("Missing environment variable 'MODEL_DEPLOYMENT_NAME'");
string agentName = Environment.GetEnvironmentVariable("AGENT_NAME")
?? throw new InvalidOperationException("Missing environment variable 'AGENT_NAME'");
AIProjectClient projectClient = new(new Uri(projectEndpoint), new AzureCliCredential());
// Optional Step: Create a conversation to use with the agent
ProjectConversation conversation = projectClient.OpenAI.Conversations.CreateProjectConversation();
ProjectResponsesClient responsesClient = projectClient.OpenAI.GetProjectResponsesClientForAgent(
defaultAgent: agentName,
defaultConversationId: conversation.Id);
// Chat with the agent to answer questions
ResponseResult response = responsesClient.CreateResponse("What is the size of France in square miles?");
Console.WriteLine(response.GetOutputText());
// Optional Step: Ask a follow-up question in the same conversation
response = responsesClient.CreateResponse("And what is the capital city?");
Console.WriteLine(response.GetOutputText());
import { DefaultAzureCredential } from "@azure/identity";
import { AIProjectClient } from "@azure/ai-projects";
import "dotenv/config";
const projectEndpoint = process.env["PROJECT_ENDPOINT"] || "<project endpoint>";
const deploymentName = process.env["MODEL_DEPLOYMENT_NAME"] || "<model deployment name>";
async function main(): Promise<void> {
const project = new AIProjectClient(projectEndpoint, new DefaultAzureCredential());
const openAIClient = project.getOpenAIClient();
// Create agent
console.log("Creating agent...");
const agent = await project.agents.createVersion("my-agent-basic", {
kind: "prompt",
model: deploymentName,
instructions: "You are a helpful assistant that answers general questions",
});
console.log(`Agent created (id: ${agent.id}, name: ${agent.name}, version: ${agent.version})`);
// Create conversation with initial user message
// You can save the conversation ID to database to retrieve later
console.log("\nCreating conversation with initial user message...");
const conversation = await openAIClient.conversations.create({
items: [
{ type: "message", role: "user", content: "What is the size of France in square miles?" },
],
});
console.log(`Created conversation with initial user message (id: ${conversation.id})`);
// Generate response using the agent
console.log("\nGenerating response...");
const response = await openAIClient.responses.create(
{
conversation: conversation.id,
},
{
body: { agent: { name: agent.name, type: "agent_reference" } },
},
);
console.log(`Response output: ${response.output_text}`);
// Clean up
console.log("\nCleaning up resources...");
await openAIClient.conversations.delete(conversation.id);
console.log("Conversation deleted");
await project.agents.deleteVersion(agent.name, agent.version);
console.log("Agent deleted");
}
main().catch(console.error);
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.AgentsClient;
import com.azure.ai.agents.AgentsClientBuilder;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class ChatWithAgent {
public static void main(String[] args) {
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
String foundryProjectEndpoint = "your_project_endpoint";
String foundryAgentName = "your-agent-name";
AgentsClientBuilder builder = new AgentsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(foundryProjectEndpoint);
// Create the agent (or a new version, if it already exists)
AgentsClient agentsClient = builder.buildAgentsClient();
PromptAgentDefinition agentDefinition = new PromptAgentDefinition("gpt-5-mini") // supports all Foundry direct models
.setInstructions("You are a helpful assistant that answers general questions");
agentsClient.createAgentVersion(foundryAgentName, agentDefinition);
// Create an OpenAI client bound to the agent endpoint
OpenAIClient openai = builder.buildAgentScopedOpenAIClient(foundryAgentName);
// Create a conversation for multi-turn chat
Conversation conversation = openai.conversations().create();
// Chat with the agent to answer questions
Response response = openai.responses().create(
ResponseCreateParams.builder()
.conversation(conversation.id())
.input("What is the size of France in square miles?")
.build());
printResponse(response);
// Ask a follow-up question in the same conversation
Response followUp = openai.responses().create(
ResponseCreateParams.builder()
.conversation(conversation.id())
.input("And what is the capital city?")
.build());
printResponse(followUp);
}
private static void printResponse(Response response) {
response.output().forEach(item -> item.message().ifPresent(message ->
message.content().forEach(content -> content.outputText().ifPresent(
text -> System.out.println(text.text())))));
}
}
Replace
YOUR-FOUNDRY-RESOURCE-NAME with your values:# Optional Step: Create a conversation to use with the agent
curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/openai/conversations?api-version=2025-11-15-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{}'
# Lets say Conversation ID created is conv_123456789. Use this in the next step
#Chat with the agent to answer questions
curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/openai/responses?api-version=2025-11-15-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"agent": {"type": "agent_reference", "name": "MyAgent"},
"conversation" : "<YOUR_CONVERSATION_ID>",
"input" : "What is the size of France in square miles?"
}'
#Optional Step: Ask a follow-up question in the same conversation
curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/openai/responses?api-version=2025-11-15-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"agent": {"type": "agent_reference", "name": "MyAgent"},
"conversation" : "<YOUR_CONVERSATION_ID>",
"input" : "And what is the capital city?"
}'
Interact with your agent.
- Add instructions, such as, “You are a helpful writing assistant.”
- Start chatting with your agent, for example, “Write a poem about the sun.”
- Follow up with “How about a haiku?”
Code uses Azure AI Projects 2.x and is incompatible with Azure AI Projects 1.x. See the Foundry (classic) documentation for the Azure AI Projects 1.x version.
Clean up resources
If you no longer need any of the resources you created, delete the resource group associated with your project.- In the Azure portal, select the resource group, and then select Delete. Confirm that you want to delete the resource group.