LLM Cost Ops - API Server
Production-ready API server for LLM Cost Ops platform
A high-performance, enterprise-grade REST API server built with Axum, providing comprehensive endpoints for cost tracking, analytics, and reporting.
Features
- RESTful API - Clean, well-documented REST endpoints
- Authentication - JWT and API key authentication
- Authorization - Role-based access control (RBAC)
- Rate Limiting - Per-organization rate limits
- Compression - Brotli and Gzip compression
- Validation - Comprehensive input validation
- Pagination - Cursor and offset-based pagination
- OpenAPI - Auto-generated API documentation
- Health Checks - Readiness and liveness probes
- Metrics - Prometheus metrics endpoint
- Distributed Tracing - OpenTelemetry integration
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.1"
= { = "1", = ["full"] }
Basic Server
use ;
use DatabaseConfig;
async
Custom Router
use create_api_router;
use DatabaseConfig;
use Router;
async
API Endpoints
Usage Endpoints
POST /api/v1/usage - Submit usage record
GET /api/v1/usage - List usage records
GET /api/v1/usage/:id - Get usage record by ID
DELETE /api/v1/usage/:id - Delete usage record
POST /api/v1/usage/batch - Batch submit usage records
GET /api/v1/usage/stats - Get usage statistics
Cost Endpoints
GET /api/v1/costs - Query cost records
GET /api/v1/costs/:id - Get cost record by ID
GET /api/v1/costs/aggregate - Aggregate costs
GET /api/v1/costs/trends - Get cost trends
Analytics Endpoints
GET /api/v1/analytics/usage - Usage analytics
GET /api/v1/analytics/costs - Cost analytics
GET /api/v1/analytics/providers - Provider comparison
GET /api/v1/analytics/models - Model comparison
Forecasting Endpoints
POST /api/v1/forecast/cost - Cost forecast
POST /api/v1/forecast/usage - Usage forecast
GET /api/v1/forecast/anomalies - Anomaly detection
Report Endpoints
POST /api/v1/reports - Generate report
GET /api/v1/reports/:id - Get report
GET /api/v1/reports/:id/download - Download report
GET /api/v1/reports - List reports
DELETE /api/v1/reports/:id - Delete report
Administration Endpoints
GET /api/v1/admin/organizations - List organizations
POST /api/v1/admin/organizations - Create organization
GET /api/v1/admin/users - List users
POST /api/v1/admin/users - Create user
PUT /api/v1/admin/users/:id/roles - Update user roles
System Endpoints
GET /health - Health check
GET /ready - Readiness check
GET /metrics - Prometheus metrics
GET /openapi.json - OpenAPI specification
Request Examples
Submit Usage
Query Costs
Generate Report
Authentication
JWT Authentication
# Login to get JWT token
# Use token in requests
API Key Authentication
Middleware
Custom Middleware
use ;
async
let app = new
.route
.layer;
Configuration
Environment Variables
# Server
# Database
# Authentication
# Features
# Rate Limiting
Configuration File
# config.toml
[]
= "0.0.0.0"
= 8080
[]
= "postgresql://user:pass@localhost/costops"
= 10
[]
= "your-secret-key"
= 1
[]
= true
= 9090
= true
= "info"
Performance
- Throughput: 10,000+ requests/second
- Latency: <10ms p50, <50ms p99
- Concurrency: 1000+ concurrent connections
- Memory: ~50MB baseline, ~200MB under load
- Database: Connection pooling with deadpool
Deployment
Docker
FROM rust:1.91 as builder
WORKDIR /app
COPY . .
RUN cargo build --release --bin llm-cost-ops-api
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libssl3 ca-certificates
COPY --from=builder /app/target/release/llm-cost-ops-api /usr/local/bin/
CMD ["llm-cost-ops-api"]
Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: cost-ops-api
spec:
replicas: 3
selector:
matchLabels:
app: cost-ops-api
template:
metadata:
labels:
app: cost-ops-api
spec:
containers:
- name: api
image: cost-ops-api:latest
ports:
- containerPort: 8080
- containerPort: 9090
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: cost-ops-secrets
key: database-url
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: cost-ops-secrets
key: jwt-secret
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Links
- Documentation: https://docs.rs/llm-cost-ops-api
- Core Library: https://crates.io/crates/llm-cost-ops
- OpenAPI Spec: https://github.com/globalbusinessadvisors/llm-cost-ops/blob/main/openapi.json
- Repository: https://github.com/globalbusinessadvisors/llm-cost-ops