# Trials and custom deals without a plan per customer

> Give one customer a feature their plan doesn't have, trial a higher plan's feature with an expiry, raise limits with overrides, pause a feature, or write a deal once as a rule for every customer that signs it.

# Trials and custom deals

Sales closes a deal: Acme stays on Starter, but gets SSO for a two-week trial and a much bigger API allowance. You
don't need a new plan for that. Change what one customer has with **gives**, **takes**, and **overrides**:

| Tool | Changes | Ex. |
|---|---|---|
| **Give** | Whether a customer has an entitlement, using a plan's definition of it. | SSO on Starter. Business's API limit for one org. |
| **Take** | Denies an entitlement the plan has, keeping its usage. | Pause exports while an invoice is unpaid. |
| **Override** | How much: one customer's limit, mode, or reset. | 250,000 requests instead of 100,000. |

<Tabs groupId="lang" queryString>
<TabItem value="typescript" label="TypeScript">

```ts

const limitr = await Limitr.new(`
policy: {
    credits: {
        request: { label: 'API request' }
        export: { label: 'Export' }
    }
    plans: {
        starter: {
            default: true
            entitlements: {
                api: {
                    limit: {
                        credit: 'request', value: 1_000
                        resets: true, reset_sch: 'monthly:1'
                    }
                }
                exports: { limit: { credit: 'export', value: 10, resets: true } }
            }
        }
        business: {
            entitlements: {
                api: {
                    limit: {
                        credit: 'request', value: 100_000
                        resets: true, reset_sch: 'monthly:1'
                    }
                }
                exports: { limit: { credit: 'export', value: 500, resets: true } }
                sso: {}
                audit_log: {}
            }
        }
    }
}`);

const DAY = 86_400_000;
await limitr.ensureCustomer('acme', 'starter', 'org', 'Acme');
await limitr.ensureCustomer('ada', '', 'user', 'Ada', ['acme']);
// Acme's features and API limit.
async function report(label: string) {
    const features = await limitr.features('acme');
    const apiLimit = await limitr.limit('acme', 'api');
    console.log(`${label}:`, features, 'api limit', apiLimit);
}

await report('starter');

// 1. A trial of Business's SSO for 14 days, without changing Acme's plan.
await limitr.giveEntitlement('acme', 'sso', undefined, Date.now() + 14 * DAY);

// 2. A negotiated API allowance: Business's API limit for Acme only.
await limitr.giveEntitlement('acme', 'api', 'business');
await report('after the deal');
console.log('ada, on acme\'s plan:', await limitr.features('ada'));

// 3. Then a custom number on top: 250k requests, for this customer only.
await limitr.createCustomerOverride('acme', 'api', 250_000);
await report('with an override');

// 4. Pause exports while an invoice is unpaid. Usage so far is kept.
await limitr.takeEntitlement('acme', 'exports');
await report('exports paused');
await limitr.restoreEntitlement('acme', 'exports');

// 5. Gives and takes are stored on the customer, so they survive a plan change.
await limitr.setCustomerPlan('acme', 'business', false);
await limitr.setCustomerPlan('acme', 'starter', false);
await report('after an upgrade and a downgrade');
console.log('given to acme:', Object.keys((await limitr.customer('acme'))?.given ?? {}));
```

It prints:

```text
starter: [ 'api', 'exports' ] api limit 1000
after the deal: [ 'api', 'exports', 'sso' ] api limit 100000
ada, on acme's plan: [ 'api', 'exports', 'sso' ]
with an override: [ 'api', 'exports', 'sso' ] api limit 250000
exports paused: [ 'api', 'sso' ] api limit 250000
after an upgrade and a downgrade: [ 'api', 'exports', 'sso' ] api limit 250000
given to acme: [ 'sso', 'api' ]
```

</TabItem>
<TabItem value="python" label="Python">

```python
from datetime import datetime, timedelta
from limitr import Limitr

limitr = Limitr("""
policy: {
    credits: {
        request: { label: 'API request' }
        export: { label: 'Export' }
    }
    plans: {
        starter: {
            default: true
            entitlements: {
                api: {
                    limit: {
                        credit: 'request', value: 1_000
                        resets: true, reset_sch: 'monthly:1'
                    }
                }
                exports: { limit: { credit: 'export', value: 10, resets: true } }
            }
        }
        business: {
            entitlements: {
                api: {
                    limit: {
                        credit: 'request', value: 100_000
                        resets: true, reset_sch: 'monthly:1'
                    }
                }
                exports: { limit: { credit: 'export', value: 500, resets: true } }
                sso: {}
                audit_log: {}
            }
        }
    }
}""")

limitr.ensure_customer('acme', 'starter', 'org', 'Acme')
limitr.ensure_customer('ada', '', 'user', 'Ada', ['acme'])
# Acme's features and API limit.
def report(label):
    print(f"{label}:", limitr.features('acme'), 'api limit', f"{limitr.limit('acme', 'api'):g}")

report('starter')

# 1. A trial of Business's SSO for 14 days, without changing Acme's plan.
limitr.give_entitlement('acme', 'sso', expires_on=datetime.now() + timedelta(days=14))

# 2. A negotiated API allowance: Business's API limit for Acme only.
limitr.give_entitlement('acme', 'api', 'business')
report('after the deal')
print("ada, on acme's plan:", limitr.features('ada'))

# 3. Then a custom number on top: 250k requests, for this customer only.
limitr.create_customer_override('acme', 'api', 250_000)
report('with an override')

# 4. Pause exports while an invoice is unpaid. Usage so far is kept.
limitr.take_entitlement('acme', 'exports')
report('exports paused')
limitr.restore_entitlement('acme', 'exports')

# 5. Gives and takes are stored on the customer, so they survive a plan change.
limitr.set_customer_plan('acme', 'business', False)
limitr.set_customer_plan('acme', 'starter', False)
report('after an upgrade and a downgrade')
print('given to acme:', list((limitr.customer('acme') or {}).get('given', {})))
```

It prints:

```text
starter: ['api', 'exports'] api limit 1000
after the deal: ['api', 'exports', 'sso'] api limit 100000
ada, on acme's plan: ['api', 'exports', 'sso']
with an override: ['api', 'exports', 'sso'] api limit 250000
exports paused: ['api', 'sso'] api limit 250000
after an upgrade and a downgrade: ['api', 'exports', 'sso'] api limit 250000
given to acme: ['sso', 'api']
```

</TabItem>
</Tabs>

## What happened

1. **A trial.** `giveEntitlement('acme', 'sso', undefined, expiresOn)` gave SSO from the closest plan that has it
   (Business), for 14 days. When it expires, SSO is gone again with no cleanup.
2. **A negotiated limit.** `giveEntitlement('acme', 'api', 'business')` uses Business's definition of `api`, limit
   included. Nothing can be invented: a give always comes from a plan that defines the entitlement.
3. **A custom number.** `createCustomerOverride('acme', 'api', 250_000)` changes the limit for Acme only, on top of
   the give.
4. **A pause.** `takeEntitlement` denies exports even though the plan has them. The meter and any override stay, so
   `restoreEntitlement` picks up where it left off.
5. **Plan changes.** Gives and takes are stored on the customer (`given`, `taken`), so they last through upgrades
   and downgrades. To keep a feature through a downgrade, give it before changing the plan.

## Orgs and their users

Ada has no plan of her own, so she's on Acme's, and she gets Acme's gives and takes with it. A give or take on Ada
herself wins over Acme's.

**Overrides belong to the customer they're set on.** Acme's override changes Acme's limit, not Ada's. For a limit
shared by the org, meter the entitlement on the org (`scope: 'org'`), and the org's override applies to every call.

## Trials

| Trial | How |
|---|---|
| A higher plan's feature | `giveEntitlement(id, 'sso', undefined, Date.now() + 14 * DAY)` |
| More usage for a while | `createCustomerOverride(id, 'chat', 1_000_000, Date.now() + 14 * DAY)` |
| A whole plan | `setCustomerPlan(id, 'business', false)` now, and back later; or a plan fee with a `trial_period` ([Plan fees](../reference/plans#plan-fees)) |

## One deal for many customers

Gives, takes, and overrides change one customer. When the same deal covers several (every org on the 2027 enterprise
terms, a startup program), write it once as a **rule** in `policy.rules` and give the customers the rule. A rule holds
the same changes: gives, takes, limit overrides, and caps. It's part of the policy, so it's versioned and reviewed with
it. Rules need engine 0.8.

<Tabs groupId="lang" queryString>
<TabItem value="typescript" label="TypeScript">

```ts

const limitr = await Limitr.new(`
policy: {
    credits: {
        request: { label: 'API request' }
    }
    plans: {
        starter: {
            default: true
            entitlements: {
                api: { limit: { credit: 'request', value: 1_000, resets: true, reset_sch: 'monthly:1' } }
                exports: { limit: { credit: 'request', value: 10, resets: true } }
            }
        }
        business: {
            entitlements: {
                api: { limit: { credit: 'request', value: 100_000, resets: true, reset_sch: 'monthly:1' } }
                sso: {}
                audit_log: {}
            }
        }
    }
    rules: {
        acme_2027: {
            label: 'Acme 2027'
            given: { sso: {}, audit_log: { plan: 'business' } }
            taken: { exports: {} }
            overrides: { api: { credit: 'request', value: 250_000, resets: true, reset_sch: 'monthly:1' } }
        }
        launch_promo: {
            priority: 1
            overrides: { api: { credit: 'request', value: 500_000, resets: true, reset_sch: 'monthly:1' } }
        }
    }
}`);

await limitr.ensureCustomer('acme', 'starter', 'org', 'Acme');
await limitr.ensureCustomer('globex', 'starter', 'org', 'Globex');
await limitr.ensureCustomer('ada', '', 'user', 'Ada', ['acme']);

// One deal, written once, for every customer that signs it.
await limitr.setCustomerRules('acme', ['acme_2027']);
await limitr.setCustomerRules('globex', ['acme_2027']);
console.log('acme:', await limitr.features('acme'), 'api limit', await limitr.limit('acme', 'api'));
console.log('ada, on acme\'s plan:', await limitr.features('ada'));
console.log('where acme\'s api comes from:', await limitr.entitlementSource('acme', 'api'));

// A higher priority rule wins where two set the same thing.
await limitr.setCustomerRules('acme', ['acme_2027', 'launch_promo']);
console.log('with the promo:', await limitr.limit('acme', 'api'));

// The customer's own change still comes first.
await limitr.createCustomerOverride('acme', 'api', 750_000);
console.log('with their own override:', await limitr.limit('acme', 'api'), await limitr.entitlementSource('acme', 'api'));

// The deal ends: back to the plan.
await limitr.setCustomerRules('globex', []);
console.log('globex after:', await limitr.features('globex'), 'api limit', await limitr.limit('globex', 'api'));
```

It prints:

```text
acme: [ 'api', 'sso', 'audit_log' ] api limit 250000
ada, on acme's plan: [ 'api', 'sso', 'audit_log' ]
where acme's api comes from: { access: 'plan', limit: 'rule:acme_2027', customer: 'acme' }
with the promo: 500000
with their own override: 750000 { access: 'plan', limit: 'override', customer: 'acme' }
globex after: [ 'api', 'exports' ] api limit 1000
```

</TabItem>
<TabItem value="python" label="Python">

```python
from limitr import Limitr

limitr = Limitr("""
policy: {
    credits: {
        request: { label: 'API request' }
    }
    plans: {
        starter: {
            default: true
            entitlements: {
                api: { limit: { credit: 'request', value: 1_000, resets: true, reset_sch: 'monthly:1' } }
                exports: { limit: { credit: 'request', value: 10, resets: true } }
            }
        }
        business: {
            entitlements: {
                api: { limit: { credit: 'request', value: 100_000, resets: true, reset_sch: 'monthly:1' } }
                sso: {}
                audit_log: {}
            }
        }
    }
    rules: {
        acme_2027: {
            label: 'Acme 2027'
            given: { sso: {}, audit_log: { plan: 'business' } }
            taken: { exports: {} }
            overrides: { api: { credit: 'request', value: 250_000, resets: true, reset_sch: 'monthly:1' } }
        }
        launch_promo: {
            priority: 1
            overrides: { api: { credit: 'request', value: 500_000, resets: true, reset_sch: 'monthly:1' } }
        }
    }
}""")

limitr.ensure_customer('acme', 'starter', 'org', 'Acme')
limitr.ensure_customer('globex', 'starter', 'org', 'Globex')
limitr.ensure_customer('ada', '', 'user', 'Ada', ['acme'])

def api_limit(id):
    return f"{limitr.limit(id, 'api'):g}"

# One deal, written once, for every customer that signs it.
limitr.set_customer_rules('acme', ['acme_2027'])
limitr.set_customer_rules('globex', ['acme_2027'])
print('acme:', limitr.features('acme'), 'api limit', api_limit('acme'))
print("ada, on acme's plan:", limitr.features('ada'))
print("where acme's api comes from:", limitr.entitlement_source('acme', 'api'))

# A higher priority rule wins where two set the same thing.
limitr.set_customer_rules('acme', ['acme_2027', 'launch_promo'])
print('with the promo:', api_limit('acme'))

# The customer's own change still comes first.
limitr.create_customer_override('acme', 'api', 750_000)
print('with their own override:', api_limit('acme'), limitr.entitlement_source('acme', 'api'))

# The deal ends: back to the plan.
limitr.set_customer_rules('globex', [])
print('globex after:', limitr.features('globex'), 'api limit', api_limit('globex'))
```

It prints:

```text
acme: ['api', 'sso', 'audit_log'] api limit 250000
ada, on acme's plan: ['api', 'sso', 'audit_log']
where acme's api comes from: {'access': 'plan', 'limit': 'rule:acme_2027', 'customer': 'acme'}
with the promo: 500000
with their own override: 750000 {'access': 'plan', 'limit': 'override', 'customer': 'acme'}
globex after: ['api', 'exports'] api limit 1000
```

</TabItem>
</Tabs>

- **The order is always** the customer's own change, then its rules (highest `priority` first, ties in the order the
  policy writes them), then its plan.
- **Rules have no dates.** Whoever gives a customer a rule decides when: your own deal records, or
  [contracts in Limitr Cloud](/guides/cloud/contracts), which give and take rules on their start and end dates.
- **Users get a rule's gives and takes** through their org, like the org's own. A rule's limit is the meter's, like an
  override: Ada's own API limit is still the plan's. To change each user's limit, give the users the rule too.
- **Rule caps** keep usage per customer, and go when the customer no longer has the rule.
- `entitlementSource(id, entitlement)` says where access and the limit come from, for showing a customer why they
  have what they have.

## Keeping records

Each change sends an event: `customer-entitlement-given`, `-taken`, `-restored`, `customer-rules-set`, plus
`customer-set`. Store them
and you have an audit trail of every deal. Gives, takes, overrides, and rules are part of the customer record, so
[saving customers](./save-customer-state) saves them.

See [Customers](../reference/customers#gives-and-takes) for every option.
