meterline-litellm 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. meterline_litellm-0.1.0/LICENSE +202 -0
  2. meterline_litellm-0.1.0/PKG-INFO +237 -0
  3. meterline_litellm-0.1.0/README.md +204 -0
  4. meterline_litellm-0.1.0/pyproject.toml +41 -0
  5. meterline_litellm-0.1.0/setup.cfg +4 -0
  6. meterline_litellm-0.1.0/src/meterline_litellm/__init__.py +18 -0
  7. meterline_litellm-0.1.0/src/meterline_litellm/cli.py +716 -0
  8. meterline_litellm-0.1.0/src/meterline_litellm/client.py +149 -0
  9. meterline_litellm-0.1.0/src/meterline_litellm/fetch.py +311 -0
  10. meterline_litellm-0.1.0/src/meterline_litellm/normalise.py +523 -0
  11. meterline_litellm-0.1.0/src/meterline_litellm/testing.py +392 -0
  12. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/PKG-INFO +237 -0
  13. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/SOURCES.txt +40 -0
  14. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/dependency_links.txt +1 -0
  15. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/entry_points.txt +2 -0
  16. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/requires.txt +10 -0
  17. meterline_litellm-0.1.0/src/meterline_litellm.egg-info/top_level.txt +1 -0
  18. meterline_litellm-0.1.0/tests/test_conformance_litellm.py +32 -0
  19. meterline_litellm-0.1.0/tests/test_conformance_litellm_broken.py +56 -0
  20. meterline_litellm-0.1.0/tests/test_done_check_B6_python_entry_litellm.py +102 -0
  21. meterline_litellm-0.1.0/tests/test_done_check_L01_shapes.py +112 -0
  22. meterline_litellm-0.1.0/tests/test_done_check_litellm.py +193 -0
  23. meterline_litellm-0.1.0/tests/test_empty_answer_litellm.py +69 -0
  24. meterline_litellm-0.1.0/tests/test_exit_codes_litellm.py +79 -0
  25. meterline_litellm-0.1.0/tests/test_float_spend_reaches_gold_litellm.py +61 -0
  26. meterline_litellm-0.1.0/tests/test_key_in_every_sink_litellm.py +86 -0
  27. meterline_litellm-0.1.0/tests/test_litellm_units.py +326 -0
  28. meterline_litellm-0.1.0/tests/test_missing_required_fields_litellm.py +152 -0
  29. meterline_litellm-0.1.0/tests/test_output_permissions_litellm.py +41 -0
  30. meterline_litellm-0.1.0/tests/test_pagination_litellm.py +94 -0
  31. meterline_litellm-0.1.0/tests/test_prompt_content_never_written_litellm.py +81 -0
  32. meterline_litellm-0.1.0/tests/test_public_api_litellm.py +53 -0
  33. meterline_litellm-0.1.0/tests/test_raw_not_overwritten_litellm.py +75 -0
  34. meterline_litellm-0.1.0/tests/test_replayed_page_litellm.py +50 -0
  35. meterline_litellm-0.1.0/tests/test_retry_after_cap_litellm.py +103 -0
  36. meterline_litellm-0.1.0/tests/test_review_f25_live_status_litellm.py +18 -0
  37. meterline_litellm-0.1.0/tests/test_review_f28_fixtures_litellm.py +43 -0
  38. meterline_litellm-0.1.0/tests/test_review_f28_shape_helper_litellm.py +48 -0
  39. meterline_litellm-0.1.0/tests/test_spend_scale_litellm.py +30 -0
  40. meterline_litellm-0.1.0/tests/test_state_file_litellm.py +128 -0
  41. meterline_litellm-0.1.0/tests/test_unknown_fields_litellm.py +111 -0
  42. meterline_litellm-0.1.0/tests/test_write_phase_litellm.py +55 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Xavier Meyer
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,237 @@
1
+ Metadata-Version: 2.4
2
+ Name: meterline-litellm
3
+ Version: 0.1.0
4
+ Summary: Reads a LiteLLM proxy's admin API and emits meterline records.
5
+ Author: Xavier Meyer
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/meyerx/meterline
8
+ Project-URL: Source, https://github.com/meyerx/meterline/tree/main/packages/meterline-litellm
9
+ Project-URL: Issues, https://github.com/meyerx/meterline/issues
10
+ Project-URL: Changelog, https://github.com/meyerx/meterline/blob/main/CHANGELOG.md
11
+ Keywords: ai,llm,usage,cost,tokens,finops,litellm,proxy
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: System Administrators
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: System :: Monitoring
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: meterline>=0.2
25
+ Requires-Dist: httpx>=0.27
26
+ Provides-Extra: parquet
27
+ Requires-Dist: pyarrow>=17; extra == "parquet"
28
+ Provides-Extra: dev
29
+ Requires-Dist: pytest>=8; extra == "dev"
30
+ Requires-Dist: pytest-xdist==3.8.0; extra == "dev"
31
+ Requires-Dist: ruff==0.17.3; extra == "dev"
32
+ Dynamic: license-file
33
+
34
+ # meterline-litellm
35
+
36
+ > Part of [meterline](https://github.com/meyerx/meterline/blob/main/README.md). meterline helps people who run or pay for AI tools in an organisation see and compare usage and cost across providers: it reads each provider's usage API and writes the data in one common record format.
37
+
38
+ Reads a LiteLLM proxy's admin API and emits [`meterline`](https://github.com/meyerx/meterline/blob/main/packages/meterline/README.md) records. Python 3.10 or later; runtime dependencies `meterline` and `httpx`.
39
+
40
+ **Status:** built against documented shapes ([evidence level](https://github.com/meyerx/meterline/blob/main/docs/glossary.md#evidence-levels)), here the LiteLLM source code (spec v2 sections 5 and 8, [`docs/dev/meterline-v2-SPEC-v1.0-2026-10-10.md`](https://github.com/meyerx/meterline/blob/main/docs/dev/meterline-v2-SPEC-v1.0-2026-10-10.md)), with acceptance tests and unit tests on a fake proxy. It was also checked against LiteLLM v1.106.6's own route code, which covers the shape of the replies, not the database, the sign-in or the timing. Not yet run against a live account. See [Known limits](#known-limits).
41
+
42
+ ```sh
43
+ pip install meterline-litellm
44
+ pip install "meterline-litellm[parquet]" # optional: Parquet output
45
+ ```
46
+
47
+ **From a clone** of the repository (for the tests and development):
48
+
49
+ ```sh
50
+ pip install -e packages/meterline -e "packages/meterline-litellm[dev]"
51
+ pip install -e "packages/meterline-litellm[parquet]" # optional: Parquet output
52
+ ```
53
+
54
+ Then, with a read-only proxy key in the environment (see "Before you run it"):
55
+
56
+ ```sh
57
+ export LITELLM_BASE_URL=https://proxy.example.com
58
+ export LITELLM_API_KEY=... # never commit this
59
+ meterline-litellm backfill --proxy-name my-proxy --since 2026-01-01
60
+ ```
61
+
62
+ ## Read this first: two views of the same traffic
63
+
64
+ `proxy_user` and `end_user` records describe the **same traffic** and must never be added together.
65
+
66
+ - `proxy_user`: the person the proxy charged the call to (the owner of the key, or the signed-in user under JWT). Always written.
67
+ - `unassigned`: usage the proxy attributes to no person (keys with no owner, JWT calls without a user claim). Always written when there is any. `proxy_user` plus `unassigned` equals the proxy's total.
68
+ - `end_user`: the person named by the calling application. Optional, off by default (`--end-users`). It repeats traffic already in the `proxy_user` records.
69
+
70
+ Also: when the proxy routes calls to a provider that has its own connector, the same calls appear in both sources. `provider` says which source reported a record; do not add a proxy's records to the serving provider's records for the same traffic.
71
+
72
+ ## Before you run it
73
+
74
+ Check these five things first:
75
+
76
+ - **Version.** The proxy must be **v1.67.0-stable or later**. v1.66.0-stable lacks the daily cache-token columns (migration `20250415151647_add_cache_read_write_tokens_daily_spend_transactions`); v1.67.0-stable has them, the cache columns of the daily metrics, `breakdown.entities` and the key metadata's `team_id` (PR #10152). The exact tag was resolved from the repository's tags. The shapes were transcribed from v1.104.2 (read on 2026-10-11), and on 2026-10-29 the reply shapes were checked again against v1.106.6 (see Response shapes). An older proxy answers 404 on the daily activity route; the run then stops with "path not found: wrong base URL, or a proxy older than v1.67.0-stable".
77
+ - **Key.** A key of a user with role `proxy_admin_viewer` (read-only). It may call every route used here and nothing that writes. `--end-users` needs `proxy_admin` or `proxy_admin_viewer`; other roles get 401 from that route.
78
+ - **Environment.** `LITELLM_BASE_URL` (for example `https://proxy.example.com`) and `LITELLM_API_KEY`. They are read in `main` and nowhere else; the key is sent only as `Authorization: Bearer <key>` and never written to disk, a log or a manifest.
79
+ - **Proxy name.** `--proxy-name` is required for `backfill` and `daily`: any non-empty label you choose. It becomes `provider_account_id`.
80
+ - **Database.** The daily routes need the proxy to run with its database; without one they answer 500.
81
+
82
+ ## Commands
83
+
84
+ All commands take `--data-dir` (default `exports/meterline-litellm`, gitignored) and `--parquet`.
85
+
86
+ | Command | What it does |
87
+ |---|---|
88
+ | `backfill --since YYYY-MM-DD` | Walks 31-day windows back from yesterday to the start date. Resumes after a failure by re-running the same command. |
89
+ | `daily` | Reads every closed UTC day since the last day loaded, and always re-reads the two days before today (rows arrive late). With no `state.json` it stops (exit 2) and asks for `--since YYYY-MM-DD`, the first day to load, rather than quietly fetching the month again; `--since` is ignored once a state file exists. Safe to run twice a day: unchanged data publishes nothing. |
90
+ | `rebuild` | Re-creates every record from the raw store and the published run manifests. No API calls, no key. |
91
+
92
+ Options of `backfill` and `daily`: `--proxy-name`, `--rate-per-minute` (default 120; the routes document no limit), `--end-users`.
93
+
94
+ The state file `state.json` is checked on every `backfill` and `daily`. If it is cut short, empty, not the shape the program writes or points outside the data directory, the run stops with exit 2 before any request and names the file. Restore it from a backup, or move it aside and run `backfill --since`: published records are kept, and unchanged rows are not written twice. `rebuild` does not read it.
95
+
96
+ Exit codes, which a run also reports as `failure_kind` (`RunResult.failure_kind` from Python; a manifest written by a run that failed a check carries it too):
97
+
98
+ | Code | `failure_kind` | Meaning | What to do |
99
+ |---|---|---|---|
100
+ | `0` | `none` | The run finished. | Nothing. |
101
+ | `1` | `transient` | A 5xx, a 429 after the retries, a timeout or a connection error. | Run the same command again later; progress is kept. |
102
+ | `2` | `input` | A failed check, bad arguments or a configuration error such as a missing key or address. | Fix the input; running the same command again fails the same way. |
103
+ | `3` | `permanent` | The provider refused the request: 400, 401, 403, 404 or 422. The message starts "permanent:" and `RunResult.http_status` holds the status. | Fix the key, its scope or the identifier the message names; do not retry unchanged. |
104
+
105
+ To run this connector every day, with a retry rule and a gold build after it, see [the daily job example](https://github.com/meyerx/meterline/blob/main/examples/03-daily-job/README.md).
106
+
107
+ ## How it works
108
+
109
+ 1. **Fetcher** (`client.py`, `fetch.py`). Per window: one call to `GET /user/daily/activity` without `user_id` (the global view), then one call per user found in the global view's `breakdown.entities`. Entities with an empty key or the key `Unassigned` are not called. With `--end-users`: one call to `GET /customer/daily/activity` for all end users, then one call per end-user id. `GET /user/list` once per run, only to read `user_id` → `user_email`. Every response page is stored as received under `raw/<endpoint>/<window>/<view>/<pulled_at>.json.gz`; per-user file names carry a hash of the id, never the id.
110
+ 2. **Normaliser** (`normalise.py`). Reads raw files only. Pages are merged by date (a day can be cut across two pages with partial numbers): metrics are summed, breakdown maps merged key by key.
111
+
112
+ At 1,000 active people this is about 1,000 calls per window for each view.
113
+
114
+ ## Files under the data directory
115
+
116
+ A run reads and writes these four things under `--data-dir`:
117
+
118
+ | Path | Contents |
119
+ |---|---|
120
+ | `raw/<endpoint>/<window>/<view>/<pulled_at>.json.gz` | One file per view and window pull: the global view, or one user or end user (the folder name is a hash of the id, never the id), with every page's status and body text. |
121
+ | `runs/<batch_id>.json` | The run manifest: `command`, `batch_id`, `status`, `windows`, `pages`, `row_counts`, `totals`, `unknown_products`, `unknown_models`, `token_anomalies`, `modelless_days`, `skipped_entities`, `raw_files`, `published_counts`, `failures` and `unknown_fields` (the fields of the provider that the connector does not know, as endpoint, key path and count, never a value). |
122
+ | `records/<kind>/<batch_id>.jsonl` | Published records (v2); kinds are `usage` and `org_totals`. A batch with no changed records writes no file. |
123
+ | `state.json` | `days_loaded`, and a `pending` block that lets an interrupted run resume. |
124
+
125
+ ## Record mapping
126
+
127
+ Four kinds of record come from the proxy:
128
+
129
+ | Record | From | Notes |
130
+ |---|---|---|
131
+ | `UsageRecord`, `proxy_user` | each per-user view, per date and model | `actor_email` from `/user/list` (null if absent), `model_provider` null (the daily view cannot tie a model to a provider) |
132
+ | `UsageRecord`, `unassigned` | global view minus the sum of the per-user views, per date and model | only when any field is non-zero; a negative difference fails the run |
133
+ | `UsageRecord`, `end_user` | each per-end-user view (`--end-users`) | `actor_login` is the end user's alias; no unassigned record is derived |
134
+ | `OrgTotal` | the global view's `breakdown.models`, per date and model | `product` `""` and `model_provider` null, as for usage; no all-models row |
135
+
136
+ Fields: `tokens_input_total` = `prompt_tokens`; `tokens_cache_read` = `cache_read_input_tokens`; `tokens_cache_write` = `cache_creation_input_tokens`; `tokens_input_uncached` = total − read − write; `tokens_output` = `completion_tokens`; `requests` = `api_requests`; `cost` = `spend`, `cost_list` null, `currency` `USD`, `cost_basis` `estimate` (LiteLLM prices calls from its own price list). LiteLLM sends `spend` and `flat_cost` as floating-point numbers with up to 19 decimals, and the connector rounds them to the 12 decimals of the record format (half to even). `extras`: `successful_requests`, `failed_requests`, and `flat_cost` when non-zero. The 5m/1h cache split and `tokens_reasoning` are null. **Unverified:** whether LiteLLM's `prompt_tokens` includes the cache figures. It may differ by provider, and the sources read on 2026-10-29 do not settle it. The connector assumes it does, because `tokens_input_uncached` is total − read − write. A live run should confirm this before `tokens_input_uncached` is trusted. If it is wrong, `tokens_input_uncached` is too small (or null) and `tokens_input_total` is too small by the cache figures. If total − read − write is negative, `tokens_input_uncached` is null and the key is listed in the manifest under `token_anomalies` (`<date>|<model>|<actor_kind>|<12 hex of sha256(actor id)>`).
137
+
138
+ **Where `requests` comes from.** `api_requests` counts rows of LiteLLM's spend logs, one row per logged request (LiteLLM's cost-tracking documentation and route code, read on 2026-10-29). It is not a count of answers the proxy gave. A request that was not logged is not counted, and each upstream attempt, a retry for example, counts as its own request. `successful_requests` and `failed_requests` come from the status in the same log rows.
139
+
140
+ The key and the constant fields are the same in every LiteLLM record:
141
+
142
+ - `provider` `litellm`; `provider_account_id` the `--proxy-name` label; `scope` `""`; `grain` `day`, with `period_start` = `period_end` = the row's date;
143
+ - `actor_id`: the proxy's `user_id` for `proxy_user`, `(unassigned)` for `unassigned`, the end user's id for `end_user`; `actor_email` from `/user/list` for `proxy_user` only; `actor_login` the end user's alias, else null;
144
+ - `product` `""` (the daily view names no product); `actor_deleted` null (the proxy does not say); `cost_only` false;
145
+ - `cost_list` null and `data_refreshed_at` null (the proxy reports neither).
146
+
147
+ `is_final` is true when the day ended at least 2 full UTC days before the pull. No `ActivityRecord`, `SeatSnapshot` or `SeatAssignment` comes from LiteLLM.
148
+
149
+ Records are built and checked with `meterline.records` (`UsageRecord`, `OrgTotal`, `batch_failures`, `latest_per_key`) and written and read with `meterline.writers` (`write_jsonl`, `write_parquet`, `read_jsonl`), the two submodule routes the core package `meterline` keeps stable. A script that reads this connector's output can import the same names from those routes or from the package root.
150
+
151
+ ## Response shapes
152
+
153
+ [`docs/api-shapes/litellm/`](https://github.com/meyerx/meterline/blob/main/docs/api-shapes/litellm) holds one file per endpoint with synthetic values, and a README naming the source file and line of each in the LiteLLM source at tag `v1.104.2` (read on 2026-10-11). On 2026-10-29 the route functions of LiteLLM v1.106.6 were run with a fake database and their reply shapes compared with these files; that check covers reply shapes, not the SQL, the sign-in or the timing, and the folder's README lists what changed. Done-check L01 compares what the fake proxy returns with these files, by keys and nesting. It cannot show that a live proxy answers the same way.
154
+
155
+ **When a live response differs.** A live run is the first real test of these shapes. If a response does not match its shape file:
156
+
157
+ 1. Save the live response's structure as the new shape file, with every value replaced by an invented one: synthetic values only, and never commit a real response.
158
+ 2. Run the shape check, `python -m pytest -q packages/meterline-litellm -k "L01"`. L01 fails and names each key that differs between the fake API and the file.
159
+ 3. Change the fake API to match, then `fetch.py` and `normalise.py`, until the shape check and the other acceptance tests pass.
160
+ 4. Update the source and the date read in the shape folder's README, and add an entry to the change list of the spec.
161
+
162
+ **When the proxy is upgraded.** The routes are versioned by the proxy's own release, not by a header: the shapes were transcribed from `v1.104.2` (read on 2026-10-11) and re-checked against `v1.106.6` on 2026-10-29, and the connector needs `v1.67.0-stable` or later. After an upgrade, compare the release notes of the routes used here with the shape files.
163
+
164
+ ## Known limits
165
+
166
+ The limits of this connector, grouped by kind.
167
+
168
+ **How the connector reads the proxy.** These follow from the proxy's API and the connector's rules:
169
+
170
+ - **Raw files are kept and never replaced.** Every pull writes its own `raw/.../<pulled_at>.json.gz`; nothing in the connector deletes or rewrites one, so the evidence a record was built from stays. A second pull in the same UTC second would need the same file name. Such a run is refused (exit 2) before it asks the proxy anything, or, if it had started, stops without replacing the file; a run in the second of a finished run is refused too. Wait a second and run again. Raw files grow with every run; archive or delete old ones by hand once the records they produced are no longer needed for a `rebuild`.
171
+ - **Page loops end.** One query follows at most 10,000 pages (`MAX_PAGES` in `fetch.py`). A page that repeats the one before, a page number other than the one asked for, or more pages than that, stops the run with exit 2 and a failed manifest.
172
+ - **Waits are capped.** A `Retry-After` header is honoured up to 300 seconds (`--max-retry-after`); both the delay-seconds and the HTTP-date forms are read, and a value that is not a finite number is ignored in favour of the back-off.
173
+ - **End-user ids with a comma** cannot be asked for one at a time (the route splits on commas); they are skipped and counted under `skipped_entities.ids_with_comma` in the manifest.
174
+ - **Re-reads and removals.** A re-read day writes a record only when a key's values changed. A key that vanishes (for example an `unassigned` difference that falls to 0 after late rows were re-attributed) is not replaced, so its earlier record stays the latest.
175
+ - **A resumed run reuses the pages it already has.** After a failure, the same command picks up the pages the failed run stored (the `pending` block of `state.json`), however old they are, and publishes them under the new run's `pulled_at`. Running a different command in between drops them, and a run whose checks fail clears them, so a failed long `backfill` asks for everything again.
176
+ - **Finality.** `daily` re-reads the two days before today, and a day is final only from the third day after it, so records written only by `daily` stay `is_final = false`; `backfill` over the period writes the final ones.
177
+
178
+ **What the proxy reports.** The proxy's own figures carry these quirks, and the records keep them:
179
+
180
+ - **Usage with no model.** The proxy counts a raw row with a null model in the day's metrics but leaves it out of `breakdown.models`. The connector keeps that usage under `model` `""` (in the per-user records and, for ownerless usage, the `unassigned` record), so that the totals still add up. The `OrgTotal` has per-model rows only; the manifest lists such dates under `modelless_days`.
181
+ - **Failed requests** are counted inside the proxy's request and token counters as the proxy reports them; meterline does not split them. LiteLLM's release notes for v1.106.2 (2026-10-21) say that timeouts, cancellations and billed retries are now priced instead of logged at $0. **Unverified:** how far this changes `spend` in the daily rows; the code behind it was not traced. Cost for days with failed requests can differ between proxy versions.
182
+ - **Rounding noise.** The proxy sums `spend` as binary floating point, so sums differ by about 1e-16. A money difference below 0.000001 counts as zero when records are checked against totals and when the `unassigned` difference is derived. Costs keep the digits the proxy sent.
183
+
184
+ **What it does not read.** The connector reads the daily activity routes and the user list only.
185
+
186
+ - **Not read:** the database path, per-request spend logs, the licence-gated `*/spend/report` routes, team and tag views, any per-request field (`messages`, `response`, `metadata`).
187
+
188
+ **Personal data.** A user id and an email appear in raw files and records (they are data); logs and manifests carry counts, statuses and hashes only.
189
+
190
+ ## Public interface
191
+
192
+ What later releases keep working, under the rules of the compatibility policy in [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md#compatibility):
193
+
194
+ - the `meterline-litellm` command line: its commands, options and exit codes, as listed under Commands;
195
+ - the records it publishes under `records/`, in the v2 format of [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md);
196
+ - from Python, the names in `meterline_litellm.__all__`: `run` (see [From Python](#from-python)), `RunResult`, the error classes `ApiError`, `PaginationError` and `PathNotFound`, and `__version__`;
197
+ - the module `meterline_litellm.testing` (`FakeAPI`, `sample_dataset`, see [Testing your integration without a key](#testing-your-integration-without-a-key)).
198
+
199
+ Everything else is internal and may change in any release: every other module of the `meterline_litellm` package, and the layout of the raw files, the run manifests and `state.json` (they are documented so you can read them, not so you can build on them).
200
+
201
+ ## From Python
202
+
203
+ ```python
204
+ from datetime import date
205
+ from meterline_litellm import run
206
+
207
+ result = run("backfill", since=date(2026, 9, 1), proxy_name="my-proxy", data_dir="exports/meterline-litellm",
208
+ credentials={"LITELLM_API_KEY": "...", "LITELLM_BASE_URL": "https://proxy.example.com"})
209
+ print(result.exit_code, result.batch_id, result.records)
210
+ ```
211
+
212
+ `run(command, *, data_dir, credentials=None, transport=None, **options)` does what the command line does; `command` is `"backfill"`, `"daily"` or `"rebuild"` and the options are the command-line options under their Python names (`since`, `proxy_name`, `end_users`, `rate_per_minute`, `max_retry_after`, `parquet`), plus `base_url`, which overrides `LITELLM_BASE_URL`. `credentials` is a mapping from `LITELLM_API_KEY` and `LITELLM_BASE_URL` to their values, or a function without arguments that returns one; when it is given the environment is not read, and a missing name counts as unset. With `None` the environment is read as the command line does. The key goes only to the HTTP client. `run` returns a `RunResult` (`exit_code`, `batch_id`, `manifest_path`, `records`, `windows`, `skipped`, `unknown_fields`, `ok`). A failed run does not raise: it returns the exit code the command line would exit with, after printing the same message.
213
+
214
+ ## Testing your integration without a key
215
+
216
+ ```python
217
+ from datetime import date
218
+ from meterline_litellm import run
219
+ from meterline_litellm.testing import FakeAPI
220
+
221
+ fake = FakeAPI() # a small synthetic dataset, built in code
222
+ result = run("backfill", since=date(2026, 9, 1), proxy_name="my-proxy", data_dir="out",
223
+ credentials={"LITELLM_API_KEY": fake.api_key, "LITELLM_BASE_URL": "https://proxy.example.com"},
224
+ transport=fake.transport)
225
+ assert result.ok
226
+ ```
227
+
228
+ `FakeAPI` answers the three routes in the documented shapes from synthetic data (four users at `example.com` addresses, two models, 2026-09-01 to 2026-09-14; pass your own rows to change that) and can script faults. It does not prove that a live proxy accepts your key, that a live proxy answers in these shapes, or how a live proxy rate-limits.
229
+
230
+ ## Tests
231
+
232
+ ```sh
233
+ python -m pytest -q packages/meterline-litellm # everything
234
+ python -m pytest -q packages/meterline-litellm -k done_check # the acceptance checks L01 to L09
235
+ ```
236
+
237
+ Tests run against a fake API built on `httpx.MockTransport` that serves synthetic data in the documented shapes and can script faults. The clock and sleep are injected, so nothing really waits.