Skip to main content

Usage Model Documentation

Overview​

The Usage model tracks resource consumption across the CODITECT platform, providing detailed metering for billing, cost allocation, and resource optimization. It captures usage of AI tokens, compute resources, storage operations, and network bandwidth with granular pricing information. The model supports both real-time tracking and historical analysis.

Model Structure​

Core Fields​

FieldTypeDescriptionConstraints
usage_idUUIDUnique usage record identifierPrimary key, auto-generated
tenant_idUUIDAssociated tenantForeign key to Tenant
user_idUUIDUser who consumed resourceForeign key to User
service_typeServiceType (Enum)Category of serviceRequired
resource_typeResourceType (Enum)Specific resource consumedRequired
quantityf64Amount consumedRequired, positive value
unitStringUnit of measurementRequired (e.g., "tokens", "requests")
cost_per_unitf64Unit price in dollarsRequired
total_costf64Calculated total costAuto-calculated (quantity × cost_per_unit)
timestampDateTimeWhen usage occurredAuto-set to current time
metadataJSON (Optional)Additional contextFlexible structure

ServiceType Enum​

enum ServiceType {
Ai, // AI model usage
Compute, // Cloud Run, containers
Storage, // GCS, FDB storage
Network, // Bandwidth usage
Database // FDB operations
}

ResourceType Enum​

enum ResourceType {
// AI Resources - Separated by input/output for accurate pricing
ClaudeInputTokens,
ClaudeOutputTokens,
GeminiInputTokens,
GeminiOutputTokens,
OpenAiInputTokens,
OpenAiOutputTokens,
OllamaInputTokens,
OllamaOutputTokens,
HuggingFaceInputTokens,
HuggingFaceOutputTokens,

// Legacy token types (deprecated)
ClaudeTokens,
GeminiTokens,
OpenAiTokens,
OllamaTokens,

// Compute Resources
CloudRunRequests,
CloudRunCpu,
CloudRunMemory,

// Storage Resources
FdbWrites,
FdbReads,
GcsStorage
}

Usage Examples​

AI Token Usage​

{
"usage_id": "550e8400-e29b-41d4-a716-446655440000",
"tenant_id": "123e4567-e89b-12d3-a456-426614174000",
"user_id": "456e7890-e89b-12d3-a456-426614174000",
"service_type": "Ai",
"resource_type": "ClaudeInputTokens",
"quantity": 1500.0,
"unit": "tokens",
"cost_per_unit": 0.000003,
"total_cost": 0.0045,
"timestamp": "2025-08-29T10:30:00Z",
"metadata": {
"model": "claude-3-opus",
"agent_type": "coder",
"task_id": "task-789",
"project_id": "project-123",
"prompt_type": "code_generation"
}
}

Compute Usage​

{
"usage_id": "660e8400-e29b-41d4-a716-446655440000",
"tenant_id": "123e4567-e89b-12d3-a456-426614174000",
"user_id": "456e7890-e89b-12d3-a456-426614174000",
"service_type": "Compute",
"resource_type": "CloudRunCpu",
"quantity": 0.25,
"unit": "vCPU-hours",
"cost_per_unit": 0.024,
"total_cost": 0.006,
"timestamp": "2025-08-29T11:00:00Z",
"metadata": {
"service_name": "coditect-api",
"region": "us-central1",
"instance_class": "standard",
"container_id": "container-abc123"
}
}

Storage Operations​

{
"usage_id": "770e8400-e29b-41d4-a716-446655440000",
"tenant_id": "123e4567-e89b-12d3-a456-426614174000",
"user_id": "456e7890-e89b-12d3-a456-426614174000",
"service_type": "Database",
"resource_type": "FdbWrites",
"quantity": 10000.0,
"unit": "operations",
"cost_per_unit": 0.0000001,
"total_cost": 0.001,
"timestamp": "2025-08-29T11:30:00Z",
"metadata": {
"operation_type": "batch_insert",
"entity_type": "tasks",
"batch_size": 100,
"latency_ms": 45
}
}

Pricing Structure​

AI Model Pricing (per 1K tokens)​

ModelInput TokensOutput TokensNotes
Claude 3 Opus$0.015$0.075Premium model
Claude 3 Sonnet$0.003$0.015Balanced model
Gemini Pro$0.0005$0.0015Cost-effective
GPT-4$0.03$0.06OpenAI flagship
Ollama$0.00$0.00Self-hosted

Compute Pricing​

ResourceUnitPriceNotes
Cloud Run Requestsper million$0.40First 2M free
Cloud Run CPUper vCPU-hour$0.024Billed per 100ms
Cloud Run Memoryper GB-hour$0.0025Billed per 100ms

Storage Pricing​

ResourceUnitPriceNotes
FDB Writesper million ops$0.10Transactional
FDB Readsper million ops$0.01Eventually consistent
GCS Storageper GB-month$0.02Standard tier

Database Schema​

Primary Storage Pattern​

Key: /{tenant_id}/usage/{YYYY-MM-DD}/{user_id}/{timestamp}:{usage_id}
Value: JSON serialized Usage object

Secondary Indexes​

# Usage by service type
/{tenant_id}/usage_by_service/{service_type}/{YYYY-MM-DD} -> [usage_ids]

# Usage by resource type
/{tenant_id}/usage_by_resource/{resource_type}/{YYYY-MM-DD} -> [usage_ids]

# User daily usage
/{tenant_id}/user_usage/{user_id}/{YYYY-MM-DD} -> [usage_ids]

# Cost aggregation
/{tenant_id}/usage_costs/{YYYY-MM-DD}/{user_id} -> total_cost

Aggregation Patterns​

# Hourly aggregates
/{tenant_id}/usage_hourly/{YYYY-MM-DD-HH}/{service_type} -> aggregated_data

# Daily summaries
/{tenant_id}/usage_daily/{YYYY-MM-DD}/{user_id} -> daily_summary

# Monthly billing
/{tenant_id}/usage_monthly/{YYYY-MM}/{user_id} -> monthly_total

Source Files​

  • Model: /src/models/usage.rs
  • Repository: /src/db/repositories/usage_repository.rs
  • Service: /src/services/usage_tracking_service.rs
  • Pricing: /src/services/pricing_service.rs
  • API Handler: /src/api/handlers/usage_handlers.rs
  • Tests: /src/models/tests/usage_tests.rs

Key Methods​

impl Usage {
fn new(
tenant_id: Uuid,
user_id: Uuid,
service_type: ServiceType,
resource_type: ResourceType,
quantity: f64,
unit: String,
cost_per_unit: f64
) -> Self

fn with_metadata(self, metadata: serde_json::Value) -> Self
}

impl UsageTrackingService {
async fn track_usage(&self, usage: Usage) -> Result<()>
async fn get_user_usage(&self, user_id: Uuid, start: DateTime<Utc>, end: DateTime<Utc>) -> Result<Vec<Usage>>
async fn calculate_costs(&self, tenant_id: Uuid, period: Period) -> Result<CostSummary>
}

API Endpoints​

Usage Tracking​

  • POST /api/usage - Record usage (internal)
  • GET /api/usage?start={date}&end={date} - Get usage history
  • GET /api/usage/summary - Get usage summary
  • GET /api/usage/costs - Get cost breakdown

Reports​

  • GET /api/reports/usage/daily - Daily usage report
  • GET /api/reports/usage/monthly - Monthly billing report
  • GET /api/reports/usage/by-service - Service breakdown
  • GET /api/reports/usage/trends - Usage trends analysis

Administrative​

  • GET /api/admin/usage/tenant/{tenant_id} - Tenant usage
  • POST /api/admin/usage/recalculate - Recalculate costs
  • GET /api/admin/usage/alerts - Usage anomalies

Metadata Patterns​

AI Usage Metadata​

{
"model": "claude-3-opus",
"model_version": "20240229",
"agent_type": "test_writer",
"agent_id": "agent-123",
"task_id": "task-456",
"project_id": "project-789",
"prompt_tokens": 500,
"completion_tokens": 1000,
"temperature": 0.7,
"execution_time_ms": 2500
}

Compute Metadata​

{
"service_name": "coditect-api",
"instance_id": "instance-abc123",
"container_id": "container-def456",
"region": "us-central1",
"zone": "us-central1-a",
"machine_type": "n1-standard-1",
"scaling_type": "automatic",
"request_count": 150
}

Storage Metadata​

{
"operation_type": "batch_write",
"entity_type": "users",
"transaction_id": "tx-123456",
"consistency_level": "strong",
"replication_factor": 3,
"compression_ratio": 0.65,
"encryption": "aes-256"
}

Aggregation Strategies​

Real-time Tracking​

  1. Write usage records immediately
  2. Update running totals in memory
  3. Flush to persistent storage periodically
  4. Alert on threshold breaches

Batch Aggregation​

// Hourly aggregation job
async fn aggregate_hourly_usage(hour: DateTime<Utc>) {
let usage_records = fetch_usage_for_hour(hour).await?;

let aggregates = usage_records
.group_by(|u| (u.service_type, u.resource_type))
.map(|group| calculate_aggregate(group))
.collect();

store_hourly_aggregates(hour, aggregates).await?;
}

Cost Optimization​

  1. Identify high-usage patterns
  2. Suggest more efficient resources
  3. Alert on unusual spikes
  4. Provide cost forecasts

Billing Integration​

Monthly Billing Cycle​

Day 1: Start new billing period
Daily: Track and aggregate usage
Day 25: Send usage forecast
Day 30: Finalize usage, generate invoice
Day 31: Process payment

Invoice Generation​

struct Invoice {
tenant_id: Uuid,
billing_period: Period,
line_items: Vec<LineItem>,
subtotal: f64,
tax: f64,
total: f64,
due_date: DateTime<Utc>
}

struct LineItem {
service_type: ServiceType,
resource_type: ResourceType,
quantity: f64,
unit_price: f64,
total: f64
}

Monitoring & Alerts​

Usage Alerts​

  • Threshold exceeded (80%, 100% of quota)
  • Unusual spike detection (3x normal)
  • New resource type usage
  • Cost projection warnings

Metrics​

  • Usage by service type
  • Cost trends over time
  • Per-user consumption
  • Resource efficiency scores
  • Waste identification

Compliance & Audit​

Data Retention​

  • Raw usage: 90 days
  • Daily aggregates: 1 year
  • Monthly summaries: 7 years
  • Audit logs: 7 years

Audit Trail​

  • Who consumed what resource
  • When consumption occurred
  • Cost at time of usage
  • Any manual adjustments

Performance Optimization​

Write Optimization​

  • Batch usage writes
  • Async processing pipeline
  • Compress old records
  • Archive to cold storage

Query Optimization​

  • Pre-aggregated views
  • Time-series indexing
  • Materialized summaries
  • Caching layer

Future Enhancements​

Advanced Features​

  1. Predictive Analytics: ML-based usage forecasting
  2. Cost Allocation: Tag-based cost distribution
  3. Budget Alerts: Proactive budget management
  4. Reserved Capacity: Commitment-based discounts

Integration Improvements​

  1. Cloud Provider APIs: Direct usage import
  2. FinOps Tools: Cost management platforms
  3. Accounting Systems: Automated invoice sync
  4. Analytics Platforms: Usage data export

Optimization Features​

  1. Smart Scheduling: Run tasks during off-peak
  2. Resource Recommendations: Right-sizing suggestions
  3. Waste Detection: Identify unused resources
  4. Cost Anomaly Detection: ML-based alerting

Last Updated: 2025-08-29 Version: 1.0