Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
Build a Calculator App with Claude Code on AWS Bedrock

Build a Calculator App with Claude Code on AWS Bedrock

Learn to build a calculator web app using Claude Code on AWS Bedrock. Follow best practices for AI-assisted development, cost management, and security.

Jonathan Evans
Amazon Employee
Published Mar 4, 2025
Hello AWS Community!
Following our previous post where we covered how to set up Claude Code with Amazon Bedrock, I'm excited to share a practical tutorial showing Claude Code in action. We'll build a calculator web application step-by-step, demonstrating how this AI coding assistant can help developers create and enhance projects efficiently.

Following Along with this Tutorial

You can follow this tutorial in two ways:
  1. Using the Jupyter Notebook: Access the complete notebook with code examples on the AWS Samples Anthropic on AWS GitHub repository. The notebook contains the same steps we'll cover here, with additional code cells you can execute.
  2. Following this Community Post: All steps are documented here in this post, and you can apply them directly in your terminal with Claude Code.
Both approaches will yield the same result, so choose whichever method you prefer. The steps and commands are identical in both versions to avoid confusion.

Setting Up Your Environment

Let's start by creating a clean workspace for our calculator project:
1
2
3
# Create a project directory
mkdir sandbox
cd sandbox
In the notebook version, we have a sandbox directory ready for you to use alongside the notebook. If following this post directly, simply create the directory and navigate into it.

Project Documentation Setup

Before we start building, we'll create a simple requirements file to guide our project. Create a file called calculator.md with the following basic requirements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Calculator Web Application Requirements

## Functionality
- Basic arithmetic operations (addition, subtraction, multiplication, division)
- Clear button to reset calculations
- Delete button to remove the last entered digit
- Responsive design for mobile and desktop use

## Optional Features
- Scientific calculator functions (sin, cos, tan, log, etc.)
- Calculation history
- Theme switching (light/dark mode)
- Support for brackets and complex expressions

## Technical Requirements
- Vanilla JavaScript (no external libraries)
- CSS for styling
- HTML for structure
- Mobile-responsive design

Project Initialization with /init

Once we have our requirements file, we'll use Claude Code's /init command to analyze our project and create helpful documentation:
1
2
# Launch Claude Code in your project directory
claude
Then run the init command:
1
/init
When you run this command, Claude Code will:
  1. Scan your repository structure
  2. Examine existing documentation (like our calculator.md file)
  3. Look for configurations, patterns, and project organization
  4. Generate a comprehensive CLAUDE.md guide for your project
Image not found
Claude Code Init Command

Understanding the /init Command in Action

The /init command performs a comprehensive analysis of your codebase to create helpful documentation. When executed:
  1. It scans your repository structure, identifying files and their relationships
  2. It examines existing documentation (like the calculator.md file we've created)
  3. It looks for configuration files, coding patterns, and project organization
  4. It checks for existing guidelines (like Cursor rules or Copilot instructions)
Based on this analysis, Claude Code generates a CLAUDE.md file that includes:
  • Practical build and run commands tailored to your project
  • Code style guidelines specific to your technology stack
  • Architecture recommendations that match your existing patterns
  • Documentation standards to maintain consistency
This automated documentation helps both human developers and AI assistants understand your project's structure and conventions, making collaboration more efficient.

Building the Calculator Web Application

Now that we're set up, let's ask Claude to create our calculator application:
1
Now create the site! Please implement the calculator web application based on the requirements in calculator.md and following the guidelines in CLAUDE.md. We'll need HTML, CSS, and JavaScript components to create a functional calculator. Name the HTML file calculator.html
Claude Code will analyze the requirements and propose an implementation plan, creating:
  1. An HTML file for the calculator structure
  2. CSS for styling and responsive design
  3. JavaScript for calculator functionality
Image not found
Claude Code Creating Calculator Files

Accepting and Reviewing Code Additions from Claude Code

When Claude Code suggests code changes or additions, it's important to carefully review them before accepting:

Permission System

  • Claude Code uses a human in the loop permission system for safety
  • File modifications require explicit approval by default
  • You'll be prompted to confirm before changes are made, unless you've accepted the file modification 'don't ask again' prompt

Best Practices for Code Review

  1. Carefully examine suggested changes before approving them
  2. Understand what the code does - ask Claude to explain if needed
  3. Check for edge cases that might not have been considered
  4. Verify security implications of any code that handles sensitive data
  5. Test the changes after they're applied to ensure they work as expected

Approval Options

When Claude suggests a file modification, you'll see options like:
  • Yes: Approve this specific action once
  • Yes, don't ask again: Approve this type of action for the remainder of the session
  • No: Reject the suggested change
⚠️ IMPORTANT ⚠️: You should always use the Yes option and avoid using "Yes, don't ask again" to ensure that all code, file changes, and commands generated by Claude are strictly reviewed by a human. This human-in-the-loop review process is critical for maintaining security and code quality. 👁️ 🔒
Remember that file modification permissions reset when you end your Claude Code session, providing an additional safety layer.
Image not found
Claude Code Permission Request Example

Testing Our Calculator Web Application

Now that Claude has created our calculator web application, let's test it out:

Opening the Calculator

  1. Navigate to the sandbox directory where the calculator files were created
  2. Open the calculator.html file in your web browser
    • You can do this by double-clicking the file or right-clicking and selecting "Open with" your preferred browser

Testing Functionality

Try performing various calculations to ensure everything works correctly:
  • Basic arithmetic operations (addition, subtraction, multiplication, division)
  • Clear functionality (C button)
  • Delete functionality (DEL button)
  • Test the responsive design by resizing your browser window

Potential Enhancements

Remember the customization ideas from our requirements:
  • Scientific calculator functions
  • Calculation history
  • Theme switching
  • Bracket support for complex expressions
Image not found
Calculator Web Application

Managing Context with the /compact Command

The /compact command is a powerful tool for managing context and maintaining efficient conversations with Claude Code.

How /compact Works:

  • Summarizes your conversation history into a concise format
  • Preserves the essential information while reducing token count
  • Maintains context awareness without the full conversation history
  • Executed directly in the Claude Code terminal with the command /compact

Benefits of Using /compact:

  • Maintains Coherence: Unlike /clear, preserves the important parts of your conversation history
  • Enhances Temporal Coherence: Helps Claude better understand the timeline and sequence of your interactions
  • Improves Context Referencing: Makes it easier for Claude to accurately reference and recall information from earlier in the conversation
  • Reduces Token Usage: Significantly lowers the number of tokens consumed, helping manage costs
  • Improves Performance: With less context to process, Claude can respond more quickly
  • Extends Conversation Length: Allows you to have longer, more productive sessions without hitting context limits

Prompt Caching and /compact:

  • With Prompt Caching: When prompt caching is enabled in Bedrock, the cost benefits of /compact are less significant since you're not charged for repeated prompts
  • Primary Benefit Shift: With prompt caching, /compact becomes more valuable for maintaining conversation coherence rather than cost management
  • Still Recommended: Even with prompt caching enabled, using /compact regularly helps Claude maintain a clearer understanding of your conversation flow and project context

When to Use /compact:

  • After completing a major task or milestone
  • When you notice Claude's responses slowing down
  • Before starting a new, related subtask
  • When Claude seems to be confusing the order of past events or instructions
  • When transitioning between different aspects of your project
  • Periodically during long coding sessions (regardless of prompt caching status)
Let's put this into practice! Try using the /compact command in your Claude Code terminal now to summarize our conversation about the calculator project:
1
/compact
Image not found
Claude Code Compact Command

Enhancing Projects with Claude Code: Adding Scientific Calculator Functions

Claude Code excels at understanding and modifying existing projects holistically. After using the /compact command to optimize our conversation context, let's see how Claude Code can help us enhance our calculator application with new functionality:
1
Add optional scientific calculator functions to our application. We should include sin, cos, tan, log, sqrt, and power functions.
When Claude suggests the changes, review them carefully and approve if they look good.
Image not found
Claude Code Scientific Calculator

Testing Your Enhanced Calculator

Now that we've added scientific calculator functions to our application, it's time to test it out:
  1. Open the calculator.html file in your web browser
  2. Try out the basic arithmetic operations (addition, subtraction, multiplication, division)
  3. Test the newly added scientific functions (sin, cos, tan, log, etc.)
  4. Verify that all calculations produce the expected results
  5. Check that the UI displays correctly on your device
Take some time to experiment with different calculations and ensure everything is working as expected!
Image not found
Enhanced Calculator Testing

Monitoring Your Costs with Claude Code

Claude Code provides several ways to track your token usage and associated costs. Understanding and managing these costs is essential, as typical usage ranges from $5-10 per developer per day but can exceed $100 per hour during intensive use.

Claude Code Pricing on Amazon Bedrock

When using Claude Code with Amazon Bedrock, pricing follows Bedrock's standard token-based pricing model. Claude Code uses both Claude 3.7 Sonnet and Claude 3.5 Haiku models, so you'll need access to both in your AWS account.

Tracking Your Costs:

  • During a session: Use the /cost command to see current token usage and estimated costs
  • Session summary: View a cost summary automatically displayed when exiting Claude Code
1
/cost

Strategies to Reduce Token Usage:

  • Compact conversations: Use /compact when your context gets large
  • Write specific queries: Avoid vague requests that trigger unnecessary file scanning
  • Break down complex tasks: Split large tasks into focused interactions
  • Clear history between tasks: Use /clear to reset context when switching topics
Image not found
Claude Code Cost Command

Conclusion: Unleashing the Power of Claude Code with Amazon Bedrock

Claude Code on Amazon Bedrock represents a significant advancement in AI-assisted software development. By combining Anthropic's cutting-edge Claude 3.7 Sonnet model with the security, scalability, and enterprise-grade infrastructure of AWS Bedrock, developers gain access to a powerful coding assistant that can:
  • Understand and navigate complex codebases
  • Generate, edit, and debug code across multiple files
  • Answer detailed questions about architecture and implementation
  • Execute commands and perform real actions in your development environment
  • Maintain context awareness throughout extended coding sessions

Key Benefits of Claude Code on Bedrock:

  • Enterprise Security: Keep your code interactions within your AWS infrastructure
  • Cost Management: Track and optimize token usage with built-in tools
  • Seamless Integration: Leverage your existing AWS Bedrock allocation and billing
  • Developer Experience: Work directly from your terminal with minimal setup
  • Context Management: Maintain productive sessions with /compact and other commands

Next Steps:

  1. Start Small: Begin with focused tasks to build familiarity with Claude Code
  2. Establish Workflows: Develop patterns for effective collaboration with the AI
  3. Monitor Usage: Keep an eye on costs and optimize with /compact and other strategies
  4. Share Knowledge: Document effective prompts and approaches with your team
  5. Stay Updated: Claude Code is actively developing with regular improvements
By following the setup instructions and best practices outlined in this guide and the accompanying notebook, you'll be well-positioned to harness the full potential of Claude Code on Amazon Bedrock for your development projects.
Remember that Claude Code is designed to augment your capabilities as a developer, not replace your expertise. The most effective use comes from a collaborative approach where you provide clear direction and carefully review the AI's suggestions.

Have you tried using Claude Code for your development projects? Share your experiences in the comments below! For more detailed examples and code samples, don't forget to check out the complete notebook on GitHub.
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

1 Comment

Log in to comment