zexl 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.
- zexl-0.1.0/.gitignore +29 -0
- zexl-0.1.0/LICENSE +202 -0
- zexl-0.1.0/PKG-INFO +177 -0
- zexl-0.1.0/README.md +157 -0
- zexl-0.1.0/pyproject.toml +69 -0
- zexl-0.1.0/src/zexl/__init__.py +103 -0
- zexl-0.1.0/src/zexl/_canon.py +193 -0
- zexl-0.1.0/src/zexl/_config.py +76 -0
- zexl-0.1.0/src/zexl/_context.py +210 -0
- zexl-0.1.0/src/zexl/_core.py +144 -0
- zexl-0.1.0/src/zexl/_export.py +368 -0
- zexl-0.1.0/src/zexl/_fingerprint.py +108 -0
- zexl-0.1.0/src/zexl/_hooks/__init__.py +21 -0
- zexl-0.1.0/src/zexl/_hooks/_anthropic.py +200 -0
- zexl-0.1.0/src/zexl/_hooks/_common.py +259 -0
- zexl-0.1.0/src/zexl/_hooks/_openai.py +204 -0
- zexl-0.1.0/src/zexl/_record.py +77 -0
- zexl-0.1.0/src/zexl/_stitch.py +81 -0
- zexl-0.1.0/src/zexl/auto.py +5 -0
- zexl-0.1.0/src/zexl/py.typed +0 -0
zexl-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
.mypy_cache/
|
|
7
|
+
.ruff_cache/
|
|
8
|
+
dist/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
|
|
11
|
+
# Node
|
|
12
|
+
node_modules/
|
|
13
|
+
.next/
|
|
14
|
+
out/
|
|
15
|
+
*.tsbuildinfo
|
|
16
|
+
next-env.d.ts
|
|
17
|
+
|
|
18
|
+
# Secrets and local config
|
|
19
|
+
.env
|
|
20
|
+
.env.*
|
|
21
|
+
!.env.example
|
|
22
|
+
|
|
23
|
+
# OS / editor
|
|
24
|
+
.DS_Store
|
|
25
|
+
.idea/
|
|
26
|
+
.vscode/
|
|
27
|
+
|
|
28
|
+
# Database backups (scripts/backup-restore-check.sh)
|
|
29
|
+
backups/
|
zexl-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
zexl-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: zexl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: See how much of your AI spend did useful work, without sending a single prompt.
|
|
5
|
+
Project-URL: Homepage, https://tryzexl.xyz
|
|
6
|
+
Author: Zexl
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: ai,anthropic,cost,llm,observability,openai
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# zexl (Python SDK)
|
|
22
|
+
|
|
23
|
+
Shows how much of your AI spend did useful work. It records metadata about each model call and sends it to Zexl. **Prompt and response text never leave your process.**
|
|
24
|
+
|
|
25
|
+
Status: early release (0.1.0). Tested against mocked provider responses only; not yet run against live provider APIs.
|
|
26
|
+
|
|
27
|
+
## Install and use
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install zexl
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
export ZEXL_API_KEY="zx_<id>_<secret>.<salt>"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import zexl.auto # add once, at your application's entry point
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
That is the whole setup. Calls made through the `openai` and `anthropic` packages are recorded from then on.
|
|
42
|
+
|
|
43
|
+
Optional:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import zexl
|
|
47
|
+
|
|
48
|
+
zexl.set_customer("acct_123") # attribute spend to one of your customers (use an id)
|
|
49
|
+
|
|
50
|
+
with zexl.run("research"): # group calls into one run explicitly
|
|
51
|
+
...
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@zexl.run() # or as a decorator
|
|
55
|
+
def summarise(doc): ...
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
with zexl.feature("checkout-bot"): # set the feature label without starting a run
|
|
59
|
+
...
|
|
60
|
+
|
|
61
|
+
zexl.flush() # before a short-lived process (serverless) exits
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Environment variable | Meaning | Default |
|
|
65
|
+
|---|---|---|
|
|
66
|
+
| `ZEXL_API_KEY` | Your key. Required | none |
|
|
67
|
+
| `ZEXL_ENV` | Environment tag for every call | `production` |
|
|
68
|
+
| `ZEXL_DISABLED` | `1` turns the SDK off completely | off |
|
|
69
|
+
| `ZEXL_ENDPOINT` | Intake URL | Zexl's hosted intake |
|
|
70
|
+
| `ZEXL_OPENAI_STREAM_USAGE` | `1` asks OpenAI for token usage on streamed chat completions (see below) | off |
|
|
71
|
+
|
|
72
|
+
## What leaves your process
|
|
73
|
+
|
|
74
|
+
Everything the SDK sends is built from a fixed allowlist (`src/zexl/_export.py`, `SCHEMA`). This is the complete list:
|
|
75
|
+
|
|
76
|
+
| Sent | Examples |
|
|
77
|
+
|---|---|
|
|
78
|
+
| Provider, API and model names | `openai`, `chat`, `gpt-4o` |
|
|
79
|
+
| Token counts reported by the provider | input, output, cache read, cache write, reasoning |
|
|
80
|
+
| Character counts | system prompt, tool definitions, input, output |
|
|
81
|
+
| Timings | start, end, time to first token |
|
|
82
|
+
| Outcome | finish reason, HTTP status, exception class name, whether a stream completed |
|
|
83
|
+
| Tool names | the `name` of each tool the model called, and how many tools were defined |
|
|
84
|
+
| Salted hashes | of the request, system prompt, tool definitions, newest input, output, and each tool call |
|
|
85
|
+
| Run id and step number | generated locally |
|
|
86
|
+
| Labels you set | feature, customer, environment |
|
|
87
|
+
| SDK details | SDK version, and an 8-character check value derived from your salt |
|
|
88
|
+
|
|
89
|
+
Never sent: message text, system prompts, tool descriptions and schemas, tool arguments, tool results, model output, images, exception messages, your provider API keys, and the salt.
|
|
90
|
+
|
|
91
|
+
Things to know:
|
|
92
|
+
|
|
93
|
+
- **Tool names and model names are sent as-is** when they look like identifiers. Do not put secrets in tool names.
|
|
94
|
+
- **The feature label defaults to `module.function`** of the code that made the call. Set it yourself with `zexl.feature()` if your function names are sensitive.
|
|
95
|
+
- **Lengths are sent.** Character and token counts reveal how long a prompt was, not what it said.
|
|
96
|
+
|
|
97
|
+
### How the hashes work
|
|
98
|
+
|
|
99
|
+
The part of your key after the dot is a salt. It stays in your process and is never transmitted. Every hash is an HMAC-SHA256 keyed with that salt, cut to 128 bits. Without the salt a hash cannot be matched to text, including by Zexl, and hashes from different workspaces cannot be compared. If you lose the key, the salt cannot be recovered; a new salt starts a new hash history.
|
|
100
|
+
|
|
101
|
+
### How this is tested
|
|
102
|
+
|
|
103
|
+
`tests/test_leak.py` plants canary strings in every place content can appear (system prompt, messages, images, tool definitions, tool arguments, tool results, responses, streamed chunks, error bodies), runs each supported call style, and fails if a canary or the salt appears in any byte the SDK sends. It also plants a deliberate leak to confirm the check would catch one.
|
|
104
|
+
|
|
105
|
+
## Behaviour you can rely on
|
|
106
|
+
|
|
107
|
+
- **Your calls are never blocked or changed.** Capture runs around the provider call; delivery happens on a background thread. If Zexl's intake is down, records are dropped and your application carries on.
|
|
108
|
+
- **Errors inside the SDK never reach your code.** They are logged once on the `zexl` logger at warning level.
|
|
109
|
+
- **Exceptions from the provider pass through untouched.**
|
|
110
|
+
- **Bounded memory.** At most 2,048 records wait for delivery; beyond that the SDK drops records.
|
|
111
|
+
- **Delivery is tried, not guaranteed.** Records go out every 2 seconds or every 256 records. A send that times out, loses its connection, or gets a 429 or 5xx is tried once more after 1 second. If that also fails, the batch is dropped and the SDK waits before the next send, from 2 seconds up to 60 seconds while the outage lasts.
|
|
112
|
+
- **No dependencies.** The SDK uses only the Python standard library.
|
|
113
|
+
|
|
114
|
+
`zexl.status()` reports whether capture is on, which hooks are active, and how many records were sent or dropped.
|
|
115
|
+
|
|
116
|
+
## Overhead
|
|
117
|
+
|
|
118
|
+
Measured with `bench/overhead.py` on a laptop (Python 3.12, 14 Nov 2026). The provider is a
|
|
119
|
+
mock inside the process, so these figures are the SDK's own work, not network time.
|
|
120
|
+
|
|
121
|
+
| Call | Time added per call (median) |
|
|
122
|
+
|---|---|
|
|
123
|
+
| Chat or messages call, 1 KB prompt | 0.05 ms |
|
|
124
|
+
| Async chat call, 1 KB prompt | 0.05 ms |
|
|
125
|
+
| Streamed chat call, 200 chunks | 0.32 ms for the whole stream |
|
|
126
|
+
| Agent step, 60 KB history and 10 tools | 0.23 ms |
|
|
127
|
+
| Agent step, 1 MB history | 1.4 ms |
|
|
128
|
+
|
|
129
|
+
Memory: a process that made 5,000 agent-step calls peaked 12 MB higher with the SDK on, and
|
|
130
|
+
16 MB higher when the intake was down the whole time. The queue holds at most 2,048 records;
|
|
131
|
+
beyond that the SDK drops records, so memory does not keep growing.
|
|
132
|
+
|
|
133
|
+
Sending happens on a background thread and never delays your call.
|
|
134
|
+
|
|
135
|
+
## What is recorded
|
|
136
|
+
|
|
137
|
+
| Provider | Covered |
|
|
138
|
+
|---|---|
|
|
139
|
+
| OpenAI | `chat.completions.create`, `.parse`, `.stream`; `responses.create`, `.parse`; streaming; `with_raw_response`; sync and async |
|
|
140
|
+
| Anthropic | `messages.create`, `.parse`, `.stream`; `beta.messages`; streaming; `with_raw_response`; sync and async |
|
|
141
|
+
|
|
142
|
+
Tested with `openai` 1.68 to 3.50 and `anthropic` 0.50 to 2.8, on Python 3.10 and 3.13.
|
|
143
|
+
|
|
144
|
+
Known limits:
|
|
145
|
+
|
|
146
|
+
- **Streamed OpenAI chat completions report no token usage** unless you pass `stream_options={"include_usage": True}`. Without it Zexl estimates cost from character counts. Setting `ZEXL_OPENAI_STREAM_USAGE=1` adds the option for you; it makes OpenAI send one extra final chunk with an empty `choices` list, so check your stream-handling code first.
|
|
147
|
+
- **`with_streaming_response` calls are recorded without response details**, because the SDK does not read the body ahead of you.
|
|
148
|
+
- **Retries inside the provider SDK are invisible.** A call that succeeded on its third attempt is one record.
|
|
149
|
+
- **Not yet covered:** the Bedrock, Vertex and Azure client variants are untested, and other providers and agent frameworks are not hooked.
|
|
150
|
+
|
|
151
|
+
### Runs
|
|
152
|
+
|
|
153
|
+
A run is one unit of agent work. Without any code from you, the SDK groups calls into a run when each request continues the previous response (the usual agent loop), or uses `previous_response_id`. If your application already has an OpenTelemetry trace active, the trace is the run.
|
|
154
|
+
|
|
155
|
+
Automatic grouping does not work when your agent trims or summarises its message history, or for sub-agents with their own conversations: those appear as separate runs. Wrap the work in `zexl.run()` to group it.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
uv sync
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
uv run ruff check . && uv run ruff format --check . && uv run mypy && uv run pytest
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Measure what the SDK adds to a call (time per call, peak memory), against a mock provider:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
uv run python bench/overhead.py
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Test against several provider SDK versions (from the repository root):
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
scripts/sdk-matrix.sh
|
|
177
|
+
```
|
zexl-0.1.0/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# zexl (Python SDK)
|
|
2
|
+
|
|
3
|
+
Shows how much of your AI spend did useful work. It records metadata about each model call and sends it to Zexl. **Prompt and response text never leave your process.**
|
|
4
|
+
|
|
5
|
+
Status: early release (0.1.0). Tested against mocked provider responses only; not yet run against live provider APIs.
|
|
6
|
+
|
|
7
|
+
## Install and use
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install zexl
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
export ZEXL_API_KEY="zx_<id>_<secret>.<salt>"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import zexl.auto # add once, at your application's entry point
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
That is the whole setup. Calls made through the `openai` and `anthropic` packages are recorded from then on.
|
|
22
|
+
|
|
23
|
+
Optional:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import zexl
|
|
27
|
+
|
|
28
|
+
zexl.set_customer("acct_123") # attribute spend to one of your customers (use an id)
|
|
29
|
+
|
|
30
|
+
with zexl.run("research"): # group calls into one run explicitly
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@zexl.run() # or as a decorator
|
|
35
|
+
def summarise(doc): ...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
with zexl.feature("checkout-bot"): # set the feature label without starting a run
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
zexl.flush() # before a short-lived process (serverless) exits
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| Environment variable | Meaning | Default |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `ZEXL_API_KEY` | Your key. Required | none |
|
|
47
|
+
| `ZEXL_ENV` | Environment tag for every call | `production` |
|
|
48
|
+
| `ZEXL_DISABLED` | `1` turns the SDK off completely | off |
|
|
49
|
+
| `ZEXL_ENDPOINT` | Intake URL | Zexl's hosted intake |
|
|
50
|
+
| `ZEXL_OPENAI_STREAM_USAGE` | `1` asks OpenAI for token usage on streamed chat completions (see below) | off |
|
|
51
|
+
|
|
52
|
+
## What leaves your process
|
|
53
|
+
|
|
54
|
+
Everything the SDK sends is built from a fixed allowlist (`src/zexl/_export.py`, `SCHEMA`). This is the complete list:
|
|
55
|
+
|
|
56
|
+
| Sent | Examples |
|
|
57
|
+
|---|---|
|
|
58
|
+
| Provider, API and model names | `openai`, `chat`, `gpt-4o` |
|
|
59
|
+
| Token counts reported by the provider | input, output, cache read, cache write, reasoning |
|
|
60
|
+
| Character counts | system prompt, tool definitions, input, output |
|
|
61
|
+
| Timings | start, end, time to first token |
|
|
62
|
+
| Outcome | finish reason, HTTP status, exception class name, whether a stream completed |
|
|
63
|
+
| Tool names | the `name` of each tool the model called, and how many tools were defined |
|
|
64
|
+
| Salted hashes | of the request, system prompt, tool definitions, newest input, output, and each tool call |
|
|
65
|
+
| Run id and step number | generated locally |
|
|
66
|
+
| Labels you set | feature, customer, environment |
|
|
67
|
+
| SDK details | SDK version, and an 8-character check value derived from your salt |
|
|
68
|
+
|
|
69
|
+
Never sent: message text, system prompts, tool descriptions and schemas, tool arguments, tool results, model output, images, exception messages, your provider API keys, and the salt.
|
|
70
|
+
|
|
71
|
+
Things to know:
|
|
72
|
+
|
|
73
|
+
- **Tool names and model names are sent as-is** when they look like identifiers. Do not put secrets in tool names.
|
|
74
|
+
- **The feature label defaults to `module.function`** of the code that made the call. Set it yourself with `zexl.feature()` if your function names are sensitive.
|
|
75
|
+
- **Lengths are sent.** Character and token counts reveal how long a prompt was, not what it said.
|
|
76
|
+
|
|
77
|
+
### How the hashes work
|
|
78
|
+
|
|
79
|
+
The part of your key after the dot is a salt. It stays in your process and is never transmitted. Every hash is an HMAC-SHA256 keyed with that salt, cut to 128 bits. Without the salt a hash cannot be matched to text, including by Zexl, and hashes from different workspaces cannot be compared. If you lose the key, the salt cannot be recovered; a new salt starts a new hash history.
|
|
80
|
+
|
|
81
|
+
### How this is tested
|
|
82
|
+
|
|
83
|
+
`tests/test_leak.py` plants canary strings in every place content can appear (system prompt, messages, images, tool definitions, tool arguments, tool results, responses, streamed chunks, error bodies), runs each supported call style, and fails if a canary or the salt appears in any byte the SDK sends. It also plants a deliberate leak to confirm the check would catch one.
|
|
84
|
+
|
|
85
|
+
## Behaviour you can rely on
|
|
86
|
+
|
|
87
|
+
- **Your calls are never blocked or changed.** Capture runs around the provider call; delivery happens on a background thread. If Zexl's intake is down, records are dropped and your application carries on.
|
|
88
|
+
- **Errors inside the SDK never reach your code.** They are logged once on the `zexl` logger at warning level.
|
|
89
|
+
- **Exceptions from the provider pass through untouched.**
|
|
90
|
+
- **Bounded memory.** At most 2,048 records wait for delivery; beyond that the SDK drops records.
|
|
91
|
+
- **Delivery is tried, not guaranteed.** Records go out every 2 seconds or every 256 records. A send that times out, loses its connection, or gets a 429 or 5xx is tried once more after 1 second. If that also fails, the batch is dropped and the SDK waits before the next send, from 2 seconds up to 60 seconds while the outage lasts.
|
|
92
|
+
- **No dependencies.** The SDK uses only the Python standard library.
|
|
93
|
+
|
|
94
|
+
`zexl.status()` reports whether capture is on, which hooks are active, and how many records were sent or dropped.
|
|
95
|
+
|
|
96
|
+
## Overhead
|
|
97
|
+
|
|
98
|
+
Measured with `bench/overhead.py` on a laptop (Python 3.12, 14 Nov 2026). The provider is a
|
|
99
|
+
mock inside the process, so these figures are the SDK's own work, not network time.
|
|
100
|
+
|
|
101
|
+
| Call | Time added per call (median) |
|
|
102
|
+
|---|---|
|
|
103
|
+
| Chat or messages call, 1 KB prompt | 0.05 ms |
|
|
104
|
+
| Async chat call, 1 KB prompt | 0.05 ms |
|
|
105
|
+
| Streamed chat call, 200 chunks | 0.32 ms for the whole stream |
|
|
106
|
+
| Agent step, 60 KB history and 10 tools | 0.23 ms |
|
|
107
|
+
| Agent step, 1 MB history | 1.4 ms |
|
|
108
|
+
|
|
109
|
+
Memory: a process that made 5,000 agent-step calls peaked 12 MB higher with the SDK on, and
|
|
110
|
+
16 MB higher when the intake was down the whole time. The queue holds at most 2,048 records;
|
|
111
|
+
beyond that the SDK drops records, so memory does not keep growing.
|
|
112
|
+
|
|
113
|
+
Sending happens on a background thread and never delays your call.
|
|
114
|
+
|
|
115
|
+
## What is recorded
|
|
116
|
+
|
|
117
|
+
| Provider | Covered |
|
|
118
|
+
|---|---|
|
|
119
|
+
| OpenAI | `chat.completions.create`, `.parse`, `.stream`; `responses.create`, `.parse`; streaming; `with_raw_response`; sync and async |
|
|
120
|
+
| Anthropic | `messages.create`, `.parse`, `.stream`; `beta.messages`; streaming; `with_raw_response`; sync and async |
|
|
121
|
+
|
|
122
|
+
Tested with `openai` 1.68 to 3.50 and `anthropic` 0.50 to 2.8, on Python 3.10 and 3.13.
|
|
123
|
+
|
|
124
|
+
Known limits:
|
|
125
|
+
|
|
126
|
+
- **Streamed OpenAI chat completions report no token usage** unless you pass `stream_options={"include_usage": True}`. Without it Zexl estimates cost from character counts. Setting `ZEXL_OPENAI_STREAM_USAGE=1` adds the option for you; it makes OpenAI send one extra final chunk with an empty `choices` list, so check your stream-handling code first.
|
|
127
|
+
- **`with_streaming_response` calls are recorded without response details**, because the SDK does not read the body ahead of you.
|
|
128
|
+
- **Retries inside the provider SDK are invisible.** A call that succeeded on its third attempt is one record.
|
|
129
|
+
- **Not yet covered:** the Bedrock, Vertex and Azure client variants are untested, and other providers and agent frameworks are not hooked.
|
|
130
|
+
|
|
131
|
+
### Runs
|
|
132
|
+
|
|
133
|
+
A run is one unit of agent work. Without any code from you, the SDK groups calls into a run when each request continues the previous response (the usual agent loop), or uses `previous_response_id`. If your application already has an OpenTelemetry trace active, the trace is the run.
|
|
134
|
+
|
|
135
|
+
Automatic grouping does not work when your agent trims or summarises its message history, or for sub-agents with their own conversations: those appear as separate runs. Wrap the work in `zexl.run()` to group it.
|
|
136
|
+
|
|
137
|
+
## Development
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
uv sync
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uv run ruff check . && uv run ruff format --check . && uv run mypy && uv run pytest
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Measure what the SDK adds to a call (time per call, peak memory), against a mock provider:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
uv run python bench/overhead.py
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Test against several provider SDK versions (from the repository root):
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
scripts/sdk-matrix.sh
|
|
157
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "zexl"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "See how much of your AI spend did useful work, without sending a single prompt."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "Zexl" }]
|
|
9
|
+
keywords = ["llm", "ai", "cost", "observability", "openai", "anthropic"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 4 - Beta",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Typing :: Typed",
|
|
19
|
+
]
|
|
20
|
+
requires-python = ">=3.10"
|
|
21
|
+
dependencies = []
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://tryzexl.xyz"
|
|
25
|
+
|
|
26
|
+
[dependency-groups]
|
|
27
|
+
dev = [
|
|
28
|
+
"pytest>=8",
|
|
29
|
+
"pytest-asyncio>=0.24",
|
|
30
|
+
"ruff>=0.8",
|
|
31
|
+
"mypy>=1.13",
|
|
32
|
+
"openai>=1.40",
|
|
33
|
+
"anthropic>=0.40",
|
|
34
|
+
"httpx",
|
|
35
|
+
"opentelemetry-api",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[build-system]
|
|
39
|
+
requires = ["hatchling"]
|
|
40
|
+
build-backend = "hatchling.build"
|
|
41
|
+
|
|
42
|
+
[tool.hatch.build.targets.wheel]
|
|
43
|
+
packages = ["src/zexl"]
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.sdist]
|
|
46
|
+
include = ["src", "README.md", "LICENSE"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff]
|
|
49
|
+
line-length = 100
|
|
50
|
+
target-version = "py310"
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = ["E", "F", "I", "B", "UP", "S"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint.isort]
|
|
56
|
+
known-first-party = ["zexl", "clients", "providers", "conftest"]
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint.per-file-ignores]
|
|
59
|
+
"tests/*" = ["S101", "S105", "S106", "S603", "E501", "E731"]
|
|
60
|
+
"bench/*" = ["S101", "S105", "S603"]
|
|
61
|
+
|
|
62
|
+
[tool.mypy]
|
|
63
|
+
strict = true
|
|
64
|
+
files = ["src"]
|
|
65
|
+
|
|
66
|
+
[tool.pytest.ini_options]
|
|
67
|
+
testpaths = ["tests"]
|
|
68
|
+
asyncio_mode = "auto"
|
|
69
|
+
filterwarnings = ["ignore::DeprecationWarning"]
|