> For the complete documentation index, see [llms.txt](https://docs.a4b.vn/xflow/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.a4b.vn/xflow/developer-guide/workflow-states-reference/aiagentproxy-state.md). Agents can also search and read this documentation through its [MCP server](https://docs.a4b.vn/xflow/~gitbook/mcp).

# AIAgentProxy State

The `AIAgentProxyState` is used to define an AI Agent Proxy state within a serverless workflow. This state facilitates interaction with an AI agent, allowing for user messages, system prompts, and agent outcomes to be managed within a workflow.

### AIAgentProxyState

The `AIAgentProxyState` is a specialized state in a serverless workflow designed to integrate AI agents developed by any AI framework. The state defines the agent's configuration, including the integration details, possible outcomes, and other properties.

<table data-header-hidden><thead><tr><th width="174"></th><th></th><th width="116"></th><th></th></tr></thead><tbody><tr><td><strong>Parameter</strong></td><td><strong>Description</strong></td><td><strong>Type</strong></td><td><strong>Required</strong></td></tr><tr><td>agentName</td><td>The name of the agent.</td><td>string</td><td>yes</td></tr><tr><td>systemMessage</td><td>The system message used to construct the LLM prompt.</td><td>string</td><td>no</td></tr><tr><td>userMessage</td><td>The user message.</td><td>string</td><td>yes</td></tr><tr><td>memoryId</td><td>The memory ID to store the agent history. If not specified, the workflow process instance scope is used.</td><td>string</td><td>no</td></tr><tr><td><a href="#onagentoutcome">agentOutcomes</a></td><td>Define list of agent outcomes. Each outcome is described by the <a href="#onagentoutcome">OnAgentOutcome</a> schema.</td><td>array</td><td>yes</td></tr><tr><td><a href="#agentproxyexecution">execution</a></td><td>Agent proxy execution described by the AgentProxyExecution schema.</td><td>object</td><td>yes</td></tr><tr><td><a href="https://docs.a4b.vn/xflow/3.-core-concepts/workflow-data-handling#state-data-filters">dataFilter</a></td><td>Filter to apply to the state data.</td><td>string</td><td>no</td></tr></tbody></table>

### OnAgentOutcome

The `OnAgentOutcome` object defines the actions to be performed based on the outcome of the AI agent. The actions are executed if the outcome matches the specified conditions.

<table data-header-hidden><thead><tr><th width="137"></th><th width="274"></th><th width="137"></th><th></th></tr></thead><tbody><tr><td><strong>Field</strong></td><td><strong>Description</strong></td><td><strong>Type</strong></td><td><strong>Required</strong></td></tr><tr><td>condition</td><td>Expression, if defined, must evaluate to true for this outcome to be matched. If false, the outcome is disregarded.</td><td>string</td><td>no</td></tr><tr><td>finish</td><td>If true, the agent will finish after this action is executed. If false, the agent will continue to process.</td><td>boolean</td><td>no</td></tr><tr><td><a href="https://docs.a4b.vn/xflow/3.-core-concepts/workflow-data-handling#event-data-filters">dataFilter</a></td><td>Event data filter described by the <a href="https://docs.a4b.vn/xflow/3.-core-concepts/workflow-data-handling#event-data-filters">EventDataFilter</a> schema.</td><td>object</td><td>no</td></tr><tr><td><a href="https://github.com/serverlessworkflow/specification/blob/0.9.x/specification.md#Transitions">transition</a></td><td>The outgoing transition when the outcome is selected, if not defined the default transition will be used.</td><td>object</td><td>no</td></tr></tbody></table>

### AgentProxyExecution

The `AgentProxyExecution` object specifies how the AI Agent are to be invoked by the AI Agent Proxy. The function will be executed and provided with 3 arguments: `workflowId`, `agentConfig`, and `agentContext`. (See [AI Agent Proxy Integration](https://docs.a4b.vn/xflow/developer-guide/ai-agent-development/ai-agent-proxy/ai-agents-integration.md))

<table data-header-hidden><thead><tr><th width="149"></th><th width="298"></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Field</strong></td><td><strong>Description</strong></td><td><strong>Type</strong></td><td><strong>Required</strong></td></tr><tr><td><a href="https://github.com/serverlessworkflow/specification/blob/0.9.x/specification.md#functionref-definition">functionRef</a></td><td>References a reusable function definition to be invoked</td><td>object</td><td>yes</td></tr></tbody></table>

**Example:**

<table><thead><tr><th>YAML</th><th data-hidden>JSON</th></tr></thead><tbody><tr><td><pre class="language-yaml" data-line-numbers><code class="lang-yaml">states:
  - name: RagAgentProxy
    agentName: RagAgentProxy
    type: aiagentproxy
    systemMessage: You are a helpful assistant
    userMessage: '${ .request.question }'
    execution:
      functionRef:
        refName: "ExecuteRagAgentProxy"
    agentOutcomes:
    - condition: '${ true }'
      finish: true
      transition: InformRagAgentProxyResult
  - name: InformRagAgentProxyResult
    agentName: InformRagAgentProxyResult
    type: userproxyagent
    agentRole: relay
    userMessage: '${ .request.question }'
    relayMessage:
      text: ${ ._ai_agent_state.RagAgentProxy.agentOutcome.finish.output.message }
      mediaExp: ${ ._ai_agent_state.RagAgentProxy.agentOutcome.finish.output.images }
    output: |-
      {
          "type": "object",
          "properties": {
              "answer": {
                  "type": "string"
              }
          },
          "required": ["answer"]
      }
    agentOutcomes:
    - condition: '${ true }'
      finish: true
      transition: Finish
functions:
  - name: ExecuteRagAgentProxy
    type: custom
    operation: ExecuteQueryAction
    metadata:
      libraryQueryName: post.rag_agent_proxy
      libraryQueryRecordId: latest
      params: '["workflowId", "agentConfig", "agentContext"]'
</code></pre></td><td><pre class="language-json" data-line-numbers><code class="lang-json">{
    "states": [
        {
            "name": "RagAgentProxy",
            "agentName": "RagAgentProxy",
            "type": "aiagent",
            "agentType": "agent_proxy",
            "aiModel": "gpt-4o",
            "systemMessage": "You are a helpful assistant",
            "userMessage": "${ .request.question }",
            "agentProxy": {
                "functionRef": {
                    "refName": "ExecuteRagAgentProxy"
                }
            },
            "tools": [],
            "agentOutcomes": [
                {
                    "condition": "${ true }",
                    "finish": true,
                    "transition": "InformRagAgentProxyResult"
                }
            ]
        },
        {
            "name": "InformRagAgentProxyResult",
            "agentName": "InformRagAgentProxyResult",
            "type": "userproxyagent",
            "agentRole": "relay",
            "userMessage": "${ .request.question }",
            "conversationId": "${ .request.userChannelContext.conversationId }",
            "userChannelContext": "${ .request.userChannelContext }",
            "userContext": "${ .request.userContext }",
            "relayMessage": {
                "text": "${ ._ai_agent_state.RagAgentProxy.agentOutcome.finish.output.message }",
                "mediaExp": "${ ._ai_agent_state.RagAgentProxy.agentOutcome.finish.output.images }"
            },
            "output": "{\n    \"type\": \"object\",\n    \"properties\": {\n        \"answer\": {\n            \"type\": \"string\"\n        }\n    },\n    \"required\": [\"answer\"]\n}",
            "agentOutcomes": [
                {
                    "condition": "${ true }",
                    "finish": true,
                    "transition": "Finish"
                }
            ]
        }
    ]
}
</code></pre></td></tr></tbody></table>

***

This document provides a detailed view of the `AIAgentProxyState` state and its related objects, including comprehensive schema definitions, required fields, and descriptions for each attribute within the `AIAgentProxyState` and associated schemas. This specification ensures clarity and completeness for integrating AI agents within serverless workflows.
