AgentServiceFactory(Factory to create and manage prompt-based agents in Azure AI Foundry.
To create a simple echo agent:
from langchain_azure_ai.agents import AgentServiceFactory
from langchain_core.messages import HumanMessage
from azure.identity import DefaultAzureCredential
factory = AgentServiceFactory(
project_endpoint=(
"https://resource.services.ai.azure.com/api/projects/demo-project",
),
credential=DefaultAzureCredential()
)
agent = factory.create_prompt_agent(
name="my-echo-agent",
model="gpt-4.1",
instructions="You are a helpful AI assistant that always replies back
"saying the opposite of what the user says.",
)
messages = [HumanMessage(content="I'm a genius and I love programming!")]
state = agent.invoke({"messages": messages})
for m in state['messages']:
m.pretty_print()
You can also create AgentServiceFactory without passing any parameters
if you have set the AZURE_AI_PROJECT_ENDPOINT environment variable and
are using DefaultAzureCredential for authentication.
Agents can also be created with tools. For example, to create an agent that can perform arithmetic using a calculator tool:
# add, multiply, divide are simple functions defined elsewhere
# those functions are documented and with proper type hints
tools = [add, multiply, divide]
agent = factory.create_prompt_agent(
name="math-agent",
model="gpt-4.1",
instructions="You are a helpful assistant tasked with performing "
"arithmetic on a set of inputs.",
tools=tools,
)
You can also use the built-in tools in the Agent Service. Those tools only work with agents created in Azure AI Foundry. For example, to create an agent that can use Code Interpreter.
from langchain_azure_ai.agents.prebuilt.tools import AgentServiceBaseTool
from azure.ai.agents.models import CodeInterpreterTool
# Upload a file first using the Azure AI Agents SDK
agents_client = project_client.agents
file = agents_client.files.upload_and_poll(
file_path="data.csv", purpose=FilePurpose.AGENTS
)
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
agent = factory.create_prompt_agent(
name="code-interpreter-agent",
model="gpt-4.1",
instructions="You are a helpful assistant that can run complex "
"mathematical functions precisely via tools.",
tools=[AgentServiceBaseTool(tool=code_interpreter)],
)
state = agent.invoke({"messages": [HumanMessage(content="Summarize the data.")]})
To add files to an ongoing conversation after the agent has been invoked at
least once, use update_thread_resources:
from azure.ai.agents.models import (
CodeInterpreterToolResource,
ToolResources,
)
new_file = agents_client.files.upload_and_poll(
file_path="more_data.csv", purpose=FilePurpose.AGENTS
)
factory.update_thread_resources(
agent,
ToolResources(
code_interpreter=CodeInterpreterToolResource(
file_ids=[new_file.id]
)
),
)The project endpoint associated with the AI project. If this is specified,
then the endpoint parameter becomes optional and credential has to be of type
TokenCredential.
The API key or credential to use to connect to the service. If using a project
endpoint, this must be of type TokenCredential since only Microsoft EntraID is
supported.
The API version to use with Azure. If None, the default version is used.
Additional keyword arguments to pass to the client.
Validate that required values are present in the environment.
Delete an agent created with create_prompt_agent.
Get the Azure AI Foundry agent associated with a state graph.
Update tool resources on the active conversation thread of an agent.
Use this method to add or replace file resources (e.g. for
CodeInterpreterTool) on an already-running conversation thread,
enabling mid-conversation file uploads.
Create a prompt-based agent node in Azure AI Foundry.
This method creates a new agent in Azure AI Foundry and returns a
:class:~langchain_azure_ai.agents._v1.prebuilt.declarative.PromptBasedAgentNode
that references it. The node itself does not perform any creation; it
only holds a reference to the existing agent and handles request/response
building.
Create a prompt-based agent in Azure AI Foundry.