Troubleshooting with CloudWatch Logs Insights and AWS X-Ray
This lesson covers CloudWatch Logs Insights, AWS X-Ray and AI-assisted error analysis, which make generative AI failures hidden in prompts, responses and service call chains searchable and traceable.
Learning outcomes
After completing this lesson, you will be able to:
- Analyze prompts and responses with Logs Insights queries.
- Trace FM calls end to end with X-Ray.
- Use AI-assisted error analysis.
Analysing prompts and responses with Logs Insights
With Bedrock model invocation logging delivering to CloudWatch Logs, each record contains the model ID, the caller's identity, the request ID, input and output bodies and token counts. CloudWatch Logs Insights queries these records interactively: finding the requests with the most tokens, counting errors by model, searching responses for a phrase, or computing latency percentiles from application logs. Application logs that record the request ID, prompt version, retrieved document IDs and guardrail actions in structured JSON make correlation straightforward.
fields @timestamp, modelId, identity.arn, input.inputTokenCount, output.outputTokenCount
| filter input.inputTokenCount > 20000
| sort input.inputTokenCount desc
| limit 20stats count(*) as calls, sum(input.inputTokenCount) as inTok, sum(output.outputTokenCount) as outTok by modelId, bin(1h)Tracing FM calls with X-Ray
AWS X-Ray (and CloudWatch tracing with OpenTelemetry through the AWS Distro for OpenTelemetry) records traces of requests as they pass through API Gateway, Lambda, Step Functions and SDK calls to Bedrock, knowledge bases, DynamoDB and other services. Each downstream call appears as a subsegment with duration and errors, so a slow request shows whether time went to retrieval, the model, a tool or a retry. Annotations such as prompt version, model ID and route are indexed for filtering traces, and the service map shows dependencies and error rates.
Agent frameworks and AgentCore Observability emit OpenTelemetry spans for each agent step, tool call and model invocation, which appear in the CloudWatch generative AI observability views alongside X-Ray traces.
The following diagram shows the trace of one slow request to a generative AI application.

Logs Insights or X-Ray?
AI-assisted error analysis
Amazon Q Developer helps recognise generative AI error patterns: pasting an error, log excerpt or stack trace into chat yields an explanation and a suggested fix, for example identifying that a ValidationException comes from consecutive assistant messages, that an AccessDeniedException after enabling cross-Region inference is caused by missing permissions in destination Regions, or that a truncated JSON response is due to a low maxTokens. In the console, it can investigate errors in the context of the account's resources. The following table maps questions to tools.
| Question | Tool |
|---|---|
| Which requests used the most tokens or failed? | Logs Insights on invocation logs |
| Where did a slow request spend its time? | X-Ray or OpenTelemetry traces |
| Which prompt version is involved in errors? | X-Ray annotations; structured app logs |
| What does this GenAI error mean and how to fix it? | Amazon Q Developer |
| How did an agent reason and which tools it called? | Agent traces; AgentCore Observability |
Intermittent slowness
Some chat requests take 20 seconds instead of the usual 4. Model latency metrics look normal.
X-Ray traces of slow requests show a knowledge base Retrieve subsegment taking 15 seconds with retries, pointing to the vector store rather than the model. A Logs Insights query over application logs confirms that slow requests all use a metadata filter on a non-indexed field, which the team then fixes.
Which attributes should be added to traces as annotations?
Show answer
The prompt version and the model ID, so traces can be filtered by them.
Exam Tips
Remember for the exam
- Query invocation logs with Logs Insights for tokens, errors and content.
- X-Ray traces show which hop (retrieval, model, tool) is slow or failing.
- Annotate traces with prompt version and model ID.
- Amazon Q Developer explains GenAI error patterns and suggests fixes.