Skip to content

How to Handle Large DynamoDB Responses & Improve Query Efficiency in an LLM-Powered Chatbot (AWS Bedrock + Lambda)

0

I’m building an LLM-powered chatbot that answers operational / IoT-style questions. When a user asks something like:

  • “How many assets of each type are in each status right now?”
  • “Which assets have flavor alerts now?”
  • “Which assets had active alerts on Sunday night?”

The flow is:

  1. User question → API Gateway
  2. API Gateway → Lambda
  3. Lambda → Uses AWS Bedrock (LLM) to translate the user’s natural-language request into a DynamoDB Query or Scan.

However, I’m running into two major limitations:

  1. Large DynamoDB result sets exceed LLM input limits Some queries return thousands of items. Passing this full dataset back into the LLM for summarization exceeds token limits and becomes expensive and slow.

  2. DynamoDB scans/queries can be slow The DynamoDB table is large, and depending on the user query, a Scan may be required. This adds latency to the entire conversational flow.

My Question

What are recommended architectural patterns or design approaches to solve these constraints?

Specifically:

  1. How can I avoid passing huge result sets into the LLM?
  2. How can I speed up or optimize DynamoDB access when the query space is large or unbounded?
  3. Are there better approaches overall for combining LLMs with structured data retrieval for analytic-type queries?

I’m open to rethinking the architecture if there’s a more scalable way to do this.

1 Answer
0
Accepted Answer

Handling Large DynamoDB Responses in LLM-Powered Chatbots

Optimizing DynamoDB Result Sets for LLMs

1. Implement Pagination and Summarization

When dealing with large result sets that exceed LLM token limits, implement pagination to process data in manageable chunks:

  • Set a smaller page size using the Limit parameter in your Query/Scan operations
  • Use ExclusiveStartKey and LastEvaluatedKey to paginate through results
  • Process each page individually, creating summaries or aggregations before sending to the LLM

For example, instead of sending raw data, pre-process it to create summaries like "There are 120 assets in 'active' status, 45 in 'maintenance'..." which is much more token-efficient.

2. Speed Up DynamoDB Access

To improve DynamoDB performance for large datasets:

  • Use Query instead of Scan whenever possible by designing your data model with appropriate access patterns
  • Implement Global Secondary Indexes (GSIs) to support different query patterns without full table scans
  • Reduce page size for unavoidable Scan operations to minimize impact on provisioned throughput
  • Consider parallel scans for very large tables, dividing the table into segments that can be scanned concurrently

For time-based queries like "alerts on Sunday night," ensure you have appropriate indexes on timestamp fields.

3. Alternative Architectural Approaches

Consider these architectural improvements:

  • Pre-aggregation: Maintain separate tables or records with pre-computed aggregations for common queries
  • Two-phase processing:
  1. First Lambda extracts and processes data from DynamoDB
  2. Second Lambda sends only the summarized results to the LLM
  • Caching layer: Implement a caching mechanism for frequently requested data patterns
  • Analytics pipeline: For complex analytical queries, consider using a dedicated analytics service (like Amazon Athena) alongside DynamoDB

Implementation Example

For a query like "How many assets of each type are in each status right now?":

  1. Lambda queries DynamoDB with appropriate pagination
  2. Process all pages to create a summary count by type and status
  3. Send only the aggregated summary to the LLM for natural language formatting

This approach minimizes both the token count sent to the LLM and the processing time, while still providing accurate answers to user queries.
Sources
DynamoDB AWS SDK SdkIterable taking time to iterate | AWS re:Post
Community | 20 Days of DynamoDB
Best practices for querying and scanning data in DynamoDB - Amazon DynamoDB
Query a DynamoDB table with pagination using an AWS SDK - Amazon DynamoDB
Perform advanced DynamoDB query operations using an AWS SDK - Amazon DynamoDB

answered a year ago

AWS
EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.