Skip to content
reaatechREAATECH

Files · AWS Bedrock Cost Control for Serverless AI Applications

56 (0 binary, 276.9 kB total)attempt 2

README.md·2990 B·markdown
markdown
# AWS Bedrock Cost Control for Serverless AI Applications
 
A budget-aware middleware that routes, caps, and tracks every AWS Bedrock LLM call, preventing surprise bills for SMBs running serverless AI features.
 
A tutorialized reference solution from [reaatech.com](https://reaatech.com), demonstrating how to build production-grade AI systems with the `@reaatech/*` package family.
 
## Features
 
- **Budget Enforcement**: Per-scope budget limits with configurable thresholds
- **LLM Routing**: Intelligent routing between different model providers
- **Cost Tracking**: Real-time spend monitoring with Helicone integration
- **OpenTelemetry**: Built-in observability and tracing support
- **AWS Bedrock**: Native support for Claude and other Bedrock models
 
## Prerequisites
 
- Node.js >= 22
- pnpm >= 10
- AWS account with Bedrock access
- Helicone account for AI observability
 
## Setup
 
1. Copy the environment file:
   ```bash
   cp .env.example .env
   ```
 
2. Configure your credentials in `.env`:
   - `AWS_ACCESS_KEY_ID`: Your AWS access key
   - `AWS_SECRET_ACCESS_KEY`: Your AWS secret key
   - `HELICONE_API_KEY`: Your Helicone API key (for usage tracking)
 
3. Install dependencies:
   ```bash
   pnpm install
   ```
 
## Environment Variables
 
| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `AWS_REGION` | Yes | - | AWS region (e.g., us-east-1) |
| `AWS_ACCESS_KEY_ID` | Yes | - | AWS access key ID |
| `AWS_SECRET_ACCESS_KEY` | Yes | - | AWS secret access key |
| `HELICONE_API_KEY` | Yes | - | Helicone API key for observability |
| `HELICONE_TEAM_ID` | No | - | Helicone team ID |
| `PORT` | No | 3001 | Server port |
| `DEFAULT_BUDGET_LIMIT` | No | 10.0 | Default budget limit in USD |
| `NODE_ENV` | No | development | Environment (development/production/test) |
 
## Development
 
```bash
# Run in development mode
pnpm dev
 
# Type checking
pnpm typecheck
 
# Linting
pnpm lint
 
# Run tests
pnpm test
```
 
## API Endpoints
 
- `GET /api/health` - Health check
- `POST /api/invoke` - Invoke LLM with budget enforcement
- `GET /api/budget` - Get all budget scopes
- `GET /api/budget/:scopeType/:scopeKey` - Get budget for specific scope
 
## Project Structure
 
```
app/                  Next.js App Router pages + API routes
src/                  services, lib, adapters
  lib/env.ts          Environment variable validation (Zod schema)
  services/           Core services
    bedrock.ts        AWS Bedrock integration
    budget.ts         Budget enforcement
    helicone.ts       Helicone observability
    interceptor.ts    Request/response interception
    otel.ts           OpenTelemetry setup
    pricing.ts        Cost pricing logic
    router.ts         LLM routing
    spend-tracker.ts  Spend tracking
tests/                Vitest test suite (mirrors src/)
```
 
## Testing
 
```bash
# Run all tests with coverage
pnpm test
 
# Run tests in watch mode
pnpm test -- --watch
```
 
## License
 
MIT — see [LICENSE](./LICENSE).