Skip to main content
In this quickstart, you create a prompt agent in Foundry Agent Service and have a conversation with it. A prompt agent is a declaratively defined agent that combines a model from the Foundry model catalog, instructions, tools, and natural language prompts to drive behavior. If you don’t have an Azure subscription, create a free account.

Prerequisites

Set environment variables

Store your project endpoint as an environment variable. Also set these values for use in your scripts. Python and JavaScript
PROJECT_ENDPOINT=<endpoint copied from welcome screen>
AGENT_NAME="MyAgent"
C# and Java
ProjectEndpoint = <endpoint copied from welcome screen>
AgentName = "MyAgent"

Install packages and authenticate

Make sure you install the correct version of the packages as shown here.
  1. Install the current version of azure-ai-projects. This version uses the Foundry projects (new) API .
    pip install azure-ai-projects>=2.0.0
    
  2. Sign in using the CLI az login command to authenticate before running your Python scripts.
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 a prompt agent

Create a prompt agent using your deployed model. The agent uses a PromptAgentDefinition with instructions that define the agent’s behavior. You can update or delete agents anytime.
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})")
The output confirms the agent was created. You see the agent name and ID printed to the console.

Chat with the agent

Use the agent you created to interact by asking a question and a related follow-up. The conversation maintains history across these interactions.
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}")
You see the agent’s responses to both prompts. The follow-up response demonstrates that the agent maintains conversation history across turns.

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.