Building AI Usage Tracking, Cost Controls, and Model Optimization for Tender Analysis

Introduction

The AI Tender Opportunity Scanner is now a fully functional SaaS platform with:

  • AI-powered tender discovery
  • Intelligent tender analysis
  • Business profile matching
  • Saved opportunities
  • Pipeline management
  • Analytics dashboards
  • Email notifications
  • Slack and Microsoft Teams integrations
  • Authentication and role-based access control
  • Secure multi-tenant architecture
  • Subscription plans
  • Billing and usage management

From a software engineering perspective, the platform is production-ready.

However, AI-powered SaaS products introduce a new operational challenge that traditional SaaS applications rarely face:

Every AI request costs money.

Unlike ordinary CRUD operations, every document processed, every summary generated, every embedding created, and every semantic search consumes computing resources that directly impact profitability.

For example:

Large Tender Document
OCR Processing
Document Parsing
Embeddings
LLM Analysis
Summary Generation
Risk Assessment
Opportunity Matching

Each stage has an associated infrastructure cost.

As customer usage grows, these costs can increase dramatically.

In this article we will build a comprehensive AI operations layer capable of:

  • Tracking AI usage
  • Measuring AI costs
  • Recording model performance
  • Selecting the most appropriate model
  • Caching AI results
  • Preventing duplicate processing
  • Managing AI budgets
  • Detecting abnormal usage
  • Monitoring profitability
  • Optimizing model selection
  • Supporting multiple AI providers
  • Building AI observability dashboards

The goal is to maximize customer value while keeping AI infrastructure costs predictable and sustainable.


Development Progress

Project Scope and Roadmap ████████████████████ 100%
MVP Definition ████████████████████ 100%
Technology Stack ████████████████████ 100%
System Architecture ████████████████████ 100%
Database Design ████████████████████ 100%
Backend Project Structure ████████████████████ 100%
SQLAlchemy Models ████████████████████ 100%
Alembic Migration ████████████████████ 100%
Repository Layer ████████████████████ 100%
CRUD Service Layer ████████████████████ 100%
Tender Ingestion Pipeline ████████████████████ 100%
AI Analysis Pipeline ████████████████████ 100%
AI Matching Engine ████████████████████ 100%
React Dashboard ████████████████████ 100%
Tender Detail Page ████████████████████ 100%
Saved Opportunities Workspace ████████████████████ 100%
Pipeline Analytics Dashboard ████████████████████ 100%
Notifications & Monitoring ████████████████████ 100%
Email Notifications ████████████████████ 100%
Slack & Teams Integrations ████████████████████ 100%
Authentication & Organizations ████████████████████ 100%
Multi-Tenant Data Isolation ████████████████████ 100%
Billing & Subscription System ████████████████████ 100%
AI Usage & Cost Optimization ████████████████████ 100%
Production Monitoring & Logging ░░░░░░░░░░░░░░░░░░░░ 0%

Why AI Cost Management Matters

Traditional SaaS costs are relatively predictable:

Database
API
Storage
Networking

AI SaaS introduces variable costs:

LLM tokens
Embeddings
OCR
Speech-to-text
Image processing
Vector search
GPU inference

If unmanaged, AI costs can quickly exceed subscription revenue.


AI Processing Pipeline

Every tender follows a processing pipeline.

Tender Document
OCR
Text Cleaning
Metadata Extraction
Embeddings
Semantic Analysis
Summary
Risk Analysis
Tender Matching
Storage

Every stage should be measurable.


AI Cost Categories

Track costs separately.

Input Tokens
Output Tokens
Embeddings
OCR Pages
Vector Searches
LLM Calls
GPU Minutes
Storage
Background Jobs

This enables accurate profitability reporting.


AI Operations Architecture

Tender
AI Orchestrator
Model Router
AI Provider
Usage Recorder
Cost Calculator
Cache
Analytics

Every AI request flows through a single orchestration layer.


AI Providers

The platform should support multiple providers.

Example:

OpenAI
Azure OpenAI
Anthropic
Google Gemini
Mistral
Local LLM
Future Provider

Business logic should remain provider-independent.


AI Provider Interface

Create:

app/ai/providers/base.py

Example:

class AIProvider:
def generate(self):
...
def embed(self):
...
def estimate_cost(self):
...
def health():
...

Each provider implements the same interface.


AI Request Model

Create:

app/models/ai_request.py

Fields:

id
organization_id
provider
model
operation
status
started_at
completed_at
duration_ms

AI Usage Model

Create:

app/models/ai_usage.py

Recommended fields:

organization_id
provider
model
operation
input_tokens
output_tokens
estimated_cost
currency
created_at

Supported Operations

Examples:

Summary
Tender Analysis
Risk Assessment
Matching
Embedding
Classification
Keyword Extraction

Each operation can use different models.


AI Cost Model

Create:

app/models/ai_cost.py

Fields:

provider
model
input_token_price
output_token_price
embedding_price
effective_date

This allows historical pricing changes.


Why Store Historical Prices?

Suppose OpenAI changes pricing.

Old reports should still calculate:

Cost
at
time
of
processing

Historical pricing prevents distorted financial reports.


Model Registry

Create:

app/models/ai_model_registry.py

Fields:

provider
model_name
supports_chat
supports_embeddings
supports_json
supports_long_context
max_context
status

AI Router

Instead of hardcoding:

GPT-4

Create:

Model Router

The router decides:

Summary
Fast model
Analysis
High quality model
Embedding
Embedding model

Model Selection Strategy

Example:

Simple Summary
Small Model
Complex Tender
Large Model
Embeddings
Embedding Model

Different workloads require different models.


AI Configuration

Create:

ai_settings.py

Example:

SUMMARY_MODEL
ANALYSIS_MODEL
MATCHING_MODEL
EMBEDDING_MODEL

Models can be changed without modifying code.


AI Cache

Repeated requests should not always call the LLM.

Architecture:

Prompt
Hash
Cache
Hit?
Return Cached Result
Miss?
Call AI

Caching dramatically reduces costs.


Cache Keys

Example:

organization
operation
document_hash
model
prompt_version

Changing prompt versions invalidates outdated cache entries.


AI Cache Model

Create:

app/models/ai_cache.py

Fields:

cache_key
provider
model
response
expires_at

Duplicate Processing Detection

Suppose the same tender arrives twice.

Instead of:

Analyze Again

Return:

Existing Analysis

This reduces unnecessary AI spending.


Prompt Versioning

Store:

Prompt v1
Prompt v2
Prompt v3

Every AI response records:

prompt_version

Future improvements remain reproducible.


AI Orchestrator

Create:

app/services/ai_orchestrator.py

Responsibilities:

  • Routing
  • Caching
  • Cost recording
  • Retry handling
  • Logging
  • Model selection
  • Provider failover

The orchestrator becomes the single entry point.


Retry Strategy

Temporary failures:

Timeout
Rate Limit
Network Error

Retry:

1 sec
5 sec
30 sec

Permanent failures:

Invalid Prompt
Authentication
Unsupported Model

No retry.


AI Budget

Organizations may have monthly budgets.

Example:

Starter
€15 AI Budget
Professional
€150
Enterprise
Unlimited

The billing system tracks subscription usage.

The AI layer tracks infrastructure spending.


AI Budget Warning

Notify at:

70%
90%
100%

Example:

AI Budget
92%
used
Upgrade Plan

Cost Per Organization

Dashboard:

Northstar
Revenue
99
AI Cost
18
Margin
81

This allows profitability analysis.


Cost Per Feature

Example:

Summary
3.12
Matching
5.80
Risk Analysis
6.40

Helps identify expensive workflows.


Cost Per Model

Example:

GPT-4
120
GPT-4 Mini
18
Embedding
6

Supports model optimization.


AI Performance Metrics

Track:

Latency
Success Rate
Cost
Tokens
Cache Hit Rate

AI Dashboard

Display:

Requests
Average Cost
Average Tokens
Top Models
Errors
Latency
Cache Hit Rate

Provider Health

Track:

Availability
Latency
Error Rate

Automatically route away from unhealthy providers.


Provider Failover

Architecture:

Primary Provider
Failure
Secondary Provider

This improves reliability.


Batch Processing

Instead of:

100
individual
summaries

Use:

Batch
Processing

Reducing overhead.


Embedding Reuse

Embeddings rarely change.

Reuse them whenever possible.

Only regenerate when:

  • Document changes
  • Embedding model changes

AI Quality Monitoring

Store:

User Rating
Reviewer Rating
Confidence
Correction Count

Helps improve prompts.


AI Feedback Loop

Workflow:

AI Output
User Correction
Review
Prompt Improvement

AI Analytics API

Create endpoints:

GET
/ai/usage
GET
/ai/costs
GET
/ai/models
GET
/ai/performance

React AI Dashboard

Create:

AI Dashboard
Model Usage
Provider Usage
Cost Trends
Latency
Cache
Budgets

Environment Variables

PRIMARY_PROVIDER
SECONDARY_PROVIDER
SUMMARY_MODEL
CACHE_ENABLED
MONTHLY_AI_BUDGET

Security

Never log:

  • Prompt secrets
  • Customer confidential information
  • API keys

Mask:

Authorization
Bearer
********

Testing

Create:

tests/
ai/
test_router.py
test_cache.py
test_cost.py
test_usage.py
test_provider.py

End-to-End Scenario

Upload Tender
Cache Miss
AI Processing
Cost Recorded
Usage Recorded
Cache Stored
Dashboard Updated

Second upload:

Upload
Cache Hit
Instant Response
Zero AI Cost

Security Checklist

✔ AI requests logged
✔ Costs tracked
✔ Usage tracked
✔ Cache enabled
✔ Duplicate detection
✔ Prompt versioning
✔ Provider failover
✔ Budgets enforced
✔ Dashboards available
✔ Cost reporting

Current Architecture

Tender Sources
AI Orchestrator
Model Router
AI Providers
Usage
Costs
Cache
Analytics
Billing

The platform now manages AI intelligently rather than simply calling language models.


Current Deliverables

✔ AI Router
✔ Provider abstraction
✔ Cost tracking
✔ Usage tracking
✔ Model registry
✔ AI cache
✔ Prompt versioning
✔ Duplicate detection
✔ Budget management
✔ Provider failover
✔ Analytics dashboards
✔ AI monitoring
✔ Cost optimization
✔ Testing strategy

Recommended Git Commit

git add .
git commit -m "Implement AI usage tracking and cost optimization"
git push origin main

What We Will Build Next

The application now measures and optimizes AI usage, but operating a production SaaS also requires complete visibility into system health and operational reliability.

The next article in the development series is:

“Building Production Monitoring, Logging, Alerting, and Observability for the Tender Opportunity Scanner SaaS.”


Conclusion

AI capabilities are the core differentiator of the Tender Opportunity Scanner, but they are also its largest variable operating expense. A dedicated AI operations layer ensures that every model invocation is measurable, attributable, and optimized for both quality and cost.

By introducing provider abstractions, intelligent model routing, request caching, prompt versioning, duplicate detection, usage tracking, historical pricing, AI budgets, and comprehensive performance monitoring, the platform gains the operational controls required to scale sustainably. These mechanisms also provide the foundation for continuous optimization as new models, providers, and pricing structures become available.

With AI usage now fully observable and cost-aware, the next stage focuses on production operations: implementing centralized logging, monitoring, alerting, tracing, and observability to keep the platform reliable as customer adoption grows.

Discover more from BidRadar

Subscribe now to keep reading and get access to the full archive.

Continue reading