Logs Insights uses an SQL-like syntax but is not full SQL. To write effective queries, know your log schema (the names and types of fields your application and Bedrock emit).
Common Logs Insights queries for Bedrock model invocations
Below are practical queries and patterns to analyze Bedrock invocation logs (for example, the log group that receives Bedrock model invocation events).- Basic extraction of timestamp, event type, and model ID (sorted by timestamp desc, limited to 10,000 results):
- Filter for failed Bedrock requests (show timestamp, event, modelId, durationMs, requestId; most recent 20):
- Find slow successful responses (duration greater than 3000 ms):
- Average latency by model (helpful to compare model performance):
- Approximate token / context window usage (prompt length + output length), sorted descending:
- Which models are slowest on average?
- Which requests are failing, and when?
- Are any requests approaching a model’s context window limit?
Quick reference: event names and their purpose
Application-level logging (Lambda)
Bedrock model invocation logs capture model-level input/output, but your application should emit structured logs that provide context around those calls—what the app was doing, which user or API request triggered the model call, and correlation identifiers. For AWS Lambda, anything written to stdout is automatically forwarded to the function’s CloudWatch Logs group. Using a structured logger (JSON) makes logs easier to parse and correlate with model invocation logs.
- Calls Bedrock via the runtime client,
- Emits structured JSON events to stdout so they appear in the function’s CloudWatch Logs group, and
- Includes the Lambda
context.aws_request_idso you can correlate application logs with Bedrock model logs.
Do not log sensitive data (PII, credentials, full user messages, or API keys). Use redaction and minimize sensitive fields in logs. Structured logs still carry risk if they include raw user content.
Notes and best practices
- Use structured logs (JSON) for application events—this makes Logs Insights filters simple and reliable.
- Include a correlation identifier such as
context.aws_request_idso you can map a Lambda invocation’s application logs to Bedrock model invocation logs. - Use distinct, consistent event names such as
bedrock_request_started,bedrock_request_succeeded, andbedrock_request_failed. The example Log Insights queries above filter on those event names. - Emit additional contextual fields (user id, API path, feature flag state, deployment stage) when relevant—this metadata speeds troubleshooting and attribution.
- For most Lambda use cases, writing to stdout is sufficient; CloudWatch Logs will automatically capture those entries. If you need programmatic control, you can instantiate a CloudWatch Logs client with boto3 to create log groups, push events, or manage retention.
- When parsing model responses, be prepared for different response shapes depending on the model—implement safe fallbacks and defensive parsing as shown in the example.