# Orchestrate an AI agent

> Build an autonomous agent on the canvas, pass data to it, and route workflow execution based on the agent's decision.

**What you'll build:** A flow that invokes an AI agent you author on the canvas, passes data to it, and routes execution based on the agent's response (approved, rejected, or failed). This is the foundation for any agentic automation: a reasoning-powered decision embedded in a larger automated process.

The agent you build here is an **Autonomous Agent**: its model, prompts, tools, and context all live on the node and travel with the flow. To reuse an agent that's already published in your organization instead of authoring one inline, use the [BPMN process](node-bpmn-process.md) node instead.

## What you'll need

- A UiPath Automation Cloud account with access to Maestro Flow.
- An available LLM model for the agent to use.

## Nodes used

- [Manual Trigger](node-manual-trigger.md): starts the flow on demand while you build
- [Agent](node-agent.md): an AI agent you author on the canvas, returns its output
- [Decision](node-decision.md): branches execution based on the agent's response
- [Terminate](node-terminate.md): ends the flow on a failure path

## Steps

### 1. Create a new flow and add a trigger

1. Open **Maestro** → **Flow** and select **New Flow**.
2. Drag a **Manual Trigger** node onto the canvas.

For a production flow, you'd replace this with a scheduled or integration trigger. A Manual Trigger is enough while you build.

### 2. Add an Agent node

The agent's palette label is **Autonomous Agent**.

1. Drag an **Agent** node from the **UiPath nodes** section of the node panel onto the canvas.
2. Connect it to the Manual Trigger.
3. In the properties panel, author the agent:
   - Set the **Model**, or leave it on your organization's default agent model.
   - Write the **System prompt**, the agent's role and standing instructions. The Model, System prompt, and User prompt are all required.
   - Write the **User prompt**, the task for this run. Reference flow data with expressions; for example, the document text to classify: `$vars.start.output.documentText`.

![Canvas with a Manual Trigger connected to an Autonomous Agent node, and the agent's properties panel open showing the Model, System prompt, and User prompt fields.](https://dev-assets.cms.uipath.com/assets/images/maestro/flow-autonomous-agent-e8d2b4e2.webp)

### 3. Return a structured result

By default the agent returns one field, `content`, holding its response text. To branch cleanly, have the agent return a structured field instead.

1. Open the **Variables** tab on the agent's properties panel.
2. Add an output variable named `decision` with type `string`.
3. In the **System prompt**, instruct the agent to set `decision` to `approved` or `rejected`.

### 4. Handle the agent's response

1. Drag a **Decision** node onto the canvas and connect it to the Agent node.
2. Set the condition to evaluate the agent's output, for example: `$vars.agent1.output.decision === "approved"`.
3. Connect the **True** branch to the next step in your automation (for example, a notification or data write).
4. Connect the **False** branch to a **Terminate** node.
5. Set the Terminate status to `Failed` and add a message such as `Agent returned: $vars.agent1.output.decision`.

### 5. Debug the flow

Select **Debug** in the toolbar. In the execution trace:

- Select the **Agent** node to inspect what inputs were sent and what the agent returned.
- Confirm the **Decision** node took the correct branch given the agent's output.
- If the agent's response isn't what you expected, review the **System prompt**, **User prompt**, and declared output before changing unrelated flow logic.

To exercise both branches without waiting for real agent responses, enable **Mock output** on the Agent node and set a fixed response.

## Result

Your flow runs end-to-end: the Manual Trigger starts execution, the Agent node reasons over the input and returns a structured `decision` field, the Decision node branches on the value, and the Terminate node ends the flow on the rejected path. You can inspect the agent's inputs and outputs in the execution trace and adjust the prompts directly on the node.

## Extend this flow

- **Give the agent tools**: connect tool resources (built-in tools, connector tools, RPA or API workflow tools, document extraction, or Model Context Protocol (MCP) servers) to the agent's **Tools** handle so it can act, not just reason.
- **Ground the agent in your data**: connect a context resource to the agent's **Context** handle so it answers from an indexed knowledge source instead of the model alone.
- **Put a human in the loop**: connect the agent's **Escalations** handle to an **Escalation** resource for cases the agent shouldn't decide alone.
- **Add a loop**: wrap the agent invocation in a [Loop](node-loop.md) node to process a list of items with the same agent.
- **Chain to another system**: after the Decision node's True branch, add an [HTTP Request](node-http-request.md) or integration node to write the result to an external system.
