InvokeAgent - Amazon Bedrock

InvokeAgent

Note

The AWS CLI doesn't support streaming operations in Amazon Bedrock, including InvokeAgent.

Sends a prompt for the agent to process and respond to. Note the following fields for the request:

  • To continue the same conversation with an agent, use the same sessionId value in the request.

  • To activate trace enablement, turn enableTrace to true. Trace enablement helps you follow the agent's reasoning process that led it to the information it processed, the actions it took, and the final result it yielded. For more information, see Trace enablement.

  • End a conversation by setting endSession to true.

  • In the sessionState object, you can include attributes for the session or prompt or, if you configured an action group to return control, results from invocation of the action group.

The response is returned in the bytes field of the chunk object.

  • The attribution object contains citations for parts of the response.

  • If you set enableTrace to true in the request, you can trace the agent's steps and reasoning process that led it to the response.

  • If the action predicted was configured to return control, the response returns parameters for the action, elicited from the user, in the returnControl field.

  • Errors are also surfaced in the response.

Request Syntax

POST /agents/agentId/agentAliases/agentAliasId/sessions/sessionId/text HTTP/1.1 Content-type: application/json { "enableTrace": boolean, "endSession": boolean, "inputText": "string", "memoryId": "string", "sessionState": { "files": [ { "name": "string", "source": { "byteContent": { "data": blob, "mediaType": "string" }, "s3Location": { "uri": "string" }, "sourceType": "string" }, "useCase": "string" } ], "invocationId": "string", "knowledgeBaseConfigurations": [ { "knowledgeBaseId": "string", "retrievalConfiguration": { "vectorSearchConfiguration": { "filter": { ... }, "numberOfResults": number, "overrideSearchType": "string" } } } ], "promptSessionAttributes": { "string" : "string" }, "returnControlInvocationResults": [ { ... } ], "sessionAttributes": { "string" : "string" } } }

URI Request Parameters

The request uses the following URI parameters.

agentAliasId

The alias of the agent to use.

Length Constraints: Minimum length of 0. Maximum length of 10.

Pattern: ^[0-9a-zA-Z]+$

Required: Yes

agentId

The unique identifier of the agent to use.

Length Constraints: Minimum length of 0. Maximum length of 10.

Pattern: ^[0-9a-zA-Z]+$

Required: Yes

sessionId

The unique identifier of the session. Use the same value across requests to continue the same conversation.

Length Constraints: Minimum length of 2. Maximum length of 100.

Pattern: ^[0-9a-zA-Z._:-]+$

Required: Yes

Request Body

The request accepts the following data in JSON format.

enableTrace

Specifies whether to turn on the trace or not to track the agent's reasoning process. For more information, see Trace enablement.

Type: Boolean

Required: No

endSession

Specifies whether to end the session with the agent or not.

Type: Boolean

Required: No

inputText

The prompt text to send the agent.

Note

If you include returnControlInvocationResults in the sessionState field, the inputText field will be ignored.

Type: String

Length Constraints: Minimum length of 0. Maximum length of 25000000.

Required: No

memoryId

The unique identifier of the agent memory.

Type: String

Length Constraints: Minimum length of 2. Maximum length of 100.

Pattern: ^[0-9a-zA-Z._:-]+$

Required: No

sessionState

Contains parameters that specify various attributes of the session. For more information, see Control session context.

Note

If you include returnControlInvocationResults in the sessionState field, the inputText field will be ignored.

Type: SessionState object

Required: No

Response Syntax

HTTP/1.1 200 x-amzn-bedrock-agent-content-type: contentType x-amz-bedrock-agent-memory-id: memoryId x-amz-bedrock-agent-session-id: sessionId Content-type: application/json { "accessDeniedException": { }, "badGatewayException": { }, "chunk": { "attribution": { "citations": [ { "generatedResponsePart": { "textResponsePart": { "span": { "end": number, "start": number }, "text": "string" } }, "retrievedReferences": [ { "content": { "text": "string" }, "location": { "confluenceLocation": { "url": "string" }, "s3Location": { "uri": "string" }, "salesforceLocation": { "url": "string" }, "sharePointLocation": { "url": "string" }, "type": "string", "webLocation": { "url": "string" } }, "metadata": { "string" : JSON value } } ] } ] }, "bytes": blob }, "conflictException": { }, "dependencyFailedException": { }, "files": { "files": [ { "bytes": blob, "name": "string", "type": "string" } ] }, "internalServerException": { }, "resourceNotFoundException": { }, "returnControl": { "invocationId": "string", "invocationInputs": [ { ... } ] }, "serviceQuotaExceededException": { }, "throttlingException": { }, "trace": { "agentAliasId": "string", "agentId": "string", "agentVersion": "string", "sessionId": "string", "trace": { ... } }, "validationException": { } }

Response Elements

If the action is successful, the service sends back an HTTP 200 response.

The response returns the following HTTP headers.

contentType

The MIME type of the input data in the request. The default value is application/json.

memoryId

The unique identifier of the agent memory.

Length Constraints: Minimum length of 2. Maximum length of 100.

Pattern: ^[0-9a-zA-Z._:-]+$

sessionId

The unique identifier of the session with the agent.

Length Constraints: Minimum length of 2. Maximum length of 100.

Pattern: ^[0-9a-zA-Z._:-]+$

The following data is returned in JSON format by the service.

accessDeniedException

The request is denied because of missing access permissions. Check your permissions and retry your request.

Type: Exception

HTTP Status Code: 403
badGatewayException

There was an issue with a dependency due to a server issue. Retry your request.

Type: Exception

HTTP Status Code: 502
chunk

Contains a part of an agent response and citations for it.

Type: PayloadPart object

conflictException

There was a conflict performing an operation. Resolve the conflict and retry your request.

Type: Exception

HTTP Status Code: 409
dependencyFailedException

There was an issue with a dependency. Check the resource configurations and retry the request.

Type: Exception

HTTP Status Code: 424
files

Contains intermediate response for code interpreter if any files have been generated.

Type: FilePart object

internalServerException

An internal server error occurred. Retry your request.

Type: Exception

HTTP Status Code: 500
resourceNotFoundException

The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.

Type: Exception

HTTP Status Code: 404
returnControl

Contains the parameters and information that the agent elicited from the customer to carry out an action. This information is returned to the system and can be used in your own setup for fulfilling the action.

Type: ReturnControlPayload object

serviceQuotaExceededException

The number of requests exceeds the service quota. Resubmit your request later.

Type: Exception

HTTP Status Code: 400
throttlingException

The number of requests exceeds the limit. Resubmit your request later.

Type: Exception

HTTP Status Code: 429
trace

Contains information about the agent and session, alongside the agent's reasoning process and results from calling actions and querying knowledge bases and metadata about the trace. You can use the trace to understand how the agent arrived at the response it provided the customer. For more information, see Trace events.

Type: TracePart object

validationException

Input validation failed. Check your request parameters and retry the request.

Type: Exception

HTTP Status Code: 400

Errors

For information about the errors that are common to all actions, see Common Errors.

AccessDeniedException

The request is denied because of missing access permissions. Check your permissions and retry your request.

HTTP Status Code: 403

BadGatewayException

There was an issue with a dependency due to a server issue. Retry your request.

HTTP Status Code: 502

ConflictException

There was a conflict performing an operation. Resolve the conflict and retry your request.

HTTP Status Code: 409

DependencyFailedException

There was an issue with a dependency. Check the resource configurations and retry the request.

HTTP Status Code: 424

InternalServerException

An internal server error occurred. Retry your request.

HTTP Status Code: 500

ResourceNotFoundException

The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.

HTTP Status Code: 404

ServiceQuotaExceededException

The number of requests exceeds the service quota. Resubmit your request later.

HTTP Status Code: 400

ThrottlingException

The number of requests exceeds the limit. Resubmit your request later.

HTTP Status Code: 429

ValidationException

Input validation failed. Check your request parameters and retry the request.

HTTP Status Code: 400

Examples

Example simple request

The following example inquires the agent to get the weather for Seattle.

POST https://bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text { "inputText": "give me the weather for seattle", "enableTrace": true }

Example response (action group defined with OpenAPI schema, control returned)

The following example shows a response from an agent that has invoked an action group that was configured as follows:

  • Defined with an OpenAPI schema

  • Configured to return control to the agent developer

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" }

Example request using results from returned control (action group defined with OpenAPI schema)

The following example shows a request in which the results returned in the InvokeAgent response from an agent are passed to the sessionState of a new request. The results were returned from an agent that has invoked an action group that was configured as follows:

  • Defined with an OpenAPI schema

  • Configured to return control to the agent developer

The invocationId must match the invocationId that was returned in the response.

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." } } } }] } }

Example response (action group defined with function details, control returned)

The following example shows a response from an agent that has invoked an action group that was configured as follows:

  • Defined with function details

  • Configured to return control to the agent developer

HTTP/1.1 200 x-amzn-bedrock-agent-content-type: application/json x-amz-bedrock-agent-session-id: session0 Content-type: application/json { "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" }

Example request using results from returned control (action group defined with function details)

The following example shows a request in which the results returned in the InvokeAgent response from an agent are passed to the sessionState of a new request. The results were returned from an agent that has invoked an action group that was configured as follows:

  • Defined with function details

  • Configured to return control to the agent developer

The invocationId must match the invocationId that was returned in the response.

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." } } } }] } }

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: