Skip to main content
This guide shows how to create a complete billing flow programmatically: define pricing, create a plan, embed checkout, and route billable traffic per customer.
Prerequisites: Start with Authenticate an Agent, then follow Route Traffic for the gateway path you chose.

MCP flow

The Lava MCP server handles gateway traffic routing. Billing setup (meters, plans, checkout) requires the SDK. A typical billing workflow:
  1. SDK: Create a meter, plan, and checkout session (Steps 1–3 below)
  2. MCP: login to authenticate the gateway session
  3. MCP: prompt or call to route billable traffic through the gateway

SDK flow

Overview

The billing flow has four parts:
  1. Meter - Defines how usage is priced (e.g., $3 per million tokens)
  2. Plan - A subscription that includes an optional monthly fee, credit allowance, and linked meters
  3. Checkout - An embedded flow where your customer subscribes and pays
  4. Forward tokens - Per-customer auth tokens that route traffic through the gateway with billing

Step 1: Create a Meter

A meter defines the pricing rules for a type of usage. For example, charging per token for AI model calls:

Meter options

You can also add volume-based tiers for graduated pricing:

Step 2: Create a Plan

A plan ties together an optional recurring price, included credits, and one or more meters (product features included in your plan):
The meter_ids field links meters to the plan. You can link multiple meters to a single plan.

Plan options

Example with credit bundles:

Step 3: Embed Checkout

Checkout is a two-step flow: your backend creates a session, your frontend opens it with the @lavapayments/checkout SDK.

Backend: Create a session

Frontend: Open checkout

The checkout overlay handles phone verification, payment method collection, and subscription creation. When complete, onSuccess fires with the customerId.
Checkout sessions expire after 60 minutes. Create a new session for each checkout attempt.
The checkout_session_token is an opaque token — do not try to construct URLs from it. Always use the SDK to open checkout.

Step 4: Route Billable Traffic

Once a customer has completed checkout, generate forward tokens scoped to their customer ID and meter. Every request made with this token is tracked and billed against their subscription:

Step 5: Monitor Customer Usage

Track usage and costs per customer:

What’s Next?

Meters

Advanced meter configuration and pricing strategies

Checkout Guide

Checkout modes, completion handling, and webhooks

Webhooks

Get notified when customers subscribe, run low on balance, or cancel

SDK Reference

Full API for customers, subscriptions, and usage