# Agent Outcomes

In xFlow, the AI Agent State leverages Agent Outcomes to define the actions that should be taken based on the results produced by the AI agent. This documentation explains the properties of Agent Outcomes, their purpose, and how they can be configured to ensure that the workflow transitions correctly based on the AI agent's output.

### Agent Outcome Properties

Agent Outcomes are critical for defining the behavior of the AI agent within a workflow. They specify conditions, actions, and transitions that should occur based on the AI agent's responses. Below are the key properties of Agent Outcomes, along with examples for each.

#### Condition

* **Name**: `condition`
* **Type**: `string`
* **Description**: An expression that must evaluate to true for this outcome to be matched. If the condition evaluates to false, the outcome is disregarded. The condition is typically used to check the AI agent's output or any relevant workflow data. These expressions follow the [Serverless Workflow Specification for Workflow Expressions](https://github.com/serverlessworkflow/specification/blob/0.9.x/specification.md#Workflow-Expressions).
* **Example**:

  ```yaml
  condition: '${ $agentOutcome.returnValues.response != null }'
  ```

#### Finish

* **Name**: `finish`
* **Type**: `boolean`
* **Default**: `false`
* **Description**: If set to `true`, the agent will finish after this action is executed. If set to `false`, the agent will continue to process other actions or outcomes. This property is used to control whether the workflow should proceed further with the AI agent's execution.
* **Example**:

  ```yaml
  finish: true
  ```

#### Data Filter

* **Name**: `dataFilter`
* **Type**: `object`
* **Description**: Defines the data filter to be applied to the event data. Data filters are used to control which parts of the data should be passed along to subsequent workflow states. More details can be found in the [Event Data Filter Specification](https://docs.a4b.vn/xflow/3.-core-concepts/workflow-data-handling#event-data-filters).
* **Example**:

  ```yaml
  dataFilter: 
    useData: true
    dataOutputPath: '${ .response }'
  ```

#### Transition

* **Name**: `transition`
* **Type**: `object`
* **Description**: Defines the outgoing transition when the outcome is selected. If not defined, the default transition will be used. The transition specifies the next state to move to in the workflow.
* **Example**:

  ```yaml
  transition: 
    nextState: "SuccessState"
  ```

### Purpose of Agent Outcomes

Agent Outcomes serve several important purposes in the context of AI agents within xFlow:

1. **Conditional Execution**: By defining conditions, workflows can execute specific actions only when certain criteria are met. This allows for more dynamic and responsive workflows that can adapt to different AI agent outputs.
2. **Workflow Control**: The `finish` property allows for precise control over the workflow's execution flow, determining whether the AI agent should continue processing or terminate after a certain outcome.
3. **Data Management**: The `dataFilter` property ensures that only relevant data is passed to subsequent states, reducing unnecessary data processing and improving workflow efficiency.
4. **State Transitions**: The `transition` property clearly defines the next steps in the workflow, ensuring smooth and logical progression based on the AI agent's outcomes.

### Usage Example

Here is an example of how to configure Agent Outcomes in an AI Agent State within a xFlow workflow:

```yaml
- name: ExampleAIState
  type: aiagent
  agentName: ExampleAgent
  aiModel: gpt-4o
  systemMessage: "You are an assistant designed to provide accurate answers."
  userMessage: '${ "User: " + .request.question }'
  output: 
    schema: 
      type: "object"
      properties: 
        response: 
          type: "string"
          description: "The AI's response to the user question"
      required: ["response"]
  maxToolExecutions: 5
  memory: 
    memoryId: "session123"
    memoryType: "message_window"
    maxMessages: 10
  tools:
    - name: SEARCH_DOCUMENTS
      description: "Search for relevant documents based on the user's query."
      parameters: 
        schema: 
          type: "object"
          properties: 
            query: 
              type: "string"
              description: "The search query"
          required: ["query"]
      output: 
        schema: 
          type: "object"
          properties: 
            documents: 
              type: "array"
              items: 
                type: "string"
                format: "uri"
          required: ["documents"]
      execution:
        actionMode: "sequential"
        actions:
          - name: "FetchDocuments"
            functionRef:
              refName: "FetchDocumentsFunction"
              arguments:
                query: "${ .request.query }"
  agentOutcomes:
    - condition: '${ $agentOutcome.returnValues.response != null }'
      finish: false
      transition: 
        nextState: "SuccessState"
    - condition: '${ $agentOutcome.returnValues.response == null }'
      finish: true
      transition: 
        nextState: "ErrorState"
```

For more detailed information and advanced configurations, refer to the [AI Agent Development Documentation](https://docs.a4b.vn/xflow/4.-developer-guide/ai-agent-development).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.a4b.vn/xflow/developer-guide/ai-agent-development/ai-agent/agent-outcomes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
