Skip to content

Guidance on deploying a generative AI document analysis with Amazon Bedrock AgentCore. Auto-classifies, enhances, and aggregates multi-type documents using Gestalt-informed vision prompts. Custom analyzer creation wizard. Scripted CDK deployment. Gradio frontend included.

License

Notifications You must be signed in to change notification settings

aws-samples/sample-badgers

🧭 Navigation:
πŸ”΅ Home | Vision LLM Theory | Frontend | Deployment | CDK Stacks | Runtime | S3 Files | Lambda Analyzers | Prompting System | Analyzer Wizard | Pricing Calculator


🦑 BADGERS

Broad Agentic Document Generative Extraction & Recognition System

BADGERS transforms document processing through vision-enabled AI and deep layout analysis. Unlike traditional text extraction tools, BADGERS understands document structure and meaning by recognizing visual hierarchies, reading patterns, and contextual relationships between elements.

πŸ€” Why BADGERS?

Traditional document processing tools extract text but lose context. They can't distinguish a header from body text, understand table relationships, or recognize that a diagram explains the adjacent paragraph. BADGERS solves this by:

  • πŸ—οΈ Preserving semantic structure - Maintains document hierarchy and element relationships
  • πŸ‘οΈ Understanding visual context - Recognizes how layout conveys meaning
  • πŸ“š Processing diverse content - Handles 21+ element types from handwriting to equations
  • πŸ€– Automating complex workflows - Orchestrates multiple specialized analyzers via an AI agent

Use cases: research acceleration, compliance automation, content management, accessibility remediation.

βš™οΈ How It Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                           AgentCore Runtime                                 β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚  PDF Analysis Agent (Strands)                                       β”‚   β”‚
β”‚   β”‚  - Claude Sonnet 4.5 with Extended Thinking                         β”‚   β”‚
β”‚   β”‚  - Session state management                                         β”‚   β”‚
β”‚   β”‚  - MCP tool orchestration                                           β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
                                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                           AgentCore Gateway                                 β”‚
β”‚   - MCP Protocol (2025-03-26)                                               β”‚
β”‚   - Cognito JWT Authentication                                              β”‚
β”‚   - Semantic tool search                                                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚                  β”‚                  β”‚
                   β–Ό                  β–Ό                  β–Ό
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚   Lambda    β”‚    β”‚   Lambda    β”‚    β”‚   Lambda    β”‚
            β”‚  Analyzer   β”‚    β”‚  Analyzer   β”‚    β”‚  Analyzer   β”‚
            β”‚ (29 tools)  β”‚    β”‚             β”‚    β”‚             β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚                  β”‚                  β”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚   Bedrock   β”‚
                               β”‚   Claude    β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. πŸ“„ User submits a document with analysis instructions
  2. 🧠 Strands Agent (running in AgentCore Runtime) interprets the request
  3. πŸ”§ Agent selects tools from 29 specialized analyzers via MCP Gateway
  4. ⚑ Lambda analyzers (29 functions) process document elements using Claude vision models
  5. πŸ“Š Results aggregate with preserved structure and semantic relationships

πŸ› οΈ Tech Stack

Component Technology
πŸ€– Agent Framework Strands Agents
🏠 Agent Hosting Amazon Bedrock AgentCore Runtime
πŸšͺ Tool Gateway Amazon Bedrock AgentCore Gateway (MCP Protocol)
🧠 Foundation Model Claude Sonnet 4.5 (via Amazon Bedrock)
⚑ Compute AWS Lambda (29 analyzer functions)
πŸ“¦ Storage Amazon S3 (configs, prompts, outputs)
πŸ” Auth Amazon Cognito (OAuth 2.0 client credentials)
πŸ—οΈ IaC AWS CDK (Python)
πŸ“ˆ Observability CloudWatch Logs, X-Ray
πŸ“Š Cost Tracking Bedrock Application Inference Profiles

πŸ”¬ Analyzers

Analyzer Purpose
πŸ“Έ pdf_to_images_converter Convert PDF pages to images
🏷️ classify_pdf_content Classify document content type
πŸ“ full_text_analyzer Extract all text content
πŸ“Š table_analyzer Extract and structure tables
πŸ“ˆ charts_analyzer Analyze charts and graphs
πŸ”€ diagram_analyzer Process diagrams and flowcharts
πŸ“ layout_analyzer Document structure analysis
β™Ώ accessibility_analyzer Generate accessibility metadata
πŸ₯ decision_tree_analyzer Medical/clinical document analysis
πŸ”¬ scientific_analyzer Scientific paper analysis
✍️ handwriting_analyzer Handwritten text recognition
πŸ’» code_block_analyzer Extract code snippets
πŸ—‚οΈ metadata_generic_analyzer Generic metadata extraction
πŸ—‚οΈ metadata_mads_analyzer MADS metadata format extraction
πŸ—‚οΈ metadata_mods_analyzer MODS metadata format extraction
πŸ”‘ keyword_topic_analyzer Extract keywords and topics
πŸ”§ remediation_analyzer Analyze remediation requirements
πŸ“„ page_analyzer Single page content analysis
🧱 elements_analyzer Document element detection
🧱 robust_elements_analyzer Enhanced element detection with fallbacks
πŸ‘οΈ general_visual_analysis_analyzer General-purpose visual content analysis
✏️ editorial_analyzer Editorial content and markup analysis
πŸ—ΊοΈ war_map_analyzer Historical war map analysis
πŸŽ“ edu_transcript_analyzer Educational transcript analysis
πŸ”— correlation_analyzer Correlate multi-analyzer results per page
πŸ–ΌοΈ image_enhancer Image enhancement and preprocessing

πŸš€ Deployment

Prerequisites

Quick Start

cd deployment
./deploy_from_scratch.sh

This deploys 10 CloudFormation stacks:

  1. πŸ“¦ S3 (config + output buckets)
  2. πŸ” Cognito (OAuth authentication)
  3. πŸ‘€ IAM (execution roles)
  4. 🐳 ECR (container registry)
  5. ⚑ Lambda (29 analyzer functions)
  6. πŸšͺ Gateway (MCP endpoint)
  7. 🧠 Memory (session persistence)
  8. πŸ“Š Inference Profiles (cost tracking)
  9. πŸƒ Runtime (Strands agent container)
  10. 🧩 Custom Analyzers (optional, wizard-created)

Manual Steps

See deployment/DEPLOYMENT_README.md for step-by-step instructions.

Cleanup

cd deployment
./destroy.sh

πŸ“ Project Structure

β”œβ”€β”€ deployment/
β”‚   β”œβ”€β”€ app.py                 # CDK app entry point
β”‚   β”œβ”€β”€ stacks/                # CDK stack definitions
β”‚   β”œβ”€β”€ lambdas/code/          # Analyzer Lambda functions
β”‚   β”œβ”€β”€ runtime/               # AgentCore Runtime container
β”‚   β”œβ”€β”€ s3_files/              # Prompts, schemas, manifests
β”‚   └── badgers-foundation/    # Shared analyzer framework
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ main.py                # Multi-page Gradio app entry point
β”‚   └── pages/                 # UI modules (chat, wizard, editor, etc.)
└── pyproject.toml

πŸ“„ License

MIT


πŸ” Technical Deep Dive

πŸ“¦ Lambda Layers

BADGERS uses Lambda layers shared across analyzer functions:

πŸ—οΈ Foundation Layer (layer.zip)

  • Built via deployment/lambdas/build_foundation_layer.sh
  • Contains the analyzer framework (7 Python modules)
  • Includes dependencies: boto3, botocore
  • Includes core system prompts used by all analyzers
layer/python/
β”œβ”€β”€ foundation/
β”‚   β”œβ”€β”€ analyzer_foundation.py    # 🎯 Main orchestration class
β”‚   β”œβ”€β”€ bedrock_client.py         # πŸ”„ Bedrock API with retry/fallback
β”‚   β”œβ”€β”€ configuration_manager.py  # βš™οΈ Config loading/validation
β”‚   β”œβ”€β”€ image_processor.py        # πŸ–ΌοΈ Image optimization
β”‚   β”œβ”€β”€ message_chain_builder.py  # πŸ’¬ Claude message formatting
β”‚   β”œβ”€β”€ prompt_loader.py          # πŸ“œ Prompt file loading (local/S3)
β”‚   └── response_processor.py     # πŸ“€ Response extraction
β”œβ”€β”€ config/
β”‚   └── config.py
└── prompts/core_system_prompts/
    └── *.xml

πŸ“„ Poppler Layer (poppler-layer.zip)

  • PDF rendering library for pdf_to_images_converter
  • Built via deployment/lambdas/build_poppler_layer.sh

πŸ”¬ How an Analyzer Works

Each analyzer follows the same pattern using AnalyzerFoundation:

# Lambda handler (simplified)
def lambda_handler(event, context):
    # 1️⃣ Load config from S3 manifest
    config = load_manifest_from_s3(bucket, "full_text_analyzer")

    # 2️⃣ Initialize foundation with S3-aware prompt loader
    analyzer = AnalyzerFoundation(...)

    # 3️⃣ Run analysis pipeline
    result = analyzer.analyze(image_data)

    # 4️⃣ Save result to S3 and return
    save_result_to_s3(result, session_id)
    return {"result": result}

The analyze() method orchestrates:

  1. πŸ–ΌοΈ Image processing - Resize/optimize for Claude's vision API
  2. πŸ“œ Prompt loading - Combine wrapper + analyzer prompts from S3
  3. πŸ’¬ Message building - Format for Bedrock Converse API
  4. πŸ€– Model invocation - Call Claude with retry/fallback logic
  5. βœ… Response processing - Extract and validate result

πŸ“œ Prompting System

Prompts are modular XML files composed at runtime:

s3://config-bucket/
β”œβ”€β”€ core_system_prompts/
β”‚   β”œβ”€β”€ prompt_system_wrapper.xml   # 🎁 Main template with placeholders
β”‚   β”œβ”€β”€ core_rules/rules.xml        # πŸ“ Shared rules for all analyzers
β”‚   └── error_handling/*.xml        # ⚠️ Error response templates
β”œβ”€β”€ prompts/{analyzer_name}/
β”‚   β”œβ”€β”€ {analyzer}_job_role.xml     # πŸ‘€ Role definition
β”‚   β”œβ”€β”€ {analyzer}_context.xml      # 🌍 Domain context
β”‚   β”œβ”€β”€ {analyzer}_rules.xml        # πŸ“ Analyzer-specific rules
β”‚   β”œβ”€β”€ {analyzer}_tasks.xml        # βœ… Task instructions
β”‚   └── {analyzer}_format.xml       # πŸ“‹ Output format spec
└── wrappers/
    └── prompt_system_wrapper.xml

The PromptLoader composes the final system prompt:

<!-- prompt_system_wrapper.xml -->
<system_prompt>
    {core_rules}           <!-- πŸ“ Injected from core_rules/rules.xml -->
    {composed_prompt}      <!-- 🧩 Injected from analyzer prompt files -->
    {error_handler_general}
    {error_handler_not_found}
</system_prompt>

Placeholders like [[PIXEL_WIDTH]] and [[PIXEL_HEIGHT]] are replaced with actual image dimensions at runtime.

βš™οΈ Configuration System

Each analyzer has a manifest file in S3:

// s3://config-bucket/manifests/full_text_analyzer.json
{
    "tool": {
        "name": "analyze_full_text_tool",
        "description": "Extracts text content maintaining reading order...",
        "inputSchema": {
            "type": "object",
            "properties": {
                "image_path": { "type": "string" },
                "session_id": { "type": "string" },
                "audit_mode": { "type": "boolean" }
            },
            "required": ["image_path", "session_id"]
        }
    },
    "analyzer": {
        "name": "full_text_analyzer",
        "enhancement_eligible": true,
        "model_selections": {
            "primary": "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
            "fallback_list": [
                "us.anthropic.claude-haiku-4-5-20251001-v1:0",
                "us.amazon.nova-premier-v1:0"
            ]
        },
        "max_retries": 3,
        "prompt_files": [
            "full_text_job_role.xml",
            "full_text_context.xml",
            "full_text_rules.xml",
            "full_text_tasks_extraction.xml",
            "full_text_format.xml"
        ],
        "max_examples": 0,
        "analysis_text": "full text content",
        "expected_output_tokens": 6000,
        "output_extension": "xml"
    }
}

Key configuration features:

  • πŸ”„ Model fallback chain - Primary model with ordered fallbacks
  • πŸ” Retry logic - Configurable retry count per analyzer
  • 🧩 Prompt composition - List of XML files to combine
  • πŸ“‹ Tool schema - MCP-compatible input schema for Gateway
  • πŸ–ΌοΈ Enhancement eligible - Flag indicating analyzer benefits from image preprocessing (used by image_enhancer tool)

Global settings (from environment or defaults):

{
    "max_tokens": 8000,
    "temperature": 0.1,
    "max_image_size": 20971520,  # 20MB
    "max_dimension": 2048,
    "jpeg_quality": 85,
    "throttle_delay": 1.0,
    "aws_region": "us-west-2"
}

πŸ“Š Inference Profiles for Cost Tracking

BADGERS uses Application Inference Profiles to enable cost allocation and usage monitoring. The system maps model IDs to profile ARNs at runtime:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Inference Profile Flow                               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                             β”‚
β”‚  1. CDK deploys InferenceProfilesStack                                      β”‚
β”‚     └─> Creates ApplicationInferenceProfile for each model                  β”‚
β”‚         β€’ badgers-claude-sonnet-{id}  (Global)                              β”‚
β”‚         β€’ badgers-claude-haiku-{id}   (Global)                              β”‚
β”‚         β€’ badgers-claude-opus-{id}    (Global)                              β”‚
β”‚         β€’ badgers-nova-premier-{id}   (US)                                  β”‚
β”‚                                                                             β”‚
β”‚  2. Runtime receives profile ARNs as environment variables                  β”‚
β”‚     └─> CLAUDE_SONNET_PROFILE_ARN, CLAUDE_HAIKU_PROFILE_ARN, etc.           β”‚
β”‚                                                                             β”‚
β”‚  3. At invocation, bedrock_client.py maps model_id β†’ profile ARN            β”‚
β”‚     └─> "global.anthropic.claude-sonnet-4-5-*" β†’ $CLAUDE_SONNET_PROFILE_ARN β”‚
β”‚                                                                             β”‚
β”‚  4. Bedrock invoked with profile ARN (enables cost tracking)                β”‚
β”‚     └─> Falls back to model ID if no profile configured                     β”‚
β”‚                                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Model ID to environment variable mapping:

Model Pattern Environment Variable
*claude-sonnet-4-5* CLAUDE_SONNET_PROFILE_ARN
*claude-haiku-4-5* CLAUDE_HAIKU_PROFILE_ARN
*claude-opus-4-6* CLAUDE_OPUS_PROFILE_ARN
*nova-premier* NOVA_PREMIER_PROFILE_ARN

βž• Adding a New Analyzer

Option 1: Use the Wizard (Recommended)

cd frontend
uv run python main.py

The Analyzer Creation Wizard is available as a tab in the multi-page Gradio app.

Option 2: Manual Creation

  1. πŸ“œ Create prompt files in deployment/s3_files/prompts/{analyzer_name}/
  2. πŸ“‹ Create manifest in deployment/s3_files/manifests/{analyzer_name}.json
  3. πŸ“ Create schema in deployment/s3_files/schemas/{analyzer_name}.json
  4. ⚑ Create Lambda code in deployment/lambdas/code/{analyzer_name}/lambda_handler.py
  5. πŸ“ Register in deployment/stacks/lambda_stack.py
  6. πŸš€ Redeploy: cdk deploy badgers-lambda badgers-gateway

Notices

Customers are responsible for making their own independent assessment of the information in this Guidance. This Guidance: (a) is for informational purposes only, (b) represents AWS current product offerings and practices, which are subject to change without notice, and (c) does not create any commitments or assurances from AWS and its affiliates, suppliers or licensors. AWS products or services are provided "as is" without warranties, representations, or conditions of any kind, whether express or implied. AWS responsibilities and liabilities to its customers are controlled by AWS agreements, and this Guidance is not part of, nor does it modify, any agreement between AWS and its customers.


Authors

  • Randall Potter

πŸ“– Further Reading

πŸ€– Amazon Bedrock & Foundation Models

πŸš€ Amazon Bedrock AgentCore

⚑ AWS Lambda

πŸ” Amazon Cognito

πŸ“ˆ Observability

πŸ“¦ Amazon S3

πŸ’» Amazon Kiro IDE

About

Guidance on deploying a generative AI document analysis with Amazon Bedrock AgentCore. Auto-classifies, enhances, and aggregates multi-type documents using Gestalt-informed vision prompts. Custom analyzer creation wizard. Scripted CDK deployment. Gradio frontend included.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages