# Add a Customer Lifetime Value Model to a dbt Project from a Linear Issue

An agent adds a customer_lifetime_value mart to a dbt project with YAML docs, a unit test and a reconciliation test; dbt build passes 62 of 62 on DuckDB.

[Run this on Rush](https://cloud.getrush.ai/run/add-customer-ltv-model/dbt/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads `Taskfile.yml`, `dbt_project.yml`, `packages.yml` and the `customers` mart's SQL and YAML first. It finds the project needs dbt 2.0 or later, so it builds a venv with `dbt` 2.0.9 and a DuckDB profile outside the repository. It also finds that `customers` already holds lifetime spend, order count and first and last order timestamps.

It adds `models/marts/customer_lifetime_value.sql`, which sums pre-tax `subtotal` from `orders` per customer and inner-joins `stg_customers`, and `customer_lifetime_value.yml` with column docs, `unique`, `not_null`, `relationships` and `dbt_utils.expression_is_true` tests plus a unit test on three customers. A singular test, `data-tests/customer_lifetime_value_matches_customers.sql`, fails for any customer whose numbers disagree with `customers`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add a Customer Lifetime Value Model to a dbt Project. Add a mart model with each customer's lifetime revenue, order count and first and last order dates, documented in YAML with tests.\" Do the work in this repository, add tests for it, run dbt build on DuckDB, and open a pull request that links back to the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on dbt-labs/jaffle-shop at 5beb145: 3 files changed, 128 insertions(+), all new: the model (+49), its YAML (+64) and the reconciliation test (+15). The issue text was written for the trial and had no ID. The untouched clone built `Summary: 49 total | 49 success`; after the change `dbt build --vars '{"load_source_data": true}'` ended `Summary: 62 total | 62 success`, with the one warning the clone already had about semantic manifest validation.

For the reviewer: the model duplicates what `customers` already computes, held in step by the reconciliation test, so decide whether to keep both. Revenue is pre-tax; switch to `order_total` if you mean tax-inclusive.

## Run it on every Linear issue labeled agent

An automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` runs the `linear-label-to-pr` recipe's prompt when an analyst in your Rush org labels a request for a new mart. An issue that says whether revenue includes tax, and whether customers with no orders belong in the model, settles the two questions this run answered on its own.

## Why run it on Rush?

The ask looked new, but `customers` already answered most of it. The run noticed, and instead of a second definition that drifts, it shipped a test that fails the build the day the two marts disagree. Its first attempt, a `dbt_utils.equal_rowcount` test against `customers`, errored in DuckDB while materializing, and the agent replaced it with a singular reconciliation test.

## Frequently asked questions

### Why did the first dbt build fail?

With `dbt-core` 1.12.6 the project refused to run: `dbt_project.yml` requires dbt `>=2.0.0`. The agent installed `dbt` 2.0.9, the package the repository's Taskfile installs, and the untouched clone then passed.

### Are customers with no orders in the model?

No. The model inner-joins on orders, so it has one row per customer who has ordered, while `customers` keeps everyone with null metrics. The reconciliation test only compares customers who have orders.

## More dbt jobs

- [Add dbt Project Evaluator to a dbt Project](https://getrush.ai/run/add-dbt-project-evaluator/dbt/)
- [Add dbt Exposures for Your Dashboards from a Linear Issue](https://getrush.ai/run/add-exposures/dbt/)
- [Add a GitHub Actions CI Job That Runs dbt build on Every Pull Request](https://getrush.ai/run/add-github-actions-ci/dbt/)

## More jobs like this: Turn a Linear label into a PR

- [Add D1 Migrations to a Cloudflare Worker](https://getrush.ai/run/add-d1-migrations/cloudflare-workers/)
- [Add Dark Mode to a Reflex App](https://getrush.ai/run/add-dark-mode/reflex/)
- [Add a Dark Mode Toggle to a Vite React App from a Linear Issue](https://getrush.ai/run/add-dark-mode/vite-react/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
