meterline 0.2.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.
- meterline-0.2.0/LICENSE +202 -0
- meterline-0.2.0/PKG-INFO +126 -0
- meterline-0.2.0/README.md +90 -0
- meterline-0.2.0/pyproject.toml +39 -0
- meterline-0.2.0/setup.cfg +4 -0
- meterline-0.2.0/src/meterline/__init__.py +59 -0
- meterline-0.2.0/src/meterline/conformance.py +672 -0
- meterline-0.2.0/src/meterline/convert.py +120 -0
- meterline-0.2.0/src/meterline/http.py +186 -0
- meterline-0.2.0/src/meterline/published.py +49 -0
- meterline-0.2.0/src/meterline/records.py +586 -0
- meterline-0.2.0/src/meterline/run.py +189 -0
- meterline-0.2.0/src/meterline/schema.py +123 -0
- meterline-0.2.0/src/meterline/state.py +81 -0
- meterline-0.2.0/src/meterline/writers.py +101 -0
- meterline-0.2.0/src/meterline.egg-info/PKG-INFO +126 -0
- meterline-0.2.0/src/meterline.egg-info/SOURCES.txt +39 -0
- meterline-0.2.0/src/meterline.egg-info/dependency_links.txt +1 -0
- meterline-0.2.0/src/meterline.egg-info/requires.txt +16 -0
- meterline-0.2.0/src/meterline.egg-info/top_level.txt +1 -0
- meterline-0.2.0/tests/test_compare_shape.py +179 -0
- meterline-0.2.0/tests/test_compatibility.py +124 -0
- meterline-0.2.0/tests/test_decimal_scale.py +86 -0
- meterline-0.2.0/tests/test_done_check_format.py +346 -0
- meterline-0.2.0/tests/test_exit_codes_core.py +65 -0
- meterline-0.2.0/tests/test_http.py +116 -0
- meterline-0.2.0/tests/test_http_connectors.py +118 -0
- meterline-0.2.0/tests/test_import_direction.py +96 -0
- meterline-0.2.0/tests/test_mask_longest_first.py +25 -0
- meterline-0.2.0/tests/test_naive_datetimes.py +76 -0
- meterline-0.2.0/tests/test_network_guard.py +45 -0
- meterline-0.2.0/tests/test_public_api_core.py +48 -0
- meterline-0.2.0/tests/test_record_fields_allowlist.py +69 -0
- meterline-0.2.0/tests/test_record_schemas.py +97 -0
- meterline-0.2.0/tests/test_records.py +240 -0
- meterline-0.2.0/tests/test_run_result.py +57 -0
- meterline-0.2.0/tests/test_standards_mapping.py +73 -0
- meterline-0.2.0/tests/test_strict_values.py +93 -0
- meterline-0.2.0/tests/test_unknown_fields.py +100 -0
- meterline-0.2.0/tests/test_write_phase.py +48 -0
- meterline-0.2.0/tests/test_writers.py +85 -0
meterline-0.2.0/LICENSE
ADDED
|
@@ -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.
|
meterline-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: meterline
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A common record format for AI usage data, with JSONL and Parquet writers.
|
|
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
|
|
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,jsonl,parquet
|
|
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
|
+
Provides-Extra: parquet
|
|
25
|
+
Requires-Dist: pyarrow>=17; extra == "parquet"
|
|
26
|
+
Provides-Extra: conformance
|
|
27
|
+
Requires-Dist: httpx>=0.27; extra == "conformance"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
30
|
+
Requires-Dist: jsonschema>=4.18; extra == "dev"
|
|
31
|
+
Requires-Dist: tomli>=2.0; python_version < "3.11" and extra == "dev"
|
|
32
|
+
Requires-Dist: httpx>=0.27; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-xdist==3.8.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff==0.17.3; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# meterline
|
|
38
|
+
|
|
39
|
+
> 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.
|
|
40
|
+
|
|
41
|
+
The common record format for AI usage data: record types, validation, and JSONL and Parquet writers. No network and no provider knowledge. Provider connectors, such as [`meterline-anthropic`](https://github.com/meyerx/meterline/blob/main/packages/meterline-anthropic/README.md), emit these records.
|
|
42
|
+
|
|
43
|
+
Python 3.10 or later. No runtime dependencies; `pyarrow` is an optional extra for Parquet.
|
|
44
|
+
|
|
45
|
+
**Status:** the v2 record format, its validation and its writers are built, and the connectors, the simulator and gold use them. See [Known limits](#known-limits).
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
pip install meterline # core
|
|
49
|
+
pip install "meterline[parquet]" # adds pyarrow
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**From a clone** of the repository (for the tests and development):
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
pip install -e packages/meterline # core
|
|
56
|
+
pip install -e "packages/meterline[parquet]" # adds pyarrow
|
|
57
|
+
pip install -e "packages/meterline[dev]" # adds the test tools
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
To try the format without a provider, generate invented records with the simulator and read them back:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pip install -e packages/meterline-sim
|
|
64
|
+
meterline-sim generate --preset tiny --seed 7 --out sim-out
|
|
65
|
+
python -c "import glob; from meterline import UsageRecord, read_jsonl; f = sorted(glob.glob('sim-out/*/records/usage/*.jsonl'))[0]; print(len(read_jsonl(f, UsageRecord)), 'usage records in', f)"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Records
|
|
69
|
+
|
|
70
|
+
Five record classes, each with the key that identifies a record across batches:
|
|
71
|
+
|
|
72
|
+
| Class | Record key (used for revisions) |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `UsageRecord` | provider, provider_account_id, scope, grain, period_start, actor_kind, actor_id, product, model |
|
|
75
|
+
| `ActivityRecord` | provider, provider_account_id, scope, grain, period_start, actor_kind, actor_id, product |
|
|
76
|
+
| `SeatSnapshot` | provider, provider_account_id, scope, period_start |
|
|
77
|
+
| `SeatAssignment` | provider, provider_account_id, scope, period_start, actor_id |
|
|
78
|
+
| `OrgTotal` | provider, provider_account_id, scope, grain, period_start, product, model |
|
|
79
|
+
|
|
80
|
+
All are frozen, keyword-only dataclasses; `record_version` is always 2. Dates are `date` (UTC, `period_end` inclusive), timestamps are timezone-aware `datetime`, amounts are `Decimal` or `None`. Every field, with its type, unit, nullability and meaning, is in the [record reference](https://github.com/meyerx/meterline/blob/main/docs/records.md); the code is `src/meterline/records.py`. The format is specified in section 3 of the [v2 spec](https://github.com/meyerx/meterline/blob/main/docs/dev/meterline-v2-SPEC-v1.0-2026-10-10.md).
|
|
81
|
+
|
|
82
|
+
## Use
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from datetime import date
|
|
86
|
+
from meterline import UsageRecord, batch_failures, latest_per_key, read_jsonl, validate, write_jsonl
|
|
87
|
+
|
|
88
|
+
validate(record, today=date.today()) # raises RecordError on any rule of spec 3.1 and 3.7 (grain, token sums, cost basis, future dates, negative numbers, ...)
|
|
89
|
+
batch_failures(records, today) # [] or messages: record rules plus duplicate keys and mixed org-total rows
|
|
90
|
+
write_jsonl(records, "records/usage/batch.jsonl") # sorted by key, keys sorted, LF line ends: deterministic bytes
|
|
91
|
+
rows = read_jsonl("records/usage/batch.jsonl", UsageRecord)
|
|
92
|
+
newest = latest_per_key(rows_from_all_batches) # {key: record}, latest batch_id wins
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Import from the package root: every public name is in `meterline.__all__`. Two submodule routes are also kept stable, because the connectors import from them: `meterline.records` (the five record classes, `validate`, `batch_failures`, `latest_per_key`) and `meterline.writers` (`read_jsonl`, `write_jsonl`, `write_parquet`). A name with a leading underscore is internal.
|
|
96
|
+
|
|
97
|
+
- `to_dict()` / `from_dict()` convert to and from plain dicts. Money is written as a string with at least two decimals (`Decimal("412.800000")` becomes `"412.80"`); quantities and decimal extras as strings without trailing zeros (`"12.5"`). Timestamps are written as UTC with `+00:00`; `from_dict` requires a timezone. A nullable field that is missing reads as `None`; a line with no `record_version` is a v1 line and is refused.
|
|
98
|
+
- `convert_v1(line, kind)` and `convert_v1_batch(path)` turn v1 lines (all Anthropic's) into the v2 lines `meterline-anthropic` writes; the v1 file is not changed. To upgrade a data directory, move the v1 files out of `records/<kind>/` and rename each `.v2.jsonl` to `<batch_id>.jsonl`; a connector reads every `*.jsonl` there as v2.
|
|
99
|
+
- `actor_cost_failures(usage, org_totals)` is the check that one actor's cost on a day never exceeds the organization's, applied only within one provider, org, scope, grain, day, `cost_basis` and currency, never counting `end_user` records.
|
|
100
|
+
- `latest_per_key` takes the record with the greatest `batch_id`. Batch ids sort chronologically (`20261009T060000Z`). On a tie, the later item wins.
|
|
101
|
+
- `write_parquet(records, path, cls=None)` writes Decimal columns as `decimal128(38, 12)`; the `quantities` and `extras` maps become Arrow maps (extras values as decimals). Pass `cls` to take the schema from an empty list. Without `pyarrow` it raises a `RuntimeError` that names the `parquet` extra.
|
|
102
|
+
|
|
103
|
+
## Known limits
|
|
104
|
+
|
|
105
|
+
The limits of the core package, grouped by kind.
|
|
106
|
+
|
|
107
|
+
**Reading records.** `from_dict`, and so `read_jsonl`, converts integer and boolean fields with Python's `int()` and `bool()` and does not reject other JSON types: a string `"false"` in a boolean field reads as true. Files written by meterline never hold such values; a hand-edited file or one from another writer can.
|
|
108
|
+
|
|
109
|
+
**Parquet.** Decimals are written as `decimal128(38, 12)`. The format keeps at most 12 decimals in a money or quantity figure (`meterline.MAX_DECIMALS`): `validate` refuses more, so a record that validates always writes. The JSON lines file holds every digit it was given.
|
|
110
|
+
|
|
111
|
+
**Revisions.** The format has no deletion marker. A record key that a source stops reporting keeps its last record as the latest, so a reader cannot tell a value that was removed from one that did not change (the LiteLLM README gives an example).
|
|
112
|
+
|
|
113
|
+
## Public interface
|
|
114
|
+
|
|
115
|
+
What later releases keep working, under the compatibility policy in [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md#compatibility):
|
|
116
|
+
|
|
117
|
+
- the record format: every field in [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md), and its JSON Schemas in [`docs/schemas/`](https://github.com/meyerx/meterline/blob/main/docs/schemas);
|
|
118
|
+
- the names in `meterline.__all__`, importable from the package root; the names the connectors import from `meterline.records`, `meterline.writers` and `meterline.http` stay importable from those three modules too. `meterline.http` holds the HTTP plumbing every connector shares (request throttle, retry back-off, `Retry-After` reading and capping, secret masking); it imports no HTTP library, so the core package keeps no dependencies.
|
|
119
|
+
|
|
120
|
+
Everything else is internal and may change in any release: every other module, and any name with a leading underscore.
|
|
121
|
+
|
|
122
|
+
## Tests
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
python -m pytest -q packages/meterline
|
|
126
|
+
```
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# meterline
|
|
2
|
+
|
|
3
|
+
> 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.
|
|
4
|
+
|
|
5
|
+
The common record format for AI usage data: record types, validation, and JSONL and Parquet writers. No network and no provider knowledge. Provider connectors, such as [`meterline-anthropic`](https://github.com/meyerx/meterline/blob/main/packages/meterline-anthropic/README.md), emit these records.
|
|
6
|
+
|
|
7
|
+
Python 3.10 or later. No runtime dependencies; `pyarrow` is an optional extra for Parquet.
|
|
8
|
+
|
|
9
|
+
**Status:** the v2 record format, its validation and its writers are built, and the connectors, the simulator and gold use them. See [Known limits](#known-limits).
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pip install meterline # core
|
|
13
|
+
pip install "meterline[parquet]" # adds pyarrow
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**From a clone** of the repository (for the tests and development):
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pip install -e packages/meterline # core
|
|
20
|
+
pip install -e "packages/meterline[parquet]" # adds pyarrow
|
|
21
|
+
pip install -e "packages/meterline[dev]" # adds the test tools
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
To try the format without a provider, generate invented records with the simulator and read them back:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pip install -e packages/meterline-sim
|
|
28
|
+
meterline-sim generate --preset tiny --seed 7 --out sim-out
|
|
29
|
+
python -c "import glob; from meterline import UsageRecord, read_jsonl; f = sorted(glob.glob('sim-out/*/records/usage/*.jsonl'))[0]; print(len(read_jsonl(f, UsageRecord)), 'usage records in', f)"
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Records
|
|
33
|
+
|
|
34
|
+
Five record classes, each with the key that identifies a record across batches:
|
|
35
|
+
|
|
36
|
+
| Class | Record key (used for revisions) |
|
|
37
|
+
|---|---|
|
|
38
|
+
| `UsageRecord` | provider, provider_account_id, scope, grain, period_start, actor_kind, actor_id, product, model |
|
|
39
|
+
| `ActivityRecord` | provider, provider_account_id, scope, grain, period_start, actor_kind, actor_id, product |
|
|
40
|
+
| `SeatSnapshot` | provider, provider_account_id, scope, period_start |
|
|
41
|
+
| `SeatAssignment` | provider, provider_account_id, scope, period_start, actor_id |
|
|
42
|
+
| `OrgTotal` | provider, provider_account_id, scope, grain, period_start, product, model |
|
|
43
|
+
|
|
44
|
+
All are frozen, keyword-only dataclasses; `record_version` is always 2. Dates are `date` (UTC, `period_end` inclusive), timestamps are timezone-aware `datetime`, amounts are `Decimal` or `None`. Every field, with its type, unit, nullability and meaning, is in the [record reference](https://github.com/meyerx/meterline/blob/main/docs/records.md); the code is `src/meterline/records.py`. The format is specified in section 3 of the [v2 spec](https://github.com/meyerx/meterline/blob/main/docs/dev/meterline-v2-SPEC-v1.0-2026-10-10.md).
|
|
45
|
+
|
|
46
|
+
## Use
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from datetime import date
|
|
50
|
+
from meterline import UsageRecord, batch_failures, latest_per_key, read_jsonl, validate, write_jsonl
|
|
51
|
+
|
|
52
|
+
validate(record, today=date.today()) # raises RecordError on any rule of spec 3.1 and 3.7 (grain, token sums, cost basis, future dates, negative numbers, ...)
|
|
53
|
+
batch_failures(records, today) # [] or messages: record rules plus duplicate keys and mixed org-total rows
|
|
54
|
+
write_jsonl(records, "records/usage/batch.jsonl") # sorted by key, keys sorted, LF line ends: deterministic bytes
|
|
55
|
+
rows = read_jsonl("records/usage/batch.jsonl", UsageRecord)
|
|
56
|
+
newest = latest_per_key(rows_from_all_batches) # {key: record}, latest batch_id wins
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Import from the package root: every public name is in `meterline.__all__`. Two submodule routes are also kept stable, because the connectors import from them: `meterline.records` (the five record classes, `validate`, `batch_failures`, `latest_per_key`) and `meterline.writers` (`read_jsonl`, `write_jsonl`, `write_parquet`). A name with a leading underscore is internal.
|
|
60
|
+
|
|
61
|
+
- `to_dict()` / `from_dict()` convert to and from plain dicts. Money is written as a string with at least two decimals (`Decimal("412.800000")` becomes `"412.80"`); quantities and decimal extras as strings without trailing zeros (`"12.5"`). Timestamps are written as UTC with `+00:00`; `from_dict` requires a timezone. A nullable field that is missing reads as `None`; a line with no `record_version` is a v1 line and is refused.
|
|
62
|
+
- `convert_v1(line, kind)` and `convert_v1_batch(path)` turn v1 lines (all Anthropic's) into the v2 lines `meterline-anthropic` writes; the v1 file is not changed. To upgrade a data directory, move the v1 files out of `records/<kind>/` and rename each `.v2.jsonl` to `<batch_id>.jsonl`; a connector reads every `*.jsonl` there as v2.
|
|
63
|
+
- `actor_cost_failures(usage, org_totals)` is the check that one actor's cost on a day never exceeds the organization's, applied only within one provider, org, scope, grain, day, `cost_basis` and currency, never counting `end_user` records.
|
|
64
|
+
- `latest_per_key` takes the record with the greatest `batch_id`. Batch ids sort chronologically (`20261009T060000Z`). On a tie, the later item wins.
|
|
65
|
+
- `write_parquet(records, path, cls=None)` writes Decimal columns as `decimal128(38, 12)`; the `quantities` and `extras` maps become Arrow maps (extras values as decimals). Pass `cls` to take the schema from an empty list. Without `pyarrow` it raises a `RuntimeError` that names the `parquet` extra.
|
|
66
|
+
|
|
67
|
+
## Known limits
|
|
68
|
+
|
|
69
|
+
The limits of the core package, grouped by kind.
|
|
70
|
+
|
|
71
|
+
**Reading records.** `from_dict`, and so `read_jsonl`, converts integer and boolean fields with Python's `int()` and `bool()` and does not reject other JSON types: a string `"false"` in a boolean field reads as true. Files written by meterline never hold such values; a hand-edited file or one from another writer can.
|
|
72
|
+
|
|
73
|
+
**Parquet.** Decimals are written as `decimal128(38, 12)`. The format keeps at most 12 decimals in a money or quantity figure (`meterline.MAX_DECIMALS`): `validate` refuses more, so a record that validates always writes. The JSON lines file holds every digit it was given.
|
|
74
|
+
|
|
75
|
+
**Revisions.** The format has no deletion marker. A record key that a source stops reporting keeps its last record as the latest, so a reader cannot tell a value that was removed from one that did not change (the LiteLLM README gives an example).
|
|
76
|
+
|
|
77
|
+
## Public interface
|
|
78
|
+
|
|
79
|
+
What later releases keep working, under the compatibility policy in [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md#compatibility):
|
|
80
|
+
|
|
81
|
+
- the record format: every field in [`docs/records.md`](https://github.com/meyerx/meterline/blob/main/docs/records.md), and its JSON Schemas in [`docs/schemas/`](https://github.com/meyerx/meterline/blob/main/docs/schemas);
|
|
82
|
+
- the names in `meterline.__all__`, importable from the package root; the names the connectors import from `meterline.records`, `meterline.writers` and `meterline.http` stay importable from those three modules too. `meterline.http` holds the HTTP plumbing every connector shares (request throttle, retry back-off, `Retry-After` reading and capping, secret masking); it imports no HTTP library, so the core package keeps no dependencies.
|
|
83
|
+
|
|
84
|
+
Everything else is internal and may change in any release: every other module, and any name with a leading underscore.
|
|
85
|
+
|
|
86
|
+
## Tests
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
python -m pytest -q packages/meterline
|
|
90
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "meterline"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "A common record format for AI usage data, with JSONL and Parquet writers."
|
|
9
|
+
license = "Apache-2.0"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
authors = [{ name = "Xavier Meyer" }]
|
|
13
|
+
keywords = ["ai", "llm", "usage", "cost", "tokens", "finops", "jsonl", "parquet"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Intended Audience :: System Administrators",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Topic :: System :: Monitoring",
|
|
24
|
+
]
|
|
25
|
+
requires-python = ">=3.10"
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/meyerx/meterline"
|
|
30
|
+
Source = "https://github.com/meyerx/meterline/tree/main/packages/meterline"
|
|
31
|
+
Issues = "https://github.com/meyerx/meterline/issues"
|
|
32
|
+
Changelog = "https://github.com/meyerx/meterline/blob/main/CHANGELOG.md"
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
parquet = ["pyarrow>=17"]
|
|
36
|
+
conformance = ["httpx>=0.27"] # the conformance kit builds fault transports; every connector already depends on httpx
|
|
37
|
+
dev = ["pytest>=8", "jsonschema>=4.18", "tomli>=2.0; python_version < '3.11'", "httpx>=0.27", "pytest-xdist==3.8.0", "ruff==0.17.3"]
|
|
38
|
+
[tool.setuptools.packages.find]
|
|
39
|
+
where = ["src"]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""meterline: a common record format for AI usage data (v2)."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError as _NotInstalled, version as _version
|
|
4
|
+
|
|
5
|
+
from meterline.convert import convert_v1, convert_v1_batch
|
|
6
|
+
from meterline.records import (
|
|
7
|
+
KINDS,
|
|
8
|
+
MAX_DECIMALS,
|
|
9
|
+
ActivityRecord,
|
|
10
|
+
OrgTotal,
|
|
11
|
+
RecordError,
|
|
12
|
+
SeatAssignment,
|
|
13
|
+
SeatSnapshot,
|
|
14
|
+
UsageRecord,
|
|
15
|
+
actor_cost_failures,
|
|
16
|
+
amount_str,
|
|
17
|
+
batch_failures,
|
|
18
|
+
grain_for,
|
|
19
|
+
latest_per_key,
|
|
20
|
+
quantity_str,
|
|
21
|
+
validate,
|
|
22
|
+
validate_batch,
|
|
23
|
+
)
|
|
24
|
+
from meterline.run import RunResult, note_failure, result_of_run, run_manifests
|
|
25
|
+
from meterline.writers import read_jsonl, write_jsonl, write_parquet
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
__version__ = _version("meterline") # the one source is pyproject.toml
|
|
29
|
+
except _NotInstalled: # a source tree that is not installed
|
|
30
|
+
__version__ = "0+unknown"
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"KINDS",
|
|
34
|
+
"MAX_DECIMALS",
|
|
35
|
+
"ActivityRecord",
|
|
36
|
+
"OrgTotal",
|
|
37
|
+
"RecordError",
|
|
38
|
+
"RunResult",
|
|
39
|
+
"SeatAssignment",
|
|
40
|
+
"SeatSnapshot",
|
|
41
|
+
"UsageRecord",
|
|
42
|
+
"actor_cost_failures",
|
|
43
|
+
"amount_str",
|
|
44
|
+
"batch_failures",
|
|
45
|
+
"convert_v1",
|
|
46
|
+
"convert_v1_batch",
|
|
47
|
+
"grain_for",
|
|
48
|
+
"latest_per_key",
|
|
49
|
+
"note_failure",
|
|
50
|
+
"quantity_str",
|
|
51
|
+
"read_jsonl",
|
|
52
|
+
"result_of_run",
|
|
53
|
+
"run_manifests",
|
|
54
|
+
"validate",
|
|
55
|
+
"validate_batch",
|
|
56
|
+
"write_jsonl",
|
|
57
|
+
"write_parquet",
|
|
58
|
+
"__version__",
|
|
59
|
+
]
|