AgentServiceFactory()Builds agent nodes from agents running in Azure AI Foundry.
You can create or deploy agents in the Azure AI Foundry Agent Service and then reference agents from LangGraph to compose complex workflows. This factory provides methods to create new agents in the foundry and to get references to existing agents as LangGraph nodes.
To reference an existing agent version in the foundry, use:
from langchain_azure_ai.agents import AgentServiceFactory
from azure.identity import DefaultAzureCredential
factory = AgentServiceFactory(
project_endpoint=(
"https://resource.services.ai.azure.com/api/projects/demo-project"
),
credential=DefaultAzureCredential(),
)
agent_node = factory.get_agent_node(
name="my-existing-agent",
version="latest",
)
Then you can use the returned agent_node in your LangGraph workflows. The
ResponsesAgentNode will handle invoking the agent in the foundry and returning
the responses as LangGraph messages.
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.
To create a new prompt agent in the foundry and get a node referencing it, use:
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()
Agents can also be created with tools:
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,
)
To indicate builtin tools from the service, use the namespace
langchain_azure_ai.agents.prebuilt.tools.
from langchain_azure_ai.agents.prebuilt.tools import (
CodeInterpreterTool,
)
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=[CodeInterpreterTool(CodeInterpreterToolAuto())],
)The project endpoint associated with the AI project.
The credential to use. Must be of type TokenCredential.
The API version to use. If None, the default is used.
Additional keyword arguments for the client.
Validate required environment values.
Delete an agent created with create_prompt_agent.
Get agent IDs (name:version) from a compiled state graph.
Get a reference to an existing agent version as a ResponsesAgentNode.
Create a prompt-based agent node using V2.
This method creates a new agent version in Azure AI Foundry and returns a
:class:~langchain_azure_ai.agents._v2.base.ResponsesAgentNode
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 using V2.