Skip to main content

Chapter 3: Building LLM Platforms

Platform Architecture​

Cost Allocation​

class CostAllocator:
def __init__(self, pricing: dict):
self.pricing = pricing
self.usage = {}

def track_usage(self, team_id: str, model: str, tokens: int):
"""Track usage by team."""
if team_id not in self.usage:
self.usage[team_id] = {}
if model not in self.usage[team_id]:
self.usage[team_id][model] = 0

self.usage[team_id][model] += tokens

def calculate_cost(self, team_id: str) -> dict:
"""Calculate cost for a team."""
team_usage = self.usage.get(team_id, {})
costs = {}
total = 0

for model, tokens in team_usage.items():
cost = (tokens / 1000) * self.pricing[model]
costs[model] = cost
total += cost

return {"breakdown": costs, "total": total}

def generate_report(self) -> dict:
"""Generate monthly cost report."""
report = {}
for team_id in self.usage:
report[team_id] = self.calculate_cost(team_id)
return report

Governance Framework​

AspectPolicyImplementation
Access ControlRole-based accessIAM, API keys
Usage LimitsQuotas per teamRate limiting, budgets
Model AccessApproved models onlyModel allowlist
Data HandlingPII restrictionsInput/output filtering
AuditAll interactions loggedCentralized logging

Next: Module 14 Quiz