Lago is an open-source billing and metering API designed for SaaS companies that need to move beyond rigid subscription models and implement flexible, usage-based pricing. It solves the pain point of fragmented, opaque billing systems by providing a unified platform for tracking usage events, calculating charges, generating invoices, and managing payments—all through a programmable API and visual interface. Built for engineering and product teams, Lago eliminates reliance on proprietary billing vendors like Chargebee or Stripe Billing while offering enterprise-grade features.
Technically, Lago is built in Go with a microservices architecture, using ClickHouse for event storage and analytics, and integrates with payment gateways, CRMs, and accounting systems via webhooks and APIs. It supports self-hosted deployment via Docker Compose, ensuring data never leaves your infrastructure, and offers client libraries for Go, Python, Ruby, and JavaScript. The platform is AGPLv3-licensed and backed by Y Combinator, making it ideal for companies prioritizing transparency, compliance, and control over their billing stack.
What You Get
- Usage metering - Tracks real-time usage events via API ingestion, supporting granular metrics like API calls, data processed, or compute hours, with time-windowed aggregation for accurate billing.
- Hybrid price plans - Supports combined subscription and usage-based pricing models, allowing fixed monthly fees plus variable charges based on consumption, ideal for AI, IoT, and infrastructure SaaS.
- Invoicing automation - Automatically generates and sends PDF invoices based on usage data and plan configurations, with support for multi-currency and tax calculations.
- Prepaid credits - Enables pay-as-you-go models with customer credit balances, allowing users to purchase credits upfront and deduct them as usage occurs, improving cash flow predictability.
- Add-ons and one-time charges - Allows on-demand, non-recurring charges to be applied mid-billing-cycle (e.g., for extra storage or support tiers), with immediate invoice generation.
- Coupon and discount management - Creates redeemable discounts for marketing campaigns, with usage limits, expiration dates, and automatic application during checkout.
Common Use Cases
- Running AI API billing - A startup like Mistral AI uses Lago to track API call volumes per customer and bill based on tokens processed, combining subscription tiers with usage overages.
- Managing IoT device billing - 1NCE automates global billing for millions of IoT devices by metering data usage per SIM and applying region-specific pricing rules via Lago’s API.
- Scaling SaaS with hybrid pricing - A B2B SaaS company offers both monthly subscriptions and usage-based pricing for premium features, using Lago to unify billing logic across customer segments.
- Building a white-label billing system - A platform provider embeds Lago’s open-source billing engine into their product to offer customers transparent, customizable invoicing without vendor lock-in.
Under The Hood
Architecture
- Monolithic Rails application with business logic tightly coupled in the app directory, lacking service or repository layers despite ActiveRecord usage
- Direct global access to Redis, PostgreSQL, and S3 via environment variables, with no dependency injection or inversion of control
- HTTP endpoints and webhook processing embedded in controllers and Sidekiq workers without abstraction layers or event bus patterns
- Frontend and backend deployed separately but share no modular contracts; GraphQL endpoints auto-generated from models without schema-first design
- Infrastructure defined via docker-compose with YAML reuse but no bounded contexts or microservice boundaries
- ClickHouse and Redis hardcoded into the application without adapter patterns or pluggable data source interfaces
Tech Stack
- Hybrid architecture combining Go-based services with legacy Ruby on Rails components, deployed via custom Docker images
- PostgreSQL 15 with partitioning extensions and multi-database configurations managed through custom init scripts
- Redis 7 used for caching, sessions, and real-time communication with dedicated instances and password authentication
- Traefik v3 as reverse proxy handling TLS, routing, and dynamic endpoint configuration for API, frontend, and webhooks
- Frontend built with React and Node.js, served via Docker with live volume mounting for development
- Infrastructure-as-code using docker-compose with persistent volumes for critical data stores including ClickHouse and Redpanda
Code Quality
- Extensive test suite using table-driven patterns and SQL mocking to validate database behavior under diverse conditions
- Consistent use of a Result pattern for typed success/failure states, enabling predictable error handling without panics
- Clear separation of concerns with well-defined service, model, and test layers, cleanly abstracted for unit testing
- Strong type safety applied to domain entities, JSONB fields, and optional values with utility helpers for robustness
- Comprehensive structured logging via slog with custom handlers that enforce filtering and observability
- Descriptive, domain-aligned naming across codebase that enhances readability and onboarding
What Makes It Unique
- Adaptive in-memory cache with dynamic eviction policies tuned to query patterns and system load
- Native ClickHouse integration that auto-generates materialized views based on data access hotspots
- Built-in HTTP request fingerprinting and auto-throttling to detect and mitigate abusive clients
- Semantic cache invalidation that responds to resource dependencies rather than fixed timestamps
- Unified target resolver that dynamically routes queries across data sources based on schema and latency