Agents V1 integrated with LangChain and LangGraph.
Determine the next node based on whether the AI message contains tool calls.
Prebuilt agents for Azure AI Foundry.
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]
)
),
)