llm-sketchkit 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.
- llm_sketchkit-0.1.0/LICENSE +159 -0
- llm_sketchkit-0.1.0/MANIFEST.in +4 -0
- llm_sketchkit-0.1.0/PKG-INFO +385 -0
- llm_sketchkit-0.1.0/README.md +352 -0
- llm_sketchkit-0.1.0/pyproject.toml +74 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/__init__.py +51 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/_proto.py +81 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/bloom.py +262 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/canon.py +57 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/frequentitems.py +370 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/hash.py +125 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/hashfamily.py +37 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/hllpp.py +421 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/hllpp_bias.py +1232 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/minhash.py +220 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/profiles.py +71 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/py.typed +1 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit/sketches_pb2.py +59 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit.egg-info/PKG-INFO +385 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit.egg-info/SOURCES.txt +55 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit.egg-info/dependency_links.txt +1 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit.egg-info/requires.txt +13 -0
- llm_sketchkit-0.1.0/python/llm_sketchkit.egg-info/top_level.txt +1 -0
- llm_sketchkit-0.1.0/scripts/__init__.py +3 -0
- llm_sketchkit-0.1.0/scripts/release_metadata.py +58 -0
- llm_sketchkit-0.1.0/setup.cfg +4 -0
- llm_sketchkit-0.1.0/tests/test_package.py +563 -0
- llm_sketchkit-0.1.0/tests/test_release_metadata.py +26 -0
- llm_sketchkit-0.1.0/vectors/hash/domain_mcp_method.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/domain_mcp_session.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/domain_prompt_same_input.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/domain_tool_error.json +20 -0
- llm_sketchkit-0.1.0/vectors/hash/domain_user_same_input.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/example_text_v1_hmac_sha256_64.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/text_v1_empty_after_trim.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/text_v1_trim_newline.json +19 -0
- llm_sketchkit-0.1.0/vectors/hash/text_v1_unicode_nfc.json +19 -0
- llm_sketchkit-0.1.0/vectors/oracles/datasketches_frequent_items.json +30 -0
- llm_sketchkit-0.1.0/vectors/schemas/hash_vector.schema.json +120 -0
- llm_sketchkit-0.1.0/vectors/schemas/sketch_vector.schema.json +157 -0
- llm_sketchkit-0.1.0/vectors/sketches/bloom_insertions.json +75 -0
- llm_sketchkit-0.1.0/vectors/sketches/bloom_merge.json +94 -0
- llm_sketchkit-0.1.0/vectors/sketches/cross_language_bloom.json +29 -0
- llm_sketchkit-0.1.0/vectors/sketches/cross_language_frequent_items.json +78 -0
- llm_sketchkit-0.1.0/vectors/sketches/cross_language_hllpp_dense.json +28 -0
- llm_sketchkit-0.1.0/vectors/sketches/cross_language_minhash.json +26 -0
- llm_sketchkit-0.1.0/vectors/sketches/example_empty_hllpp_small.json +21 -0
- llm_sketchkit-0.1.0/vectors/sketches/frequent_items_merge.json +70 -0
- llm_sketchkit-0.1.0/vectors/sketches/frequent_items_query_modes.json +57 -0
- llm_sketchkit-0.1.0/vectors/sketches/frequent_items_weighted_updates.json +55 -0
- llm_sketchkit-0.1.0/vectors/sketches/hllpp_merge_sparse.json +48 -0
- llm_sketchkit-0.1.0/vectors/sketches/hllpp_merge_sparse_dense.json +1318 -0
- llm_sketchkit-0.1.0/vectors/sketches/hllpp_promotion_boundary_dense.json +1053 -0
- llm_sketchkit-0.1.0/vectors/sketches/hllpp_promotion_boundary_sparse.json +1049 -0
- llm_sketchkit-0.1.0/vectors/sketches/hllpp_sparse_updates.json +51 -0
- llm_sketchkit-0.1.0/vectors/sketches/minhash_merge.json +118 -0
- llm_sketchkit-0.1.0/vectors/sketches/minhash_signature.json +100 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work
|
|
75
|
+
to which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the
|
|
102
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
103
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
104
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
|
105
|
+
the NOTICE file are for informational purposes only and do not modify the
|
|
106
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
107
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
108
|
+
provided that such additional attribution notices cannot be construed as
|
|
109
|
+
modifying the License.
|
|
110
|
+
|
|
111
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
112
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
113
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
114
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
115
|
+
with the conditions stated in this License.
|
|
116
|
+
|
|
117
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
118
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
119
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
120
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
121
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
122
|
+
executed with Licensor regarding such Contributions.
|
|
123
|
+
|
|
124
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
125
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
126
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
127
|
+
reproducing the content of the NOTICE file.
|
|
128
|
+
|
|
129
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
130
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
131
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
132
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
133
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
134
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
135
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
136
|
+
associated with Your exercise of permissions under this License.
|
|
137
|
+
|
|
138
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
139
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
140
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
141
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
142
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
143
|
+
arising as a result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
145
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
146
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
147
|
+
damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
150
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
151
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
152
|
+
and/or rights consistent with this License. However, in accepting such
|
|
153
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
154
|
+
responsibility, not on behalf of any other Contributor, and only if You agree to
|
|
155
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred
|
|
156
|
+
by, or claims asserted against, such Contributor by reason of your accepting any
|
|
157
|
+
such warranty or additional liability.
|
|
158
|
+
|
|
159
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llm-sketchkit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministic Go and Python sketches for high-cardinality LLM data.
|
|
5
|
+
Author: Vijay Erramilli, Codex
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Documentation, https://github.com/llm-measurement/llm-sketchkit#readme
|
|
8
|
+
Project-URL: Issues, https://github.com/llm-measurement/llm-sketchkit/issues
|
|
9
|
+
Project-URL: Source, https://github.com/llm-measurement/llm-sketchkit
|
|
10
|
+
Keywords: sketches,hyperloglog,minhash,bloom-filter,streaming
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: protobuf<7,>=6.33.5
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
27
|
+
Provides-Extra: oracle
|
|
28
|
+
Requires-Dist: datasketches==5.2.0; extra == "oracle"
|
|
29
|
+
Provides-Extra: release
|
|
30
|
+
Requires-Dist: build==1.5.0; extra == "release"
|
|
31
|
+
Requires-Dist: twine==6.2.0; extra == "release"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# llm-sketchkit
|
|
35
|
+
|
|
36
|
+
[](https://github.com/llm-measurement/llm-sketchkit/actions/workflows/ci.yml)
|
|
37
|
+
|
|
38
|
+
`llm-sketchkit` provides continuous, bounded answers about high-cardinality agent
|
|
39
|
+
traffic without exporting or indexing every underlying value. It is a small Go and
|
|
40
|
+
Python library with matching semantics for text canonicalization, privacy-preserving
|
|
41
|
+
keyed hashes, mergeable sketches, and a deterministic protobuf wire format.
|
|
42
|
+
|
|
43
|
+
It provides bounded measurement primitives for AI agent observability pipelines,
|
|
44
|
+
especially when exporting and indexing every identity or event would be expensive,
|
|
45
|
+
slow to query, or inappropriate to retain.
|
|
46
|
+
|
|
47
|
+
Raw prompts and identifiers do not need to enter sketch state. Producers can
|
|
48
|
+
summarize locally and merge compatible sketches across processes or languages.
|
|
49
|
+
|
|
50
|
+
## When This Fits
|
|
51
|
+
|
|
52
|
+
Use `llm-sketchkit` inside telemetry producers and processing components when
|
|
53
|
+
exporting and indexing every key would create uncontrolled cardinality, make
|
|
54
|
+
operational queries slow or unpredictable, or retain values that should not enter
|
|
55
|
+
aggregate state.
|
|
56
|
+
|
|
57
|
+
Agent fleets and multi-agent systems can produce more identities and events than
|
|
58
|
+
an observability backend should continuously index. The library provides bounded,
|
|
59
|
+
mergeable summaries across workers and windows.
|
|
60
|
+
|
|
61
|
+
Questions it can help answer include:
|
|
62
|
+
|
|
63
|
+
- **How many distinct prompts, users, sessions, tools, or documents are active
|
|
64
|
+
without keeping one counter per value?** HLL++ provides a bounded estimate.
|
|
65
|
+
- **Your token budget is climbing and FinOps wants to know which configured
|
|
66
|
+
identities account for the reported volume. How certain is the answer?** Weighted
|
|
67
|
+
frequent-items identifies token-heavy or request-heavy keys with deterministic
|
|
68
|
+
lower and upper bounds.
|
|
69
|
+
- **Have we already observed this request or document without maintaining an exact
|
|
70
|
+
set of every value?** Bloom filters provide bounded approximate membership checks.
|
|
71
|
+
- **Did the prompt, tool, or retrieval-document population change materially after
|
|
72
|
+
a deployment or model change?** MinHash compares large sets using bounded
|
|
73
|
+
similarity signatures.
|
|
74
|
+
- **Can Go services summarize locally while Python analysis jobs read and merge the
|
|
75
|
+
same state?** Shared profiles, fixtures, and wire semantics keep the two
|
|
76
|
+
implementations compatible.
|
|
77
|
+
|
|
78
|
+
For investigations described as "tokenmaxxing" (also written "token-maxing"),
|
|
79
|
+
reported token counts can be used as weights in the frequent-items sketch to identify
|
|
80
|
+
which keyed values account for the most token volume. The library measures
|
|
81
|
+
concentration; it does not infer task value, enforce budgets, or stop agent loops.
|
|
82
|
+
See [Token-Volume Heavy Hitters](#token-volume-heavy-hitters) for a runnable example.
|
|
83
|
+
|
|
84
|
+
Inputs can be canonicalized and keyed before entering sketch state. This keeps
|
|
85
|
+
raw values out of the sketch, but the resulting hashes remain pseudonymous and
|
|
86
|
+
linkable while the same secret is in use.
|
|
87
|
+
|
|
88
|
+
This is a sketch library, not a trace collector, sampling processor, storage
|
|
89
|
+
backend, dashboard, or differential-privacy system. The [FAQ](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md)
|
|
90
|
+
answers common questions about fit, operational bounds, accuracy, and
|
|
91
|
+
interoperability.
|
|
92
|
+
|
|
93
|
+
## Choosing An Integration
|
|
94
|
+
|
|
95
|
+
| Your pipeline | Use | Why |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| GenAI spans already flow through an OpenTelemetry Collector | [OpenTelemetry Collector connector](https://github.com/llm-measurement/otelcol-genai-sketches) | It applies keyed hashing, bounded windows, cardinality controls, and trace-to-metrics conversion at the collector boundary. |
|
|
98
|
+
| A custom Go or Python streaming service processes events | `llm-sketchkit` directly | Update sketches inside each bounded window, then serialize or merge compatible summaries. |
|
|
99
|
+
| A batch or warehouse job reads stored events | `llm-sketchkit` directly | Build bounded summaries per partition or window and merge them before publishing results. |
|
|
100
|
+
| You only need to store or visualize finished metrics | Your existing backend integration | The library is not an exporter; ClickHouse, Datadog, Prometheus, and similar systems normally receive the aggregated results. |
|
|
101
|
+
|
|
102
|
+
Use the collector path when the source spans already flow through OpenTelemetry. Use
|
|
103
|
+
the library when you own the event-processing code or need matching Go and Python
|
|
104
|
+
summaries outside an OpenTelemetry pipeline. In either case, compatible producers
|
|
105
|
+
must agree on profile, hash domain, hash algorithm, secret, and window boundaries.
|
|
106
|
+
|
|
107
|
+
## Included Sketches
|
|
108
|
+
|
|
109
|
+
| Component | Use it for | Important property |
|
|
110
|
+
|---|---|---|
|
|
111
|
+
| HLL++ | Approximate distinct counts | Bounded, mergeable state |
|
|
112
|
+
| Weighted frequent-items | Heavy hitters and top items | Deterministic lower and upper bounds |
|
|
113
|
+
| Bloom filter | Set membership | No false negatives; configurable false-positive rate |
|
|
114
|
+
| MinHash | Approximate Jaccard similarity | Bounded, mergeable signatures |
|
|
115
|
+
|
|
116
|
+
The Go and Python implementations share the same profiles, hash domains, test
|
|
117
|
+
vectors, and serialized representation.
|
|
118
|
+
|
|
119
|
+
## Evidence At A Glance
|
|
120
|
+
|
|
121
|
+
Measurements use deterministic workloads and report the least favorable of five
|
|
122
|
+
Linux benchmark runs where applicable.
|
|
123
|
+
|
|
124
|
+
- HMAC-SHA256-64 sustained at least **1.65 million 64-byte inputs/s/core** and
|
|
125
|
+
**850,340 1 KiB inputs/s/core** on an Intel Xeon Platinum 8573C with Go 1.26.5.
|
|
126
|
+
- HLL++ and weighted frequent-items updates took at most **10.58 ns/op** and
|
|
127
|
+
**145.6 ns/op**, respectively, with **0 allocations/op** in the measured paths.
|
|
128
|
+
- The HLL++ `small` profile's maximum observed relative error was **2.3301%**
|
|
129
|
+
across the characterization grid, within its **2.4375%** enforced bound.
|
|
130
|
+
- Bloom profile false-positive rates were at or below their configured targets
|
|
131
|
+
in the measured trials, with zero false negatives among inserted hashes.
|
|
132
|
+
- MinHash mean absolute error fell from **0.02845** at `k=128` to **0.02009** at
|
|
133
|
+
`k=256`, closely following the expected inverse-square-root relationship.
|
|
134
|
+
- Both weighted frequent-items oracle workloads retained **100% true top-20
|
|
135
|
+
recall** and valid no-false-positive query results in both implementations.
|
|
136
|
+
|
|
137
|
+
See the [visual scorecard](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/scorecard.md),
|
|
138
|
+
[raw measurement records](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/README.md),
|
|
139
|
+
and [general-purpose library comparison](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/DATASKETCHES.md)
|
|
140
|
+
for methods, limitations, and reproduction commands.
|
|
141
|
+
|
|
142
|
+
## Requirements
|
|
143
|
+
|
|
144
|
+
- Go 1.25 or newer, with the latest security patch for that release line
|
|
145
|
+
- Python 3.11 or newer
|
|
146
|
+
|
|
147
|
+
## Install
|
|
148
|
+
|
|
149
|
+
### Python
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
python -m pip install llm-sketchkit
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Generate a process secret:
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
export LLM_SKETCHKIT_SECRET="$(python -c 'import secrets; print(secrets.token_hex(32))')"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Run a distinct-count example:
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
python - <<'PY'
|
|
165
|
+
from llm_sketchkit import PROMPT_V1, canonicalize_text_v1, hash64, hllpp
|
|
166
|
+
from llm_sketchkit import secret_from_env
|
|
167
|
+
|
|
168
|
+
secret = secret_from_env("LLM_SKETCHKIT_SECRET")
|
|
169
|
+
sketch = hllpp.new("small", PROMPT_V1)
|
|
170
|
+
|
|
171
|
+
canonical = canonicalize_text_v1(" cafe\u0301\r\n")
|
|
172
|
+
sketch.add_hash(hash64(secret, PROMPT_V1, canonical))
|
|
173
|
+
|
|
174
|
+
print(f"estimated distinct prompts: {sketch.estimate():.0f}")
|
|
175
|
+
PY
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Expected output:
|
|
179
|
+
|
|
180
|
+
```text
|
|
181
|
+
estimated distinct prompts: 1
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Go
|
|
185
|
+
|
|
186
|
+
From an existing Go module, add the package used by the equivalent example:
|
|
187
|
+
|
|
188
|
+
```sh
|
|
189
|
+
go get github.com/llm-measurement/llm-sketchkit/go/sketchkit/hllpp@latest
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Use the same `LLM_SKETCHKIT_SECRET` and run:
|
|
193
|
+
|
|
194
|
+
```go
|
|
195
|
+
package main
|
|
196
|
+
|
|
197
|
+
import (
|
|
198
|
+
"fmt"
|
|
199
|
+
"log"
|
|
200
|
+
|
|
201
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/canon"
|
|
202
|
+
sketchhash "github.com/llm-measurement/llm-sketchkit/go/sketchkit/hash"
|
|
203
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/hllpp"
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
func main() {
|
|
207
|
+
secret, err := sketchhash.SecretFromEnv("LLM_SKETCHKIT_SECRET")
|
|
208
|
+
if err != nil {
|
|
209
|
+
log.Fatal(err)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
sketch, err := hllpp.New(
|
|
213
|
+
hllpp.ProfileSmall,
|
|
214
|
+
sketchhash.PromptV1,
|
|
215
|
+
sketchhash.HMACSHA25664,
|
|
216
|
+
)
|
|
217
|
+
if err != nil {
|
|
218
|
+
log.Fatal(err)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
canonical, err := canon.CanonicalizeString(canon.TextV1, " cafe\u0301\r\n")
|
|
222
|
+
if err != nil {
|
|
223
|
+
log.Fatal(err)
|
|
224
|
+
}
|
|
225
|
+
digest, err := sketchhash.Hash64(secret, sketchhash.PromptV1, canonical)
|
|
226
|
+
if err != nil {
|
|
227
|
+
log.Fatal(err)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
sketch.AddHash(digest)
|
|
231
|
+
fmt.Printf("estimated distinct prompts: %.0f\n", sketch.Estimate())
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Go To Python Notebook
|
|
236
|
+
|
|
237
|
+
The [runnable notebook](https://github.com/llm-measurement/llm-sketchkit/blob/main/examples/go-to-python/go-to-python.ipynb) produces
|
|
238
|
+
per-service HLL++ and weighted frequent-items summaries in Go, then loads,
|
|
239
|
+
validates, merges, and plots them in Python. It checks that serialization round trips
|
|
240
|
+
return the same bytes,
|
|
241
|
+
explicit merge rejection for incompatible profiles, distinct-count estimates,
|
|
242
|
+
and deterministic bounds around token-heavy pseudonymous keys.
|
|
243
|
+
|
|
244
|
+
The tutorial uses an exact synthetic side channel only to check its estimates.
|
|
245
|
+
Raw synthetic identifiers do not enter the emitted files. See the
|
|
246
|
+
[example guide](https://github.com/llm-measurement/llm-sketchkit/blob/main/examples/go-to-python/README.md)
|
|
247
|
+
for setup and security details.
|
|
248
|
+
|
|
249
|
+
## Token-Volume Heavy Hitters
|
|
250
|
+
|
|
251
|
+
If by "tokenmaxxing" (also written "token-maxing") you mean unexpected or runaway
|
|
252
|
+
token consumption, use a bounded frequent-items sketch to find where reported volume
|
|
253
|
+
is concentrated. Hash the value being investigated, such as a prompt template, tool,
|
|
254
|
+
or user, with the registered domain for that entity class, and use the reported token
|
|
255
|
+
count as its weight. This example measures prompt templates with `prompt:v1`:
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
from llm_sketchkit import PROMPT_V1, canonicalize_text_v1, frequentitems
|
|
259
|
+
from llm_sketchkit import hash64, secret_from_env
|
|
260
|
+
|
|
261
|
+
secret = secret_from_env("LLM_SKETCHKIT_SECRET")
|
|
262
|
+
sketch = frequentitems.Sketch("small", PROMPT_V1)
|
|
263
|
+
|
|
264
|
+
events = [
|
|
265
|
+
("support/refund", 1_240),
|
|
266
|
+
("research/synthesis", 8_900),
|
|
267
|
+
("support/refund", 980),
|
|
268
|
+
("coding/review", 3_600),
|
|
269
|
+
]
|
|
270
|
+
|
|
271
|
+
for prompt_template, reported_tokens in events:
|
|
272
|
+
canonical = canonicalize_text_v1(prompt_template)
|
|
273
|
+
digest = hash64(secret, PROMPT_V1, canonical)
|
|
274
|
+
sketch.add_hash(digest, reported_tokens)
|
|
275
|
+
|
|
276
|
+
for item in sketch.frequent_items(frequentitems.NO_FALSE_NEGATIVES)[:10]:
|
|
277
|
+
print(
|
|
278
|
+
f"{item.hash:016x} estimate={item.estimate} "
|
|
279
|
+
f"bounds=[{item.lower_bound}, {item.upper_bound}]"
|
|
280
|
+
)
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
The sketch retains at most the selected profile's bounded map size. Returned hashes
|
|
284
|
+
are pseudonymous and remain linkable while the same secret and domain are in use.
|
|
285
|
+
The estimate is not a billing total; use the lower and upper bounds when deciding
|
|
286
|
+
whether an item is meaningfully heavy.
|
|
287
|
+
|
|
288
|
+
Do not substitute guessed weights when token usage is missing. Count missing usage
|
|
289
|
+
separately so operators know how complete the reported totals are. For an OTLP
|
|
290
|
+
pipeline with ready-made metrics, bounded slices, missing-usage accounting, and
|
|
291
|
+
token-weighted top-k snapshots, use
|
|
292
|
+
[`otelcol-genai-sketches`](https://github.com/llm-measurement/otelcol-genai-sketches).
|
|
293
|
+
|
|
294
|
+
## Merge Sketches
|
|
295
|
+
|
|
296
|
+
Sketches merge only when their kind, profile, hash domain, hash algorithm, and
|
|
297
|
+
shape metadata match. A mismatch is an error rather than an implicit conversion.
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
left = hllpp.new("small", PROMPT_V1)
|
|
301
|
+
right = hllpp.new("small", PROMPT_V1)
|
|
302
|
+
|
|
303
|
+
left.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("alpha")))
|
|
304
|
+
right.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("beta")))
|
|
305
|
+
|
|
306
|
+
left.merge(right)
|
|
307
|
+
print(f"merged distinct prompts: {left.estimate():.0f}")
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Security And Privacy
|
|
311
|
+
|
|
312
|
+
- Hash inputs with a registered domain and a high-entropy secret before adding
|
|
313
|
+
them to a sketch. The built-in secret loaders require at least 16 bytes and
|
|
314
|
+
reject known placeholder values.
|
|
315
|
+
- Keyed hashes are pseudonymous, not anonymous. Anyone with the secret can test
|
|
316
|
+
candidate values, and repeated hashes remain linkable while the same secret
|
|
317
|
+
and domain are in use.
|
|
318
|
+
- Never log, serialize, or commit the hash secret. Rotate it when the trust
|
|
319
|
+
boundary changes; rotation intentionally breaks comparison with older state.
|
|
320
|
+
- Bound raw input size before canonicalization. Canonicalization operates on
|
|
321
|
+
in-memory text and intentionally leaves application-specific limits to callers.
|
|
322
|
+
- Sketches reveal bounded aggregate information and may reveal membership or
|
|
323
|
+
recurrence. They do not provide differential privacy.
|
|
324
|
+
- Treat serialized sketches as untrusted input at process boundaries. The parse
|
|
325
|
+
APIs cap input size and reject invalid profiles, domains, shapes, counters, and
|
|
326
|
+
register values.
|
|
327
|
+
|
|
328
|
+
See [SECURITY.md](https://github.com/llm-measurement/llm-sketchkit/blob/main/SECURITY.md)
|
|
329
|
+
for private vulnerability reporting.
|
|
330
|
+
|
|
331
|
+
## Development
|
|
332
|
+
|
|
333
|
+
```sh
|
|
334
|
+
git clone https://github.com/llm-measurement/llm-sketchkit.git
|
|
335
|
+
cd llm-sketchkit
|
|
336
|
+
python -m venv .venv
|
|
337
|
+
source .venv/bin/activate
|
|
338
|
+
python -m pip install --upgrade pip==26.2.1 setuptools==83.0.0
|
|
339
|
+
python -m pip install -e '.[dev]'
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Wire Compatibility
|
|
343
|
+
|
|
344
|
+
Deterministic protobuf encoding is part of the compatibility surface. Go and
|
|
345
|
+
Python are checked against the same canonicalization, hashing, sketch, and
|
|
346
|
+
cross-language merge fixtures in
|
|
347
|
+
[`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/).
|
|
348
|
+
|
|
349
|
+
Run all local checks:
|
|
350
|
+
|
|
351
|
+
```sh
|
|
352
|
+
go test ./... -race
|
|
353
|
+
python -m pytest -q
|
|
354
|
+
ruff check .
|
|
355
|
+
mypy --strict
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
The optional Apache DataSketches comparison checks weighted frequent-items
|
|
359
|
+
query behavior against an independent implementation:
|
|
360
|
+
|
|
361
|
+
```sh
|
|
362
|
+
python -m pip install -e '.[oracle]'
|
|
363
|
+
python scripts/datasketches_oracle.py --check
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Reference
|
|
367
|
+
|
|
368
|
+
- [`spec/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/spec/) defines canonicalization, hashing, profiles, and wire encoding.
|
|
369
|
+
- [`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/) contains executable conformance fixtures.
|
|
370
|
+
- [`reports/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/reports/) contains benchmark, accuracy, and oracle results.
|
|
371
|
+
- [`bench/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/bench/) contains the Go benchmark harnesses.
|
|
372
|
+
- [`docs/FAQ.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md) answers common adoption questions.
|
|
373
|
+
- [`CHANGELOG.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/CHANGELOG.md) records release-level changes.
|
|
374
|
+
|
|
375
|
+
## Status
|
|
376
|
+
|
|
377
|
+
`llm-sketchkit 0.1.x` is a supported release line. Patch releases preserve the
|
|
378
|
+
documented wire formats, named hash domains, and Go and Python APIs exercised by
|
|
379
|
+
the checked-in conformance vectors. Additive or incompatible changes to that
|
|
380
|
+
surface receive a new minor version and are called out in the changelog before
|
|
381
|
+
`1.0`.
|
|
382
|
+
|
|
383
|
+
## License
|
|
384
|
+
|
|
385
|
+
Apache-2.0.
|