Track agent's step-by-step reasoning process using trace
Each response from an Amazon Bedrock agent is accompanied by a trace that details the steps being orchestrated by the agent. The trace helps you follow the agent's reasoning process that leads it to the response it gives at that point in the conversation.
Use the trace to track the agent's path from the user input to the response it returns. The trace provides information about the inputs to the action groups that the agent invokes and the knowledge bases that it queries to respond to the user. In addition, the trace provides information about the outputs that the action groups and knowledge bases return. You can view the reasoning that the agent uses to determine the action that it takes or the query that it makes to a knowledge base. If a step in the trace fails, the trace returns a reason for the failure. Use the detailed information in the trace to troubleshoot your agent. You can identify steps at which the agent has trouble or at which it yields unexpected behavior. Then, you can use this information to consider ways in which you can improve the agent's behavior.
Structure of the trace
The trace is shown as a JSON object in both the console and the API. Each Step in the console or Trace in the API can be one of the following traces:
-
PreProcessingTrace – Traces the input and output of the pre-processing step, in which the agent contextualizes and categorizes user input and determines if it is valid.
-
OrchestrationTrace – Traces the input and output of the orchestration step, in which the agent interprets the input, invokes action groups, and queries knowledge bases. Then the agent returns output to either continue orchestration or to respond to the user.
-
PostProcessingTrace – Traces the input and output of the post-processing step, in which the agent handles the final output of the orchestration and determines how to return the response to the user.
-
FailureTrace – Traces the reason that a step failed.
-
GuardrailTrace – Traces the actions of the Guardrail.
Each of the traces (except FailureTrace
) contains a ModelInvocationInput object. The ModelInvocationInput object contains configurations set in the prompt template for the step, alongside the prompt provided to the agent at this step. For more information about how to modify prompt templates, see Enhance agent's accuracy using advanced prompt templates in Amazon Bedrock. The structure of the ModelInvocationInput
object is as follows:
{ "traceId": "string", "text": "string", "type": "PRE_PROCESSING | ORCHESTRATION | KNOWLEDGE_BASE_RESPONSE_GENERATION | POST_PROCESSING", "inferenceConfiguration": { "maximumLength": number, "stopSequences": ["string"], "temperature": float, "topK": float, "topP": float }, "promptCreationMode": "DEFAULT | OVERRIDDEN", "parserMode": "DEFAULT | OVERRIDDEN", "overrideLambda": "string" }
The following list describes the fields of the ModelInvocationInput object:
-
traceId
– The unique identifier of the trace. -
text
– The text from the prompt provided to the agent at this step. -
type
– The current step in the agent's process. -
inferenceConfiguration
– Inference parameters that influence response generation. For more information, see Influence response generation with inference parameters. -
promptCreationMode
– Whether the agent's default base prompt template was overridden for this step. For more information, see Enhance agent's accuracy using advanced prompt templates in Amazon Bedrock. -
parserMode
– Whether the agent's default response parser was overridden for this step. For more information, see Enhance agent's accuracy using advanced prompt templates in Amazon Bedrock. -
overrideLambda
– The Amazon Resource Name (ARN) of the parser Lambda function used to parse the response, if the default parser was overridden. For more information, see Enhance agent's accuracy using advanced prompt templates in Amazon Bedrock.
For more information about each trace type, see the following sections:
{ "modelInvocationInput": { // see above for details } "modelInvocationOutput": { "metadata": { "usage": { "inputToken":: int, "outputToken":: int }, "rawResponse": { "content": "string" } "parsedResponse": { "isValid": boolean, "rationale": "string" }, "traceId": "string" } }
The PreProcessingTrace consists of a ModelInvocationInput object and a PreProcessingModelInvocationOutput object. The PreProcessingModelInvocationOutput contains the following fields.
-
metadata
– Contains the following information about the foundation model output.usage
– Contains the following information of the usage of the foundation model.-
inputTokens
– Contains the information about the input tokens from the foundation model usage. -
outputTokens
– Contains the information about the output tokens from the foundation model usage.
-
-
rawResponse
– Contains the raw output from the foundation model.-
content
– The foundation model's raw output content.
-
-
parsedResponse
– Contains the following details about the parsed user prompt.-
isValid
– Specifies whether the user prompt is valid. -
rationale
– Specifies the agent's reasoning for the next steps to take.
-
-
traceId
– The unique identifier of the trace.
The OrchestrationTrace consists of the ModelInvocationInput object, OrchestrationModelInvocationOutput object, and any combination of the Rationale, InvocationInput, and Observation objects. The OrchestrationModelInvocationOutput contains the following fields. For more information about Rationale, InvocationInput, and Observation objects, select from the following tabs.
{ "modelInvocationInput": { // see above for details }, "modelInvocationOutput": { "metadata": { "usage": { "inputToken":: int, "outputToken":: int }, "rawResponse": { "content": "string" }, "rationale": { ... }, "invocationInput": { ... }, "observation": { ... } }
{ "modelInvocationInput": { // see above for details } "modelInvocationOutput": { "rawResponse": { "content": "string" }, "metadata": { "usage": { "inputToken": int, "outputToken": int } }, "parsedResponse": { "text": "string" }, "traceId": "string" } }
The PostProcessingTrace consists of a ModelInvocationInput object and a PostProcessingModelInvocationOutput object. The PostProcessingModelInvocationOutput contains the following fields:
-
rawResponse
– Contains the raw output from the foundation model.-
content
– The foundation model's raw output content.
-
-
metadata
– Contains the following information about the foundation model output.usage
– Contains the following information of the usage of the foundation model.-
inputTokens
– Contains the information about the input tokens from the foundation model usage. -
outputTokens
– Contains the information about the output tokens from the foundation model usage.
-
-
parsedResponse
– Contains thetext
to return to the user after the text is processed by the parser function. -
traceId
– The unique identifier of the trace.
{ "failureReason": "string", "traceId": "string" }
The following list describes the fields of the FailureTrace object:
-
failureReason
– The reason that the step failed. -
traceId
– The unique identifier of the trace.
{ "action": "GUARDRAIL_INTERVENED" | "NONE", "inputAssessments": [GuardrailAssessment], "outputAssessments": [GuardrailAssessment] }
The following list describes the fields of the GuardrailAssessment object:
-
action
– indicates whether guardrails intervened or not on the input data. Options areGUARDRAIL_INTERVENED
orNONE
. -
inputAssessments
– The details of the Guardrail assessment on the user input. -
outputAssessments
– The details of the Guardrail assessment on the response.
For more details on the GuardrailAssessment
object and testing a Guardrail, see Test a guardrail.
GuardrailAssessment example:
{ "topicPolicy": { "topics": [{ "name": "string", "type": "string", "action": "string" }] }, "contentPolicy": { "filters": [{ "type": "string", "confidence": "string", "action": "string" }] }, "wordPolicy": { "customWords": [{ "match": "string", "action": "string" }], "managedWordLists": [{ "match": "string", "type": "string", "action": "string" }] }, "sensitiveInformationPolicy": { "piiEntities": [{ "type": "string", "match": "string", "action": "string" }], "regexes": [{ "name": "string", "regex": "string", "match": "string", "action": "string" }] } }