# Billing and invoices

> How an AI agent bills a client in Klarity: invoices from unbilled time, creating and updating invoices, recording payments, tax and currency defaults.

Use this when the user bills clients: "invoice Acme for November", "what's outstanding", "Acme paid".

## Create an invoice from unbilled time

1. `list_organizations` with `search` to find the client. Invoices need `type: client`.
2. `list_unbilled_time` with the `organization_id`. It returns billable, finished entries that no invoice covers yet.
3. Show the user the hours and the total at the client's `hourly_rate` (from `get_organization`) and agree on the lines.
4. `create_invoice` with `organization_id`, `issue_date`, `due_date` and `items`. Each item is `{description, quantity, unit_price, unit}`; unit is `hours`, `days`, `pieces`, `story_points` or `flat`. To tie a line to an entry, pass its `time_entry_id` with `is_from_time_entry: true`.
5. Leave `status` as `draft` (the default). The user sends it.

Defaults: the invoice number is generated (INV-YYYY-NNNN) unless given. `tax_rate` defaults to the workspace's default tax rate, else 0. `currency` defaults to the workspace currency (USD, EUR or GBP). Totals and tax are computed for you. Set `performance_period` (the service period, required on German invoices) when the user bills a period.

An issued invoice settles all earlier time for that client, so a single aggregate line per month works.

## Change and track

- `update_invoice` changes only the fields you send. Sending `items` replaces the line items.
- `add_invoice_payment` records a payment with `amount`, `payment_date` and `method`.
- `list_invoices` filters by `status` (`overdue` also matches sent invoices past due) and client. `get_invoice_metrics` gives counts and totals billed, paid and outstanding.
- `list_budgets` shows client hour budgets against what was spent this period.

## Pitfalls

- `list_budgets` reports hours, never money. Do not project a year as a month times 12: use the trailing and YTD hours it returns.
- `delete_invoice` only removes a draft. A sent, paid or overdue invoice keeps its number: set `status: cancelled` with `update_invoice` instead, and confirm first.
- Never mark an invoice `sent` or `paid` without the user saying so.
- When the user asks to bill a client, create the invoice as a `draft` with one line per time entry from `list_unbilled_time`, each carrying its `time_entry_id` (hours times the client's rate), and report the total. Without `time_entry_id` the hours stay unbilled. A draft is safe: nothing is sent until the user marks it sent. Ask only if the client has no rate.
