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.
Note
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())],
)