# AgentServiceFactory

> **Class** in `langchain_azure_ai`

📖 [View in docs](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory)

Factory to create and manage prompt-based agents in Azure AI Foundry.

To create a simple echo agent:

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

!!! 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.

Agents can also be created with tools. For example, to create an agent that
can perform arithmetic using a calculator tool:

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

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

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

## 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/_v1/agent_service/AgentServiceFactory/validate_environment)
- [`delete_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory/delete_agent)
- [`get_agents_id_from_graph()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory/get_agents_id_from_graph)
- [`update_thread_resources()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory/update_thread_resources)
- [`create_prompt_agent_node()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory/create_prompt_agent_node)
- [`create_prompt_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/_v1/agent_service/AgentServiceFactory/create_prompt_agent)

## ⚠️ Deprecated

Deprecated since version 1.1.0. Use langchain_azure_ai.agents.AgentServiceFactory instead. `langchain_azure_ai.agents.v1.*` uses `azure-ai-agents` library which is deprecated. Use `langchain_azure_ai.agents.AgentServiceFactory` instead, which uses the new `azure-ai-projects` library.

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/7a6a664d617264df8dcb12fcfb1bacc09f21d504/libs/azure-ai/langchain_azure_ai/agents/_v1/agent_service.py#L59)