Skip to content

Generative AI Engineer (Part 2) — Interview 4

Q: How do you manage the process of converting documents into embeddings and indexing them for use with the system?

  • The entire process is automated and designed for scalability and reliability, using AWS Glue, S3, and OpenSearch.
  • Here’s how the pipeline works in production:
  • Metadata Detection: An AWS Glue job runs on a schedule (daily), scanning the S3 bucket for new or updated document metadata exported from the central CAS knowledge system.
  • Document Retrieval: For each new metadata entry, the pipeline uses the document ID to call the CAS API, fetches the render link, and downloads the actual document (PDF, HTML, etc.) into our S3 bucket.
  • Document Processing: The downloaded document is converted to a standard format (Markdown) and split into manageable chunks (using a chunking strategy, typically up to 25 KB per chunk).
  • Embedding Generation: Each chunk is summarized (if needed) using Azure OpenAI, and then an embedding is generated (1536-dimensional vector) via the OpenAI/Azure OpenAI embedding API.
  • Metadata Enrichment: Each chunk is assigned metadata (chunk ID, source, language, title, timestamps, etc.) and the embedding vector.
  • Bulk Indexing: All processed chunks and their metadata are batched and ingested into OpenSearch using the Bulk API, making them immediately searchable.
  • Incremental Updates: The pipeline only processes new or changed documents, ensuring efficient incremental updates without manual intervention.
  • This automated ETL ensures that all new documents are quickly available for semantic search and RAG-based LLM workflows, supporting continuous data growth and real-time enterprise needs.

Q: If you store metadata with each chunk in the vector DB, will semantic search still return the correct result?

  • Yes, semantic search will still return the correct result even when metadata is stored with each chunk.
  • The vector database (OpenSearch) stores both the embedding vector and associated metadata (like chunk ID, source, language, title, timestamps, etc.) for each chunk as a single document.
  • During semantic (KNN) search, only the embedding vector is used to compute similarity and retrieve the most relevant chunks based on the query’s meaning.
  • Once relevant chunks are retrieved, their metadata is included in the search results, allowing you to identify the source document, chunk position, language, and other context.
  • This approach ensures:
  • Accurate semantic retrieval based on content meaning.
  • Easy traceability and context enrichment using metadata.
  • Support for hybrid search (combining semantic and lexical/BM25) for even better relevance, as described in the Knowledge GPT architecture.
  • Storing metadata alongside embeddings is a standard practice and does not interfere with the semantic search process; it actually enhances downstream processing and user experience.

Q: How would you design a scalable, cost-optimized agent system to handle 1000 transactions per second while managing user context?

  • Scalable Architecture:
  • Use a microservices-based architecture deployed on AWS (EKS, ECS, or Lambda) to auto-scale based on incoming traffic.
  • Deploy stateless API services (FastAPI) behind a load balancer (ALB/NLB) to distribute requests evenly.
  • Use managed vector DBs (OpenSearch) and cache layers (Redis/ElastiCache) for fast retrieval and reduced DB load.
  • Store user session/context in a distributed cache (Redis) or DynamoDB for quick access and horizontal scaling.

  • Efficient Context Management:

  • Store only essential context (recent conversation turns, user ID, session metadata) to minimize memory and storage usage.
  • Use context windowing: limit the number of previous messages sent to the LLM (e.g., last 3-5 turns) to control prompt size and cost.
  • For long conversations, summarize older context using LLMs and store summaries instead of full history.

  • Cost Optimization:

  • Batch embedding and retrieval requests where possible to reduce API calls and latency.
  • Use hybrid search (BM25 + KNN) with Reciprocal Rank Fusion (RRF) to optimize retrieval quality and minimize unnecessary LLM calls.
  • Implement request throttling and rate limiting to prevent abuse and control cloud resource usage.
  • Use spot instances or serverless compute for non-critical workloads to reduce compute costs.
  • Monitor usage and set up autoscaling policies to match resource allocation with real-time demand.

  • Production-Grade Practices:

  • Use CI/CD pipelines for rapid deployment and rollback.
  • Implement observability (CloudWatch, Prometheus, Grafana) for real-time monitoring, alerting, and cost tracking.
  • Continuously profile and optimize LLM prompt engineering to minimize token usage and API costs.

  • Summary:

  • By combining stateless, auto-scalable services, efficient context management, and cost-aware retrieval and LLM usage, the system can reliably handle 1000+ TPS while keeping costs under control and maintaining high performance.

Q: How would you handle scalability from the application (not just DevOps) perspective?

  • At the application level, I focus on designing stateless, modular services and efficient resource management to ensure scalability:
  • Stateless Agent Services: Each agent or API instance does not store session data locally. All user context and session state are managed in distributed caches (like Redis/ElastiCache) or DynamoDB, enabling any instance to handle any request.
  • Asynchronous Processing: Use asynchronous request handling (async Python frameworks like FastAPI/Starlette) to maximize throughput and minimize blocking, especially for I/O-heavy tasks like calling LLM APIs or vector DBs.
  • Batching and Queueing: For high-throughput scenarios, batch similar requests (like embedding generation or retrieval) and use message queues (SQS/Kafka) to smooth out spikes and decouple components.
  • Context Window Optimization: Limit the context sent to LLMs (e.g., last N turns or summarized history) to control token usage and latency, which directly impacts cost and performance.
  • Connection Pooling: Use efficient connection pooling for DBs, vector stores, and external APIs to avoid bottlenecks and resource exhaustion.
  • Horizontal Scaling: Application logic is designed so that any number of agent instances can be added or removed without affecting correctness or user experience.
  • Graceful Degradation: Implement fallback mechanisms (e.g., cached responses, reduced context) if downstream services (LLM, vector DB) are under heavy load.
  • Observability and Auto-Tuning: Integrate real-time monitoring (CloudWatch, OpenTelemetry) and auto-tune parameters (like batch size, context window) based on live traffic and performance metrics.
  • Cost Controls: Use guardrails in the agent logic to limit expensive operations (like LLM calls per user/session) and monitor usage patterns for optimization.

  • This approach ensures the application can handle thousands of concurrent users, maintain low latency, and keep costs predictable, even as load increases.


Q: Does batching or queuing jobs after implementing async processing increase latency and negatively impact user experience?

  • Batching and queuing can introduce some additional latency, especially if you wait to accumulate enough requests before processing.
  • However, in high-throughput systems, batching is often necessary to optimize resource usage (like embedding generation or LLM API calls) and reduce costs.
  • The key is to balance batch size and wait time:
  • Use small, time-bound batches (e.g., process every 50ms or after N requests, whichever comes first) to minimize added latency.
  • For real-time user-facing APIs, prioritize async processing and only use batching for backend-heavy or non-blocking tasks (like background data enrichment, logging, or analytics).
  • For critical user interactions (like chat or search), keep the main request path as fast as possible—use async I/O, stateless services, and direct retrieval.
  • Use queues mainly for tasks that can tolerate slight delays (e.g., retraining, large document ingestion, or bulk updates).
  • Monitor end-to-end latency and user experience metrics. If batching causes noticeable delays, reduce batch size or move batching to non-critical paths.
  • In summary, batching and queuing are powerful for scalability and cost, but should be carefully tuned and applied only where they don’t harm UX. For real-time APIs, async processing with minimal batching is best.

Q: How would you manage scaling and orchestration of many agent instances (e.g., 1 to 10,000 agents) from the application perspective?

  • I would use a combination of Kubernetes (EKS/ECS) and a service discovery mechanism to manage agent instances efficiently:
  • Kubernetes Orchestration: Deploy each agent as a containerized microservice (pod) in an EKS cluster. Use Horizontal Pod Autoscaler (HPA) to automatically scale the number of agent pods based on CPU, memory, or custom metrics (like request rate).
  • Service Discovery: Register each agent as a Kubernetes Service with a stable DNS name. The MCP Gateway or orchestrator can route requests to the correct agent using Kubernetes DNS and built-in load balancing.
  • Stateless Design: Agents are stateless; all session/context data is stored in distributed caches (Redis, DynamoDB) or state stores, so any agent instance can handle any request.
  • Dynamic Scaling: With HPA and Cluster Autoscaler (or Karpenter), the system can scale from 1 to 10,000+ agent pods as needed, without manual intervention.
  • Health Checks & Readiness Probes: Use liveness and readiness probes to ensure only healthy agent pods receive traffic. Unhealthy pods are automatically replaced.
  • Multi-AZ & Resilience: Deploy across multiple availability zones for high availability. Use PodDisruptionBudgets to maintain minimum agent availability during updates or failures.
  • Cost & Resource Management: Use resource requests/limits for each agent pod to optimize cluster utilization and control costs. Monitor with Prometheus/Grafana or AWS CloudWatch.
  • Custom Orchestration (if needed): For advanced scenarios, use MCP (Model Context Protocol) for multi-agent orchestration, tool invocation, and memory/state management, as described in the KGPT MCP design.

  • This approach ensures seamless, automated scaling and management of agent instances, supporting both small and massive workloads with high reliability and efficiency.


Q: How do you manage and keep the distributed cache and memory layer updated when scaling agent instances in real time?

  • I use a centralized, distributed cache system like Redis (ElastiCache) or DynamoDB to store all session and context data, ensuring all agent instances access the same up-to-date information.
  • When scaling up or down, new agent pods automatically connect to the same cache endpoint, so there’s no risk of stale or isolated data.
  • All read/write operations for user context, session state, and temporary memory go through the distributed cache, not local memory, so updates are instantly visible to all agents.
  • For cache consistency:
  • Use atomic operations and TTL (time-to-live) settings to avoid stale data.
  • Implement cache invalidation strategies—when data changes (e.g., user updates context), the cache is updated immediately, and any outdated entries are purged.
  • For critical updates, use pub/sub mechanisms (Redis Pub/Sub or AWS SNS) to notify all agent instances of changes, ensuring real-time synchronization.
  • For high availability and resilience:
  • Use Redis clusters with multi-AZ deployment and automatic failover.
  • Monitor cache health and set up alerts for latency or replication lag.
  • This approach ensures that, regardless of how many agent instances are running or scaling, all agents always have access to the latest, consistent context and memory data, supporting seamless user experience and reliable multi-agent orchestration.

Q: Is enabling TTL (time-to-live) on agent memory/cache helpful or could it cause problems for users in AI agent systems?

  • Enabling TTL on cache/memory can be both helpful and risky, depending on the use case:
  • Helpful: TTL helps prevent stale or outdated data from persisting in the cache, which is important for stateless, scalable systems. It also helps manage memory usage and ensures that unused sessions are cleaned up automatically.
  • Risky: In AI agent systems, especially those maintaining conversational or contextual memory, aggressive TTL settings can cause important user context to expire unexpectedly. This can disrupt user experience, as users may lose conversation history or context if they return after the TTL expires.
  • Best Practices:
  • Use TTL for short-lived, non-critical session data or temporary states (e.g., authentication tokens, transient cache).
  • For critical user context or long-running conversations, store data in a persistent store (like DynamoDB or PostgreSQL) with point-in-time recovery and only use cache for fast access.
  • Implement logic to refresh TTL on active sessions (sliding expiration), so active users don’t lose context.
  • Always inform users if their session is about to expire, or provide a way to restore context if possible.
  • Summary: TTL is useful for managing resources and avoiding stale data, but in AI agent systems, it must be carefully configured to avoid negatively impacting user experience. Use persistent storage for important context and apply TTL mainly to non-critical or inactive data.

Q: How do you balance cache management (like TTL) with maintaining a good user experience in agent systems?

  • To balance cache efficiency and user experience in agent systems, I use a hybrid approach:
  • Short-lived Data in Cache: Store temporary or non-critical data (like recent tokens, transient session info) in Redis/ElastiCache with a reasonable TTL to manage memory and avoid stale data.
  • Critical Context in Persistent Store: Store important user context, conversation history, and long-term memory in a persistent database (like DynamoDB or PostgreSQL) with point-in-time recovery. This ensures that even if cache entries expire, user context is not lost.
  • Sliding Expiration: For active sessions, implement sliding TTL—refresh the cache expiration time on each user interaction. This keeps active users’ context alive while cleaning up only truly inactive sessions.
  • Cache Invalidation & Sync: When user context is updated, immediately update both cache and persistent store to keep data consistent. Use cache invalidation strategies to prevent serving outdated information.
  • User Experience Safeguards: Monitor session expiry and, if a session is about to expire, notify the user or provide a way to restore context from persistent storage.
  • Observability: Use metrics and tracing (CloudWatch, OpenTelemetry, Grafana) to monitor cache hit/miss rates, latency, and user session patterns, allowing for continuous tuning of TTL and cache policies.

  • This approach ensures high performance and scalability from caching, while persistent storage guarantees that important user context and experience are never lost, even during scaling or cache eviction events.


Q: How do you handle disaster recovery in AI systems?

  • For disaster recovery in AI systems, I follow a multi-layered approach to ensure data integrity, service continuity, and minimal downtime:
  • Automated Backups: Enable automated, point-in-time backups for all persistent data stores (e.g., DynamoDB, PostgreSQL, S3). Schedule regular snapshots and store them in multiple availability zones or regions.
  • Multi-AZ & Multi-Region Deployment: Deploy critical components (APIs, databases, caches, agent services) across multiple availability zones (AZs) and, if needed, across regions for high availability and failover.
  • Stateless Services: Design agent and API services to be stateless, so they can be quickly redeployed or scaled in any healthy zone without data loss.
  • Distributed Cache with Persistence: Use Redis with AOF/RDB persistence or DynamoDB with global tables to ensure cache/state data can be restored or accessed from another region if needed.
  • Infrastructure as Code (IaC): Use tools like Terraform or AWS CloudFormation to automate infrastructure provisioning and recovery, enabling rapid redeployment in case of failure.
  • Disaster Recovery Drills: Regularly test failover and recovery procedures (e.g., simulate region failure, restore from backup, switch traffic) to ensure readiness.
  • Monitoring & Alerts: Set up comprehensive monitoring (CloudWatch, Prometheus) and alerting for failures, latency spikes, or data inconsistencies, enabling fast response.
  • Audit Trails & Governance: Maintain audit logs (e.g., via AWS CloudTrail, EventBridge) for all critical operations, supporting traceability and compliance during recovery.
  • Documentation & Runbooks: Maintain clear disaster recovery runbooks and escalation procedures for the team.

  • This approach ensures that, even in the event of major failures, the AI system can recover quickly with minimal data loss and service disruption, maintaining reliability and trust for users and stakeholders.


Q: How would you design a highly available RAG or multi-agent system with strict data isolation, security, and prompt injection protection for specified users?

  • High Availability:
  • Deploy all core services (APIs, vector DBs, agent services) across multiple availability zones and, if needed, multiple regions.
  • Use managed services like AWS Aurora, DynamoDB, and OpenSearch with multi-AZ replication.
  • Implement automated backups and point-in-time recovery for all persistent stores.

  • Data Isolation:

  • Enforce strict tenant/user isolation at the data layer:
  • Use logical partitioning (e.g., tenant/user IDs as partition keys in DynamoDB, index-level isolation in OpenSearch).
  • Apply row-level security or access control lists (ACLs) to ensure users can only access their own data.
  • For multi-tenant systems, use separate indices or databases per tenant if required for compliance.
  • Integrate with enterprise SSO (OAuth2/OIDC) for unified identity and access management.

  • Security Controls:

  • Use JWT authentication with RS256 signature verification for every API request.
  • Implement API product authorization and disclosure level capping to restrict data access based on user roles and clearance.
  • Apply input sanitization to block script tags, JavaScript URIs, and other injection vectors.
  • Enforce zero-trust architecture: mTLS for all service-to-service communication, token validation everywhere.
  • Maintain audit trails and structured logging for all access and modification events.

  • Prompt Injection & PII Detection:

  • Integrate a prompt injection detection validator:
  • Analyze all user input (including conversation history) for injection patterns using a dedicated detection model.
  • If injection is detected (score > threshold), block the request and return a safe fallback message—no LLM or search call is made.
  • If the detection service is down, fail open to maintain availability, but log the event for review.
  • Add PII detection in parallel to block or redact sensitive personal data before it reaches the LLM or is stored.

  • Governance & Observability:

  • Use distributed tracing and structured logging for full visibility.
  • Regularly review audit logs and security events.
  • Implement custom guardrails and cost controls as needed.

  • Reference Architecture:

  • Use API Gateway or MCP Proxy for traffic management and security.
  • Deploy MCP servers with unified identity, standard contracts, and loose coupling for easy scaling and integration.
  • Follow best practices from the KGPT and MCP design documents for multi-agent orchestration, event flows, and built-in guardrails.

  • This design ensures high availability, strict data isolation, robust security, and advanced prompt injection protection, meeting enterprise requirements for RAG and multi-agent AI systems.


Q: How do you handle prompt injection attacks at the server side to prevent users from accessing other users' data?

  • To prevent prompt injection attacks and protect user data, I implement a dedicated prompt injection detection layer at the server side:
  • Prompt Injection Detection Validator:
  • All user messages (including conversation history and current input) are combined and split into manageable windows (e.g., 256 words each).
  • Each window is sent to an external ML-based prompt injection detection API.
  • The API returns a score for each window; if any window has a score above the threshold (e.g., 0.5), the request is blocked.
  • When blocked, the system returns a safe fallback message and does not proceed with any search or LLM call, ensuring no information leakage.
  • If the detection service is unavailable, the system fails open (request proceeds), but this is logged for review—availability is prioritized, but such events are monitored.
  • Additional Security Controls:
  • Input sanitization removes script tags, JavaScript URIs, and other potentially harmful content before processing.
  • JWT authentication and disclosure level enforcement ensure users can only access data they are authorized for, even if prompt injection is attempted.
  • Audit logging tracks all requests and responses, with masking for sensitive data, to support monitoring and incident response.
  • Parallel PII Detection:
  • All user input is also checked for PII using AWS Comprehend, and requests containing sensitive personal data are blocked before reaching the LLM.
  • Summary:
  • This multi-layered approach—combining prompt injection detection, input sanitization, strict authentication/authorization, and audit logging—ensures that even if a user tries to inject malicious prompts, they cannot access another user's data or compromise the system.
  • These controls are based on proven patterns from enterprise-grade RAG and multi-agent systems, as implemented in the Knowledge GPT architecture.

Q: How do you ensure that sensitive personal information (PII) is not stored or exposed by the system, especially when LLMs might output or log such data?

  • Parallel PII Detection:
  • PII detection is run in parallel with other validators before any LLM call or data storage.
  • All user messages are joined and scanned for PII entities (like email, SSN, phone, credit card) using AWS Comprehend.
  • If PII is detected, the request is hard-blocked (returns 400), and no further processing or storage occurs.
  • This check is language-aware (supports "en" and "es"); if unsupported, the check is skipped (fail-open), but this is logged.

  • Output Filtering:

  • After the LLM generates a response, an additional content filter (e.g., Azure Content Filter) checks for harmful or policy-violating output.
  • If triggered, a safe fallback message is returned to the user, and the original output is not shown or stored.

  • Audit Logging with Masking:

  • All input and output logs are masked for PII—detected entities are replaced with placeholders like [EMAIL_ADDRESS], [PHONE], etc.
  • If logging is disabled for user input, the query is masked as "**" in all logs.
  • No sensitive data is ever logged in plain text.

  • Input Sanitization:

  • All user input is sanitized to remove script tags, JavaScript URIs, and other potentially harmful content before any processing or storage.

  • Strict Access Controls:

  • JWT authentication and disclosure level enforcement ensure only authorized users can access data, further reducing risk of accidental exposure.

  • Summary:

  • By running PII detection in parallel before LLM calls and storage, filtering LLM outputs, masking logs, and sanitizing inputs, the system ensures that sensitive personal information is neither stored nor exposed, meeting enterprise security and compliance requirements.
  • These controls are implemented as described in the Knowledge GPT API architecture, using modules like comprehend.py, external_validator.py, and audit logging with masking.

Q: How do you ensure strong PII and content filtering without negatively impacting user experience?

  • To balance security (PII/content filtering) and user experience, I use these strategies:
  • Parallel Validation:
  • All validators (PII detection, prompt injection, etc.) run in parallel before the LLM call, minimizing latency and ensuring fast feedback to the user.
  • Fail-Open for Language/Service Limits:
  • If PII detection is not supported for a user’s language or the detection service is down, the system skips the check (fail-open) instead of blocking the user. This avoids unnecessary rejections for non-English users and maintains availability.
  • Clear, Friendly Fallbacks:
  • When a request is blocked (due to PII or policy violation), the system returns a clear, user-friendly fallback message explaining why the request couldn’t be processed, rather than a generic error.
  • No Unnecessary Blocking:
  • Only block requests when there is a confirmed risk (e.g., PII detected, high prompt injection score). Otherwise, allow the request to proceed.
  • Continuous Monitoring & Tuning:
  • Monitor false positives/negatives and user feedback to tune detection thresholds and improve the balance between security and usability.
  • Transparent Communication:
  • Inform users about input requirements and reasons for any blocks, so they can adjust their queries and avoid frustration.

  • This approach ensures that security controls are robust but do not create unnecessary friction, keeping the system both safe and user-friendly.


Q: How do you maximize hardware (GPU/server) utilization when deploying your own open-source LLM to serve all users efficiently?

  • Model Quantization & Optimization:
  • Use quantized versions of the LLM (e.g., 8-bit or 4-bit) to reduce memory and compute requirements, allowing more concurrent requests per GPU.
  • Apply model optimizations like ONNX, TensorRT, or DeepSpeed for faster inference and lower latency.

  • Efficient Serving Frameworks:

  • Deploy the LLM using optimized serving frameworks such as vLLM, Triton Inference Server, or HuggingFace Text Generation Inference, which support batching and multi-GPU scaling.
  • Enable dynamic batching to combine multiple user requests into a single inference pass, maximizing GPU throughput.

  • Resource-Aware Scheduling:

  • Use a job scheduler (like Kubernetes with GPU support) to allocate GPU resources efficiently across multiple LLM instances or microservices.
  • Set resource limits and requests to prevent overloading and ensure fair sharing among users.

  • Autoscaling & Load Balancing:

  • Implement autoscaling policies to spin up/down LLM containers based on real-time demand and GPU utilization.
  • Use a load balancer to distribute incoming requests evenly across available servers/GPUs.

  • Queue Management:

  • Introduce a request queue with rate limiting and prioritization to handle peak loads gracefully and avoid GPU starvation.

  • Monitoring & Profiling:

  • Continuously monitor GPU utilization, memory usage, and inference latency using tools like NVIDIA DCGM, Prometheus, or custom scripts.
  • Profile workloads to identify bottlenecks and tune batch sizes, concurrency, and model parameters for optimal performance.

  • Summary:

  • By combining model quantization, efficient serving frameworks, dynamic batching, resource-aware scheduling, autoscaling, and real-time monitoring, you can maximize limited GPU/server utilization and ensure all users get reliable access to LLM services, even with constrained hardware.
  • These are industry-standard practices for scalable, cost-effective LLM deployment in production environments.

Q: What are the options for efficiently deploying and serving an open-source LLM on on-premise GPUs/servers?

  • Model Quantization & Optimization:
  • Use quantized models (4-bit, 8-bit) to reduce GPU memory usage and increase throughput.
  • Apply optimization libraries like ONNX Runtime, DeepSpeed, or TensorRT for faster inference.

  • Efficient Serving Frameworks (On-Prem):

  • Deploy with open-source frameworks designed for local hardware:
  • vLLM: Highly efficient, supports dynamic batching and multi-GPU.
  • Triton Inference Server: NVIDIA’s open-source server for multi-model, multi-framework serving with GPU support.
  • HuggingFace Text Generation Inference: Can be run on-prem, supports batching and optimized for transformers.
  • FastAPI/Flask Custom Server: For lightweight, custom deployments with direct GPU access.

  • Batching & Scheduling:

  • Implement dynamic batching to combine multiple user requests into a single inference pass, maximizing GPU utilization.
  • Use job schedulers (like Slurm or Kubernetes with GPU support) to allocate and balance workloads across available GPUs.

  • Resource Monitoring & Scaling:

  • Continuously monitor GPU utilization and inference latency.
  • Adjust batch sizes, concurrency, and model parameters based on real-time metrics.

  • CI/CD & Automation:

  • Use scripts and pipelines (as in the UIDS project: deploy-sagemaker.sh, deploy-endpoints.sh, Jenkinsfile) to automate deployment and updates, even for on-prem environments.

  • Summary:

  • By combining quantized models, optimized serving frameworks, batching, resource-aware scheduling, and automated deployment, you can maximize the efficiency and throughput of your on-prem LLM deployment, ensuring all users get reliable access with limited hardware.
  • These are standard industry practices for scalable, cost-effective LLM serving on local infrastructure.