Metadata-Version: 2.4
Name: llm-forecast
Version: 0.1.0
Summary: Forecast LLM tokens and spend through model turnover, compare against standard methods on your own history, and check whether your pricing keeps its margin.
Author: Ankit Chopra
License: MIT
Project-URL: Homepage, https://github.com/azc21086/llm-forecast
Project-URL: Issues, https://github.com/azc21086/llm-forecast/issues
Project-URL: Paper-code, https://doi.org/10.5281/zenodo.24575344
Keywords: llm,token forecasting,finops,fp&a,pricing,margin,forecasting,llmops,focus
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas>=2.0
Requires-Dist: numpy>=1.22
Requires-Dist: matplotlib>=3.6
Provides-Extra: excel
Requires-Dist: openpyxl; extra == "excel"
Provides-Extra: parquet
Requires-Dist: pyarrow; extra == "parquet"
Provides-Extra: ml
Requires-Dist: scikit-learn; extra == "ml"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: scikit-learn; extra == "dev"
Dynamic: license-file

# llm-forecast

**Forecast your LLM tokens and spend for the weeks ahead, even as models keep changing. See how the forecast compares with standard methods on your own history, which models and teams are rising or fading, and whether your pricing still makes money at that usage.**

[![PyPI version](https://img.shields.io/pypi/v/llm-forecast)](https://pypi.org/project/llm-forecast/)
[![Total downloads](https://static.pepy.tech/badge/llm-forecast)](https://pepy.tech/project/llm-forecast)
[![Monthly downloads](https://img.shields.io/pypi/dm/llm-forecast)](https://pypistats.org/packages/llm-forecast)
[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/azc21086/llm-forecast/blob/main/notebooks/quickstart.ipynb)

![Example result card](docs/card_forecast.png)

## Try it in 30 seconds

```bash
pip install llm-forecast
llm-forecast demo
```

The demo runs on a bundled synthetic usage log (no real company data) and writes reports, tables, a chart and shareable cards to `results/`. No install? Use the Colab button.

## Four commands

| Command | What it does |
|---|---|
| `llm-forecast demo` | See everything work on sample data |
| `llm-forecast check my_usage.csv` | Show how your file was read: format, dates, models, accounts, totals |
| `llm-forecast run my_usage.csv --next 8w` | Forecast tokens and spend, compare methods, show model stages and account segments |
| `llm-forecast price-check` | Test whether your pricing keeps its margin at the usage you are forecasting |
| `llm-forecast cite` | Print the citation (plain text and BibTeX) |

`--next` takes any horizon: `30d`, `8w`, `3m`.

## Your data, as you already have it

The format is detected automatically:

| Where your data comes from | What to export |
|---|---|
| OpenAI | Usage API response (JSON) or a CSV of it |
| Anthropic | Usage report from the Admin API (JSON) or a CSV of it |
| OpenRouter | Activity export (CSV) or the activity API |
| LiteLLM | Spend logs (CSV or JSON) |
| AWS, Azure, Google Cloud and other FOCUS billing exports | The FOCUS cost and usage file; token rows are picked out automatically |
| Anything else | Any CSV, Excel, Parquet or JSON file with a date, a model and a token count |

If a column is not recognised, name it in one line:

```bash
llm-forecast run usage.csv --map date=usage_day,model=engine,tokens=units,account=team
```

Request-level logs are rolled up to days, input and output tokens are added, and several files or a whole folder can be read at once. If your data has a team, project, workspace, API key or user column, account segments are added automatically.

## What `run` shows you

**1. The forecast.** Expected tokens (and spend, when costs or prices are known) with a low to high range, by model and provider, split into recent level, replacement and expansion.

**2. How it compares with other methods, on your own history.** Every method forecasts from past dates using only the data available then, and is scored against what actually happened:

| Method | Error per model | Error per provider | Error on total | MASE | Bias | p-value vs recent level |
|---|---|---|---|---|---|---|
| Replacement and Expansion (this tool) | | | | | | |
| Recent level (last full week) | | | | | | |
| 28-day average | | | | | | |
| Holt-Winters with weekly seasonality | | | | | | |
| Linear trend | | | | | | |
| Gradient boosting (`--with-ml`, needs scikit-learn) | | | | | | |

Error is WAPE (absolute error divided by actual volume). MASE compares against repeating the previous window. The p-value is a Diebold-Mariano test with the small-sample correction, as in the paper. The table ranks honestly: if another method wins on your data, it says so. On the bundled sample, Holt-Winters is best per model and this method is best per provider and on the total.

**3. Model lifecycle stages** (paper, Table 4): new, young, ramping, fading, sibling launched and mature, with what actually happened next to models in each stage on your history.

**4. Account segments** (paper, Table 3): steady heavy, steady mid-size, intermittent, declining and ramping, built from 28-day scale, activity, volatility, trend and peak-day share. The paper fitted clusters on these features; this tool uses fixed rules with the same five labels so results are stable on any data. It also tests the paper's finding that switching declining accounts to the recent level cuts their error.

## Price check

```bash
llm-forecast price-check
```

![Price check card](docs/card_price_check.png)

It asks a few questions (how you charge, price, allowance, overage, seats, target margin), takes usage and your cost per token from the forecast, and runs the alignment test:

- **Level test:** at expected usage, does price cover cost?
- **Metric test:** as usage rises to heavy, does revenue grow at least as fast as cost? If not, the price metric is not aligned with the cost driver, and margin leaks as customers use more.

The result is a decision, not just numbers: **ALIGNED**, **WATCH**, **AT RISK**, **LEAK** or **UNDERPRICED**. Options are worked out for your plan: bound the gap with an allowance and overage, redesign the metric, reprice, or hold and recheck when metered usage arrives. Everything can also be passed as options for scripts, e.g. `--plan seat --price 30 --units 20000 --target-margin 0.3 --yes`.

The test checks the price metric against the cost driver. It does not set value-based prices or measure customer value.

## How the forecast works

LLM demand behaves like a product market with short generations: new models arrive every few weeks, take volume from their siblings and add demand of their own. The forecast is built in layers:

1. **Recent level.** The last full week, scaled to the horizon.
2. **Provider level.** The median of three simple rules on weekly totals.
3. **Replacement.** Lifecycle stage multipliers learned from your history and rescaled inside each provider, so volume a fading model loses passes to its siblings.
4. **Expansion.** Growth factor g times 1 + ρ(u − 1), where u is the net lift from a launch and ρ is the chance of one in the window. All three are estimated from your history when it is long enough; set them yourself with `--growth`, `--launch-lift` and `--launch-prob` (for example `--launch-prob 1` when a release is announced).

Every learned quantity uses only forecast windows that had already closed. Under 14 days the recent level is used alone. The paper evaluated 7 to 56 days. Longer horizons run the same way, and the comparison table shows how each method does on your history at that horizon.

## Outputs

| File | Contents |
|---|---|
| `forecast_models.csv`, `forecast_providers.csv` | Expected, low and high tokens (and spend) by model and provider, with the recent level, replacement and expansion parts |
| `method_comparison.csv` | Error measures for every method |
| `model_stages.csv`, `account_segments.csv`, `accounts_now.csv` | Stage and segment tables, and each account's current segment |
| `report.md`, `chart.png`, `card.png` | A readable report, a chart and a shareable card |
| `summary.json`, `price_check.json` | Everything above in a fixed, machine-readable form for agents and scripts |

## Use with AI agents

`summary.json` gives agents auditable numbers to explain rather than numbers to compute. `skills/llm-forecast/SKILL.md` is a ready-made Agent Skill, and `llms.txt` summarises the project for coding assistants.

## Limits

- This is a simplified open version of the method. The paper's full evaluation is in its companion code (below).
- Gains over the recent level are modest and depend on how fast models turn over in your data. On stable usage, simpler methods can do as well; the comparison table will show it.
- The price check assumes usage per billed unit is spread around the expected level by the light and heavy multiples you choose (defaults 0.5x and 2x).

## Cite

Please cite the method if you use this tool. Run `llm-forecast cite` for ready-to-paste text and BibTeX, or see `CITATION.cff`:

- Chopra, A. (2026). *Replacement and Expansion: Forecasting LLM Token Demand Under Rapid Model Turnover.* IEEE Access (under review). Companion code: [10.5281/zenodo.24575344](https://doi.org/10.5281/zenodo.24575344).
- Chopra, A. (2026, February 17). *Financial Planning for Agentic AI Systems: Managing Volatility in the Age of Autonomy.* California Management Review Insights.

## Privacy

The tool runs entirely on your machine. It sends nothing anywhere: no usage data, no telemetry, no run counts.

## Contributing

Readers for more export formats are the most useful contribution: see the issues labelled `adapter wanted` and [CONTRIBUTING.md](CONTRIBUTING.md). Results on public data are welcome too, including results where this method does not win.

## License

MIT. Copyright (c) 2026 Ankit Chopra.
