> ## Documentation Index
> Fetch the complete documentation index at: https://docs.integrateledger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Entities

> Give each user, seat, agent or workspace inside a customer its own credit allowance, a cap on the shared credits, or a plan of its own, all on the customer's one invoice.

Entities give every user, seat, agent or workspace inside a customer its own budget, so one teammate or runaway agent never drains the whole account's credits. [Create an entity](https://docs.integrateledger.com/api-reference/entities/upsert.md) for each with either or both of:

* **An allowance** (`allowanceCredits`): credits per billing period that only this entity can spend, used before the shared pool. Granted for the customer's plan period while the customer has a plan.
* **A cap** (`capCredits`): the most it may draw from the shared pool (shared credits and overage) per billing period.

Each entity can also hold [a plan of its own](#entity-plans), such as Workspace Pro per workspace, billed on the customer's invoice.

```typescript TypeScript theme={"system"}
await billing.upsertEntity({
	customerId: "acme",
	entityId: "alice",
	allowanceCredits: 500,
	capCredits: 2000,
});
```

```python Python theme={"system"}
billing.upsert_entity(
    customer_id="acme",
    entity_id="alice",
    allowance_credits=500,
    cap_credits=2000,
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/entities \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "acme", "entityId": "alice", "allowanceCredits": 500, "capCredits": 2000}'
```

Calling it again updates the entity: fields you leave out keep their values, and `null` clears one.

Then pass `entityId` to [check](https://docs.integrateledger.com/customers/check-and-track.md), track and [reservations](https://docs.integrateledger.com/customers/reservations.md) to spend as the entity:

```typescript TypeScript theme={"system"}
await billing.trackUsage({
	customerId: "acme",
	featureId: "image_generation",
	requestId: "run_123",
	entityId: "alice",
});
```

```python Python theme={"system"}
billing.track_usage(
    customer_id="acme",
    feature_id="image_generation",
    request_id="run_123",
    entity_id="alice",
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/track \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "acme", "featureId": "image_generation", "request_id": "run_123", "entityId": "alice"}'
```

## How entities spend

* **Order:** the entity's own credits first (its allowance and its [plans'](#entity-plans) own grants), then the shared pool in [spend order](https://docs.integrateledger.com/concepts/balances.md#spend-order-for-credits) and overage, with the entity's cap limiting everything after the allowance.
* **Caps:** the customer's own overage cap still applies; whichever cap is tighter refuses first. A request past the entity's cap returns `409 entity_cap_reached` with `capRemaining`, and check returns `allowed: false` with that reason.
* **Balances:** with `entityId`, `availableCredits` is what that entity can still spend. [Get an entity](https://docs.integrateledger.com/api-reference/entities/get.md) shows its allowance left, the shared credits it drew this period and what its cap still allows.
* **Shared pool:** a request without `entityId` spends the shared pool and never an entity's own credits.
* **Periods:** allowances and caps reset with the customer's billing period, and monthly inside an [annual plan](https://docs.integrateledger.com/pricing/annual.md). An upgrade starts a new period, and the entity's allowance and cap start again with it; the old period's allowance ends then.
* **Deleted entities:** a reservation made for an entity that is deleted before the work ends still settles, from the shared pool, for at most the credits it held. Settling for more returns `404 entity_not_found`.
* **Other feature types:** on a metered, held or boolean feature, the customer's plans and the entity's own [plans](#entity-plans) decide it together. An entity spends its own metered grants first, then the customer's. Without entity plans, `entityId` labels the use in the usage log and the customer's allowance, limit or flag decides it.

To bill per seat, model each seat as a held feature with a [seat price](https://docs.integrateledger.com/pricing/per-seat.md). To give each workspace its own plan, attach one to the entity.

## Entity plans

An entity can hold plans of its own: each workspace on Workspace Pro, billed on the customer's one invoice. Attach the plan with `entityId` and an `Idempotency-Key`:

```typescript TypeScript theme={"system"}
const change = await billing.attach(
	{ customerId: "acme", planId: "workspace_pro", entityId: "ws-1" },
	{ headers: { "Idempotency-Key": `ws-1-pro-${purchase.id}` } }, // saved when the customer confirmed
);
```

```python Python theme={"system"}
change = billing.attach(
    customer_id="acme",
    plan_id="workspace_pro",
    entity_id="ws-1",
    idempotency_key=f"ws-1-pro-{purchase.id}",  # saved when the customer confirmed
)
```

```bash cURL theme={"system"}
curl https://api.integrateledger.com/v1/billing/attach \
  -H "Authorization: Bearer $LEDGER_API_KEY" \
  -H "Idempotency-Key: ws-1-pro" \
  -H "Content-Type: application/json" \
  -d '{"customerId": "acme", "planId": "workspace_pro", "entityId": "ws-1"}'
```

* **One invoice:** a paid entity plan is a prorated change on the customer's Stripe bill of the plan's interval, answered like an [add-on attach](https://docs.integrateledger.com/pricing/add-ons.md#attach-it) with `itemChangeId` and `status`. Entities on the same plan share one Stripe item, its quantity counting them.
* **Grants:** with `entityGrants: "own"` (the default), the plan's credits and allowances belong to the entity and are spent before the shared pool. With `"pooled"`, they join the customer's shared pool. The first period is prorated from the attach; each renewal grants in full.
* **Free entity plans** change nothing in Stripe. They ride on the customer's bill and are granted with each of its periods.
* **Upgrades:** move an entity to a pricier plan with [upgrade](https://docs.integrateledger.com/api-reference/billing/upgrade.md), passing `entityId`. It's prorated, the period is kept, and other entities on the old plan stay on it.
* **Reading:** [Get an entity](https://docs.integrateledger.com/api-reference/entities/get.md) lists its `plans`, each with `planId`, `group`, `status` (`pending`, `active` or `canceling`), `startsAt` and `endsAt`.
* **Overage** stays with the customer's plan.

[Plans per workspace or user](https://docs.integrateledger.com/pricing/entity-plans.md) walks through the catalog and how it bills.

## Rules

* Entity allowances and caps apply to credits features.
* Create an entity before it spends: an unknown `entityId` returns `404 entity_not_found` on every feature type, so a typo never bypasses a cap.
* A `request_id` belongs to the entity it was first tracked for; retrying it for another entity, or for none, returns `409 request_id_conflict`.
* Creating, changing or deleting an entity needs a key with the `billing:write` permission, because an allowance is credits on top of the plan.
* Entity changes wait behind the customer's other billing changes, as track does. `503 customer_busy` means that wait passed 1.5 seconds and nothing changed: retry after `Retry-After` (1 second).
* An entity holds one plan per [group](https://docs.integrateledger.com/pricing/plan-groups.md) (`409 subscription_exists`).
* A paid entity plan joins a paid bill of its interval: attach the customer's own paid plan first (`409 entity_plan_needs_paid_bill`). A free entity plan needs the customer to hold a plan (`409 entity_plan_needs_bill`).
* An entity plan has a Stripe Price of its own, apart from the customer's plan (`409 entity_plan_shares_base_price`), and renews monthly or yearly (`400 entity_plan_must_recur`).
* An entity's plan moves by upgrade (`409 downgrade_unavailable_for_item`).
* An entity is deleted once it holds no plan (`409 entity_has_plan`), so a delete never changes what the customer pays. Its plans end with the customer's bill.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.