Files · OpenAI Cost Control for SMB Agent Workflows
75 (1 binary, 580.9 kB total)attempt 1
README.md·2975 B·markdown
markdown
# OpenAI Cost Control for SMB Agent Workflows
> Slash OpenAI spending with AI-powered caching and budget enforcement for small business support teams.
A tutorialized reference solution from [reaatech.com](https://reaatech.com), demonstrating how to build production-grade AI cost control with the `@reaatech/*` package family.
## Architecture
This solution wraps every OpenAI call with `@reaatech/llm-cost-telemetry` for real-time token cost capture, enforces monthly budgets via `@reaatech/agent-budget-engine`, and uses `@reaatech/llm-cache` with Redis to serve cached responses for semantically similar prompts, cutting redundant API costs. `@reaatech/llm-cost-telemetry-aggregation` rolls up spend across tenants, and Langfuse provides OTel-compliant observability.
## API Endpoints
| Endpoint | Method | Description |
|---|---|---|
| `/api/cost?tenantId=X` | GET | Aggregated spend data per tenant |
| `/api/budget?tenantId=X` | GET | Current budget state and disabled tools |
| `/api/budget` | POST | Pre-flight budget check (`{ tenantId, estimatedCost, modelId }`) |
| `/api/usage?tenantId=X` | GET | Usage history for a tenant |
| `/api/cache` | GET | Cache health status |
## Environment Variables
| Variable | Description |
|---|---|
| `OPENAI_API_KEY` | OpenAI API key for LLM calls |
| `REDIS_URL` | Redis connection URL for cache backend |
| `DATABASE_URL` | PostgreSQL connection string for settings store |
| `LANGFUSE_PUBLIC_KEY` | Langfuse public key for observability |
| `LANGFUSE_SECRET_KEY` | Langfuse secret key for observability |
| `LANGFUSE_HOST` | Langfuse host URL |
| `DEFAULT_DAILY_BUDGET` | Default daily budget per tenant (USD) |
| `DEFAULT_MONTHLY_BUDGET` | Default monthly budget per tenant (USD) |
## Quick Start
1. Create a PostgreSQL database
2. Copy `.env.example` to `.env` and fill in your credentials
3. Start Redis on localhost:6379
4. Run `pnpm install`
5. Run `pnpm dev`
## Running Tests
```bash
pnpm test
```
## Project Layout
```
app/
api/
cost/route.ts Aggregated spend endpoint
budget/route.ts Budget check and state endpoints
usage/route.ts Usage history endpoint
cache/route.ts Cache health endpoint
src/
lib/
cost-types.ts Shared type definitions
db.ts Database connection
db/schema.ts Drizzle ORM schema
db/index.ts Database barrel exports
wrap-openai.ts OpenAI SDK wrapper with telemetry
budget-middleware.ts Budget enforcement via agent-budget-engine
cache-layer.ts Semantic caching with Redis
aggregation.ts Cost collection and aggregation
langfuse.ts Langfuse observability
settings-store.ts Drizzle-backed tenant settings
orchestrator.ts Unified pipeline orchestrator
tests/
lib/ Test suite mirroring src/lib/
app/api/ Integration tests for API routes
```
## License
MIT — see [LICENSE](./LICENSE).