

# Handle fulfillment of the action
<a name="action-handle"></a>

When you configure the action group, you also select one of the following options for the agent to pass the information and parameters that it receives from the user:
+ Add user input to your agent's action group. With user input, agent can [request user for more information](agents-user-input.md) if it doesn't have enough information to complete a task. 
+ Pass to a [Lambda function that you create](agents-lambda.md) to define the business logic for the action group.
+ Skip using a Lambda function and [return control](agents-returncontrol.md) by passing the information and parameters from the user in the `InvokeAgent` response. The information and parameters can be sent to your own systems to yield results and these results can be sent in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_SessionState.html#bedrock-Type-agent-runtime_SessionState](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_SessionState.html#bedrock-Type-agent-runtime_SessionState) of another [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) request.
+ Enable user confirmation for an action. Enabling user confirmation can safeguard your application from malicious prompt injections by [requesting confirmation from your application users](agents-userconfirmation.md) before invoking the action group function. 

Select a topic to learn how to configure how fulfillment of the action group is handled after the necessary information has been elicited from the user.

**Topics**
+ [Configure Lambda functions to send information that an Amazon Bedrock agent elicits from the user](agents-lambda.md)
+ [Return control to the agent developer by sending elicited information in an InvokeAgent response](agents-returncontrol.md)
+ [Get user confirmation before invoking action group function](agents-userconfirmation.md)

# Configure Lambda functions to send information that an Amazon Bedrock agent elicits from the user
<a name="agents-lambda"></a>

You can define a Lambda function to program the business logic for an action group. After an Amazon Bedrock agent determines the API operation that it needs to invoke in an action group, it sends information from the API schema alongside relevant metadata as an input event to the Lambda function. To write your function, you must understand the following components of the Lambda function:
+ **Input event** – Contains relevant metadata and populated fields from the request body of the API operation or the function parameters for the action that the agent determines must be called.
+ **Response** – Contains relevant metadata and populated fields for the response body returned from the API operation or the function.

You write your Lambda function to define how to handle an action group and to customize how you want the API response to be returned. You use the variables from the input event to define your functions and return a response to the agent.

**Note**  
An action group can contain up to 11 API operations, but you can only write one Lambda function. Because the Lambda function can only receive an input event and return a response for one API operation at a time, you should write the function considering the different API operations that may be invoked.

For your agent to use a Lambda function, you must attach a resource-based policy to the function to provide permissions for the agent. For more information, follow the steps at [Resource-based policy to allow Amazon Bedrock to invoke an action group Lambda function](agents-permissions.md#agents-permissions-lambda). For more information about resource-based policies in Lambda, see [Using resource-based policies for Lambda](https://docs.aws.amazon.com/lambda/latest/dg/access-control-resource-based.html) in the AWS Lambda Developer Guide.

To learn how to define a function while creating the action group, see [Add an action group to your agent in Amazon Bedrock](agents-action-add.md).

**Topics**
+ [Lambda input event from Amazon Bedrock](#agents-lambda-input)
+ [Lambda response event to Amazon Bedrock](#agents-lambda-response)
+ [Action group Lambda function example](#agents-lambda-example)

## Lambda input event from Amazon Bedrock
<a name="agents-lambda-input"></a>

When an action group using a Lambda function is invoked, Amazon Bedrock sends a Lambda input event of the following general format. You can define your Lambda function to use any of the input event fields to manipulate the business logic within the function to successfully perform the action. For more information about Lambda functions, see [Event-driven invocation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html#event-driven-invocation) in the AWS Lambda Developer Guide.

The input event format depends on whether you defined the action group with an API schema or with function details:
+ If you defined the action group with an API schema, the input event format is as follows:

  ```
  {
      "messageVersion": "1.0",
      "agent": {
          "name": "string",
          "id": "string",
          "alias": "string",
          "version": "string"
      },
      "inputText": "string",
      "sessionId": "string",
      "actionGroup": "string",
      "apiPath": "string",
      "httpMethod": "string",
      "parameters": [
          {
              "name": "string",
              "type": "string",
              "value": "string"
          },
      ...
      ],
      "requestBody": {
          "content": {
              "<content_type>": {
                  "properties": [
                     {
                         "name": "string",
                         "type": "string",
                         "value": "string"
                      },
                              ...
                  ]
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      }
  }
  ```
+ If you defined the action group with function details, the input event format is as follows:

  ```
  {
      "messageVersion": "1.0",
      "agent": {
          "name": "string",
          "id": "string",
          "alias": "string",
          "version": "string"
      },
      "inputText": "string",
      "sessionId": "string",
      "actionGroup": "string",
      "function": "string",
      "parameters": [
          {
              "name": "string",
              "type": "string",
              "value": "string"
          },
      ...
      ],
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      }
  }
  ```

The following list describes the input event fields;
+ `messageVersion` – The version of the message that identifies the format of the event data going into the Lambda function and the expected format of the response from a Lambda function. Amazon Bedrock only supports version 1.0.
+ `agent` – Contains information about the name, ID, alias, and version of the agent that the action group belongs to.
+ `inputText` – The user input for the conversation turn.
+ `sessionId` – The unique identifier of the agent session.
+ `actionGroup` – The name of the action group.
+ `parameters` – Contains a list of objects. Each object contains the name, type, and value of a parameter in the API operation, as defined in the OpenAPI schema, or in the function.
+ If you defined the action group with an API schema, the input event contains the following fields:
  + `apiPath` – The path to the API operation, as defined in the OpenAPI schema.
  + `httpMethod` – The method of the API operation, as defined in the OpenAPI schema.
  + `requestBody` – Contains the request body and its properties, as defined in the OpenAPI schema for the action group.
+ If you defined the action group with function details, the input event contains the following field:
  + `function` – The name of the function as defined in the function details for the action group.
+ `sessionAttributes` – Contains [session attributes](agents-session-state.md) and their values. These attributes are stored over a [session](advanced-prompts.md#advanced-prompts-terminology) and provide context for the agent.
+ `promptSessionAttributes` – Contains [prompt session attributes](agents-session-state.md) and their values. These attributes are stored over a [turn](advanced-prompts.md#advanced-prompts-terminology) and provide context for the agent.

## Lambda response event to Amazon Bedrock
<a name="agents-lambda-response"></a>

Amazon Bedrock expects a response from your Lambda function that matches the following format. The response consists of parameters returned from the API operation. The agent can use the response from the Lambda function for further orchestration or to help it return a response to the customer.

**Note**  
The maximum payload response size matches the maximum size of a synchronous response from the Lambda function. For more information, see the *invocation payload* resource [quota](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html#function-configuration-deployment-and-execution) in the AWS Lambda Developer Guide.

The input event format depends on whether you defined the action group with an API schema or with function details:
+ If you defined the action group with an API schema, the response format is as follows:

  ```
  {
      "messageVersion": "1.0",
      "response": {
          "actionGroup": "string",
          "apiPath": "string",
          "httpMethod": "string",
          "httpStatusCode": number,
          "responseBody": {
              "<contentType>": {
                  "body": "JSON-formatted string" 
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
          ...
      },
      "promptSessionAttributes": {
          "string": "string",
          ...
      },
      "knowledgeBasesConfiguration": [
          {
              "knowledgeBaseId": "string",
              "retrievalConfiguration": {
                  "vectorSearchConfiguration": {
                      "numberOfResults": int,
                      "overrideSearchType": "HYBRID | SEMANTIC",
                      "filter": [RetrievalFilter](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html) object
                  }
              }
          },
          ...
      ]
  }
  ```
+ If you defined the action group with function details, the response format is as follows:

  ```
  {
      "messageVersion": "1.0",
      "response": {
          "actionGroup": "string",
          "function": "string",
          "functionResponse": {
              "responseState": "FAILURE | REPROMPT",
              "responseBody": {
                  "<functionContentType>": { 
                      "body": "JSON-formatted string"
                  }
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      },
      "knowledgeBasesConfiguration": [
          {
              "knowledgeBaseId": "string",
              "retrievalConfiguration": {
                  "vectorSearchConfiguration": {
                      "numberOfResults": int,
                      "filter": {
                          [RetrievalFilter](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html) object
                      }
                  }
              }
          },
          ...
      ]
  }
  ```

The following list describes the response fields:
+ `messageVersion` – The version of the message that identifies the format of the event data going into the Lambda function and the expected format of the response from a Lambda function. Amazon Bedrock only supports version 1.0.
+ `response` – Contains the following information about the API response.
  + `actionGroup` – The name of the action group.
  + If you defined the action group with an API schema, the following fields can be in the response:
    + `apiPath` – The path to the API operation, as defined in the OpenAPI schema.
    + `httpMethod` – The method of the API operation, as defined in the OpenAPI schema.
    + `httpStatusCode` – The HTTP status code returned from the API operation.
    + `responseBody` – Contains the response body, as defined in the OpenAPI schema.
  + If you defined the action group with function details, the following fields can be in the response:
    + `responseState` (Optional) – Set to one of the following states to define the agent's behavior after processing the action:
      + FAILURE – The agent throws a `DependencyFailedException` for the current session. Applies when the function execution fails because of a dependency failure.
      + REPROMPT – The agent passes a response string to the model to reprompt it. Applies when the function execution fails because of invalid input.
    + `responseBody` – Contains an object that defines the response from execution of the function. The key is the content type (currently only `TEXT` is supported) and the value is an object containing the `body` of the response.
+ (Optional) `sessionAttributes` – Contains session attributes and their values. For more information, see [Session and prompt session attributes](agents-session-state.md#session-state-attributes).
+ (Optional) `promptSessionAttributes` – Contains prompt attributes and their values. For more information, see [Session and prompt session attributes](agents-session-state.md#session-state-attributes).
+ (Optional) `knowledgeBasesConfiguration` – Contains a list of query configurations for knowledge bases attached to the agent. For more information, see [Knowledge base retrieval configurations](agents-session-state.md#session-state-kb).

## Action group Lambda function example
<a name="agents-lambda-example"></a>

The following is a minimal example of how the Lambda function can be defined in Python. Select the tab corresponding to whether you defined the action group with an OpenAPI schema or with function details:

------
#### [ OpenAPI schema ]

```
def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    api_path = event['apiPath']
    # get parameters
    get_parameters = event.get('parameters', [])
    # post parameters
    post_parameters = event['requestBody']['content']['application/json']['properties']

    response_body = {
        'application/json': {
            'body': "sample response"
        }
    }
    
    action_response = {
        'actionGroup': event['actionGroup'],
        'apiPath': event['apiPath'],
        'httpMethod': event['httpMethod'],
        'httpStatusCode': 200,
        'responseBody': response_body
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    api_response = {
        'messageVersion': '1.0', 
        'response': action_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return api_response
```

------
#### [ Function details ]

```
def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    response_body = {
        'TEXT': {
            'body': "sample response"
        }
    }
    
    function_response = {
        'actionGroup': event['actionGroup'],
        'function': event['function'],
        'functionResponse': {
            'responseBody': response_body
        }
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    action_response = {
        'messageVersion': '1.0', 
        'response': function_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return action_response
```

------

# Return control to the agent developer by sending elicited information in an InvokeAgent response
<a name="agents-returncontrol"></a>

Rather than sending the information that your agent has elicited from the user to a Lambda function for fulfillment, you can instead choose to return control to the agent developer by sending the information in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response. You can configure return of control to the agent developer when creating or updating an action group. Through the API, you specify `RETURN_CONTROL` as the `customControl` value in the `actionGroupExecutor` object in a [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) or [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html) request. For more information, see [Add an action group to your agent in Amazon Bedrock](agents-action-add.md).

If you configure return of control for an action group, and if the agent determines that it should call an action in this action group, the API or function details elicited from the user will be returned in the `invocationInputs` field in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response, alongside a unique `invocationId`. You can then do the following:
+ Set up your application to invoke the API or function that you defined, provided the information returned in the `invocationInputs`.
+ Send the results from your application's invocation in another [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) request, in the `sessionState` field, to provide context to the agent. You must use the same `invocationId` and `actionGroup` that were returned in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response. This information can be used as context for further orchestration, sent to post-processing for the agent to format a response, or used directly in the agent's response to the user.
**Note**  
If you include `returnControlInvocationResults` in the `sessionState` field, the `inputText` field will be ignored.

To learn how to configure return of control to the agent developer while creating the action group, see [Add an action group to your agent in Amazon Bedrock](agents-action-add.md).

## Example for returning control to the agent developer
<a name="agents-returncontrol-ex"></a>

For example, you might have the following action groups:
+ A `PlanTrip` action group with a `suggestActivities` action that helps your users find activities to do during a trip. The `description` for this action says `This action suggests activities based on retrieved weather information`.
+ A `WeatherAPIs` action group with a `getWeather` action that helps your user get the weather for a specific location. The action's required parameters are `location` and `date`. The action group is configured to return control to the agent developer.

The following is a hypothetical sequence that might occur:

1. The user prompts your agent with the following query: **What should I do today?** This query is sent in the `inputText` field of an [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) request.

1. Your agent recognizes that the `suggestActivities` action should be invoked, but given the description, predicts that it should first invoke the `getWeather` action as context for helping to fulfill the `suggestActivities` action.

1. The agent knows that the current `date` is `2024-09-15`, but needs the `location` of the user as a required parameter to get the weather. It reprompts the user with the question "Where are you located?"

1. The user responds **Seattle**.

1. The agent returns the parameters for `getWeather` in the following [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response (select a tab to see examples for an action group defined with that method):

------
#### [ Function details ]

   ```
   HTTP/1.1 200
   x-amzn-bedrock-agent-content-type: application/json
   x-amz-bedrock-agent-session-id: session0
   Content-type: application/json
    
   {
       "returnControl": {
           "invocationInputs": [{
               "functionInvocationInput": {
                   "actionGroup": "WeatherAPIs",
                   "function": "getWeather",
                   "parameters": [
                       {
                           "name": "location",
                           "type": "string",
                           "value": "seattle"
                       },
                       {
                           "name": "date",
                           "type": "string",
                           "value": "2024-09-15"
                       }
                   ]
               }
           }],
           "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172"
       }
   }
   ```

------
#### [ OpenAPI schema ]

   ```
   HTTP/1.1 200
   x-amzn-bedrock-agent-content-type: application/json
   x-amz-bedrock-agent-session-id: session0
   Content-type: application/json
   
   {
       "invocationInputs": [{
           "apiInvocationInput": {
               "actionGroup": "WeatherAPIs",
               "apiPath": "/get-weather",
               "httpMethod": "get",
               "parameters": [
                   {
                       "name": "location",
                       "type": "string",
                       "value": "seattle"
                   },
                   {
                       "name": "date",
                       "type": "string",
                       "value": "2024-09-15"
                   }
               ]
           }
       }],
       "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad"
   }
   ```

------

1. Your application is configured to use these parameters to get the weather for `seattle` for the date `2024-09-15`. The weather is determined to be rainy.

1. You send these results in the `sessionState` field of another [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) request, using the same `invocationId`, `actionGroup`, and `function` as the previous response. Select a tab to see examples for an action group defined with that method:

------
#### [ Function details ]

   ```
   POST https://bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
               
   {
       "enableTrace": true,
       "sessionState": {
           "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172",
           "returnControlInvocationResults": [{
               "functionResult": {
                   "actionGroup": "WeatherAPIs",
                   "function": "getWeather",
                   "responseBody": {
                       "TEXT": {
                           "body": "It's rainy in Seattle today."
                       }
                   }
               }
           }]
       }
   }
   ```

------
#### [ OpenAPI schema ]

   ```
   POST https: //bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
    
   {
       "enableTrace": true,
       "sessionState": {
           "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad",
           "returnControlInvocationResults": [{
               "apiResult": {
                   "actionGroup": "WeatherAPIs",
                   "httpMethod": "get",
                   "apiPath": "/get-weather",
                   "responseBody": {
                       "application/json": {
                           "body": "It's rainy in Seattle today."
                       }
                   }
               }
           }]
       }
   }
   ```

------

1. The agent predicts that it should call the `suggestActivities` action. It uses the context that it's rainy that day and suggests indoor, rather than outdoor, activities for the user in the response.

### Example for returning control to the collaborator agent
<a name="collaborator-agent-returncontrol-ex"></a>

If you are using [multi-agent collaboration](agents-multi-agent-collaboration.md) and if an agent collaborator chooses to return control by sending the information in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response, the information is returned in supervisor agent response with additional identifiers `agentId` and `collaboratorName`. Select a tab to see examples for an action group defined with that method: 

------
#### [ Function details ]

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: application/json
x-amz-bedrock-agent-session-id: session0
Content-type: application/json
 
{
    "returnControl": {
        "invocationInputs": [{
            "functionInvocationInput": {
                "agentId": "AGENTID",
                "collaboratorName": "WeatherAgent"
                "actionGroup": "WeatherAPIs",
                "function": "getWeather",
                "parameters": [
                    {
                        "name": "location",
                        "type": "string",
                        "value": "seattle"
                    },
                    {
                        "name": "date",
                        "type": "string",
                        "value": "2024-09-15"
                    }
                ]
            }
        }],
        "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172"
    }
}
```

------
#### [ OpenAPI Schema ]

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: application/json
x-amz-bedrock-agent-session-id: session0
Content-type: application/json

{
    "invocationInputs": [{
        "apiInvocationInput": {
            "actionGroup": "WeatherAPIs",
            "agentId": "AGENTID",
            "collaboratorName": "WeatherAgent"
            "apiPath": "/get-weather",
            "httpMethod": "get",
            "parameters": [
                {
                    "name": "location",
                    "type": "string",
                    "value": "seattle"
                },
                {
                    "name": "date",
                    "type": "string",
                    "value": "2024-09-15"
                }
            ]
        }
    }],
    "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad"
}
```

------

Invoker of supervisor agent sends the results in the `sessionState` field back to supervisor agent with the corresponding `agentId` for it to be propagated to the right agent collaborator.

------
#### [ Function details ]

```
POST https://bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
            
{
    "enableTrace": true,
    "sessionState": {
        "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172",
        "returnControlInvocationResults": [{
            "functionResult": {
                "agentId": "AGENTID",
                "actionGroup": "WeatherAPIs",
                "function": "getWeather",
                "responseBody": {
                    "TEXT": {
                        "body": "It's rainy in Seattle today."
                    }
                }
            }
        }]
    }
}
```

------
#### [ OpenAPI Schema ]

```
POST https: //bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
 
{
    "enableTrace": true,
    "sessionState": {
        "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad",
        "returnControlInvocationResults": [{
            "apiResult": {
                "agentId": "AGENTID",
                "actionGroup": "WeatherAPIs",
                "httpMethod": "get",
                "apiPath": "/get-weather",
                "responseBody": {
                    "application/json": {
                        "body": "It's rainy in Seattle today."
                    }
                }
            }
        }]
    }
}
```

------

# Get user confirmation before invoking action group function
<a name="agents-userconfirmation"></a>

You can safeguard your application from malicious prompt injections by requesting confirmation from your application users before invoking the action group function. When an end user interacts with your application, Amazon Bedrock Agent figures out the API or knowledge bases to invoke to automate the task for the user. The information from the API or knowledge bases might contain potentially damaging data. Between each iteration if the response contains any instruction, the agent will comply. If the response includes instructions for the model to invoke unintended actions, the agent will go ahead and comply with the instruction. To ensure that certain actions are implemented only after explicit user consent, we recommend that you request confirmation from the end user before invoking the function. 

When you configure your action group, you can choose to enable user confirmation for specific actions. If user confirmation is enabled for an action, agent responds with a confirmation question asking end user to either confirm or deny the action. You can enable user confirmation in the console, using the CLI, or using the SDK. 

To enable user confirmation for an action, see [Add an action group to your agent in Amazon Bedrock](agents-action-add.md).

## How user confirmation works
<a name="user-confirmation-works"></a>

The user confirmation is configured for an action in the action group by the agent developer. If the agent decides that it should call that action, the API or the function details elicited from the user and the user confirmation configured by the agent developer will be returned in the `invocationInputs` field in the [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) response, alongside `invocationType`, and an unique `invocationId`.

The agent invokes the API or the function that was provided in the `invocationInputs`. If the user confirmation is enabled for the function or the API, the user is presented with an option to **CONFIRM** or **DENY** the action mentioned in the response.

The results from the agent’s invocation of the function or API is sent in another [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) request, in the `sessionState` field, to provide context to the agent. The request parameter for `InvokeAgent` uses `returnControlInvocationResults`, which is a list of map to `apiResult` or `functionResult` objects. The `apiResult` and `functionResult` objects have an additional field of `confirmationState`. This field has the user confirmation response. 

If the user response is **CONFIRM**, the function or the API in the response is implemented. 

If the user response is **DENY**, the function or the API in the response is not implemented. 

## Examples of the InvokeAgent response and request
<a name="agents-userconfirmation-ex"></a>

**Response**

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: contentType
x-amz-bedrock-agent-session-id: sessionId
Content-type: application/json

{
   "chunk": { 
      ...
   },
   ...
   "returnControl": { 
      "invocationId": "string",
      "invocationInputs": [ 
         { ... }
      ]
   },
   "trace": { 
      "agentAliasId": "string",
      "agentId": "string",
      "agentVersion": "string",
      "sessionId": "string",
      "trace": { ... }
   },
}
```

**Request**

```
POST /agents/agentId/agentAliases/agentAliasId/sessions/sessionId/text HTTP/1.1
Content-type: application/json

{
   "enableTrace": boolean,
   "endSession": boolean,
   "inputText": "string",
   "sessionState": { 
      "invocationId": "string",
      "promptSessionAttributes": { 
         "string" : "string" 
      },
      "returnControlInvocationResults": [ 
         { ... }
      ],
      "sessionAttributes": { 
         "string" : "string" 
      }
   }
}
```