# AgentServiceFactory

> **Class** in `langchain_azure_ai`

📖 [View in docs](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/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:

```python
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:

```python
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:

```python
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``.

```python
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())],
)
```

## Signature

```python
AgentServiceFactory()
```

## Extends

- `BaseModel`

## Properties

- `model_config`
- `project_endpoint`
- `credential`
- `api_version`
- `client_kwargs`

## Methods

- [`validate_environment()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/validate_environment)
- [`delete_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/delete_agent)
- [`get_agents_id_from_graph()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/get_agents_id_from_graph)
- [`get_agent_node()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/get_agent_node)
- [`create_prompt_agent_node()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/create_prompt_agent_node)
- [`create_prompt_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v2/prebuilt/factory/AgentServiceFactory/create_prompt_agent)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/7a6a664d617264df8dcb12fcfb1bacc09f21d504/libs/azure-ai/langchain_azure_ai/agents/_v2/prebuilt/factory.py#L228)