spendgate 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. spendgate-0.1.0/LICENSE +202 -0
  2. spendgate-0.1.0/PKG-INFO +188 -0
  3. spendgate-0.1.0/README.md +158 -0
  4. spendgate-0.1.0/pyproject.toml +165 -0
  5. spendgate-0.1.0/src/spendgate/__init__.py +1 -0
  6. spendgate-0.1.0/src/spendgate/catalog/prices.yaml +342 -0
  7. spendgate-0.1.0/src/spendgate/catalog/schema.json +135 -0
  8. spendgate-0.1.0/src/spendgate/cli/__init__.py +5 -0
  9. spendgate-0.1.0/src/spendgate/cli/client.py +58 -0
  10. spendgate-0.1.0/src/spendgate/cli/commands.py +231 -0
  11. spendgate-0.1.0/src/spendgate/cli/entry.py +185 -0
  12. spendgate-0.1.0/src/spendgate/cli/render.py +26 -0
  13. spendgate-0.1.0/src/spendgate/cli/runtime.py +52 -0
  14. spendgate-0.1.0/src/spendgate/cli/serve.py +163 -0
  15. spendgate-0.1.0/src/spendgate/client/__init__.py +41 -0
  16. spendgate-0.1.0/src/spendgate/client/anthropic.py +64 -0
  17. spendgate-0.1.0/src/spendgate/client/errors.py +207 -0
  18. spendgate-0.1.0/src/spendgate/client/openai.py +64 -0
  19. spendgate-0.1.0/src/spendgate/client/session.py +158 -0
  20. spendgate-0.1.0/src/spendgate/config/__init__.py +17 -0
  21. spendgate-0.1.0/src/spendgate/config/load.py +407 -0
  22. spendgate-0.1.0/src/spendgate/config/schema.py +234 -0
  23. spendgate-0.1.0/src/spendgate/config/secrets.py +63 -0
  24. spendgate-0.1.0/src/spendgate/config/yamlio.py +37 -0
  25. spendgate-0.1.0/src/spendgate/core/__init__.py +1 -0
  26. spendgate-0.1.0/src/spendgate/core/audio.py +427 -0
  27. spendgate-0.1.0/src/spendgate/core/budgets.py +194 -0
  28. spendgate-0.1.0/src/spendgate/core/catalog.py +294 -0
  29. spendgate-0.1.0/src/spendgate/core/cost.py +240 -0
  30. spendgate-0.1.0/src/spendgate/core/errors.py +106 -0
  31. spendgate-0.1.0/src/spendgate/core/estimate.py +388 -0
  32. spendgate-0.1.0/src/spendgate/core/health.py +122 -0
  33. spendgate-0.1.0/src/spendgate/core/limits.py +206 -0
  34. spendgate-0.1.0/src/spendgate/core/money.py +191 -0
  35. spendgate-0.1.0/src/spendgate/core/outcomes.py +166 -0
  36. spendgate-0.1.0/src/spendgate/core/policy.py +199 -0
  37. spendgate-0.1.0/src/spendgate/core/sessions.py +128 -0
  38. spendgate-0.1.0/src/spendgate/gateway/__init__.py +1 -0
  39. spendgate-0.1.0/src/spendgate/gateway/admin.py +126 -0
  40. spendgate-0.1.0/src/spendgate/gateway/admin_budgets.py +172 -0
  41. spendgate-0.1.0/src/spendgate/gateway/admin_codec.py +149 -0
  42. spendgate-0.1.0/src/spendgate/gateway/admin_http.py +297 -0
  43. spendgate-0.1.0/src/spendgate/gateway/admin_models.py +131 -0
  44. spendgate-0.1.0/src/spendgate/gateway/admin_reports.py +166 -0
  45. spendgate-0.1.0/src/spendgate/gateway/admin_scopes.py +215 -0
  46. spendgate-0.1.0/src/spendgate/gateway/app.py +177 -0
  47. spendgate-0.1.0/src/spendgate/gateway/bootstrap.py +133 -0
  48. spendgate-0.1.0/src/spendgate/gateway/logs.py +92 -0
  49. spendgate-0.1.0/src/spendgate/gateway/maintenance.py +57 -0
  50. spendgate-0.1.0/src/spendgate/gateway/metrics.py +267 -0
  51. spendgate-0.1.0/src/spendgate/gateway/observability.py +135 -0
  52. spendgate-0.1.0/src/spendgate/gateway/wiring.py +163 -0
  53. spendgate-0.1.0/src/spendgate/ledger/__init__.py +1 -0
  54. spendgate-0.1.0/src/spendgate/ledger/admin_engine.py +87 -0
  55. spendgate-0.1.0/src/spendgate/ledger/admin_flow.py +360 -0
  56. spendgate-0.1.0/src/spendgate/ledger/admin_port.py +131 -0
  57. spendgate-0.1.0/src/spendgate/ledger/admin_reads.py +229 -0
  58. spendgate-0.1.0/src/spendgate/ledger/common.py +217 -0
  59. spendgate-0.1.0/src/spendgate/ledger/engine.py +299 -0
  60. spendgate-0.1.0/src/spendgate/ledger/hold_flow.py +298 -0
  61. spendgate-0.1.0/src/spendgate/ledger/ids.py +12 -0
  62. spendgate-0.1.0/src/spendgate/ledger/key_flow.py +318 -0
  63. spendgate-0.1.0/src/spendgate/ledger/key_types.py +102 -0
  64. spendgate-0.1.0/src/spendgate/ledger/keys.py +200 -0
  65. spendgate-0.1.0/src/spendgate/ledger/migrations/__init__.py +34 -0
  66. spendgate-0.1.0/src/spendgate/ledger/migrations/postgres/0001_init.sql +204 -0
  67. spendgate-0.1.0/src/spendgate/ledger/migrations/postgres/0002_usage.sql +5 -0
  68. spendgate-0.1.0/src/spendgate/ledger/migrations/postgres/0003_scope_display_name.sql +2 -0
  69. spendgate-0.1.0/src/spendgate/ledger/migrations/postgres/0004_calls_http_status_check.sql +3 -0
  70. spendgate-0.1.0/src/spendgate/ledger/migrations/sqlite/0001_init.sql +194 -0
  71. spendgate-0.1.0/src/spendgate/ledger/migrations/sqlite/0002_usage.sql +5 -0
  72. spendgate-0.1.0/src/spendgate/ledger/migrations/sqlite/0003_scope_display_name.sql +2 -0
  73. spendgate-0.1.0/src/spendgate/ledger/migrations/sqlite/0004_calls_http_status_check.sql +12 -0
  74. spendgate-0.1.0/src/spendgate/ledger/port.py +368 -0
  75. spendgate-0.1.0/src/spendgate/ledger/postgres.py +158 -0
  76. spendgate-0.1.0/src/spendgate/ledger/postgres_driver.py +109 -0
  77. spendgate-0.1.0/src/spendgate/ledger/rows.py +83 -0
  78. spendgate-0.1.0/src/spendgate/ledger/settle_flow.py +272 -0
  79. spendgate-0.1.0/src/spendgate/ledger/sql.py +365 -0
  80. spendgate-0.1.0/src/spendgate/ledger/sqlite.py +220 -0
  81. spendgate-0.1.0/src/spendgate/ledger/usage.py +290 -0
  82. spendgate-0.1.0/src/spendgate/ledger/usage_flow.py +104 -0
  83. spendgate-0.1.0/src/spendgate/local/__init__.py +16 -0
  84. spendgate-0.1.0/src/spendgate/local/bridge.py +107 -0
  85. spendgate-0.1.0/src/spendgate/local/guard.py +280 -0
  86. spendgate-0.1.0/src/spendgate/local/loop.py +44 -0
  87. spendgate-0.1.0/src/spendgate/local/principal.py +71 -0
  88. spendgate-0.1.0/src/spendgate/local/runtime.py +156 -0
  89. spendgate-0.1.0/src/spendgate/local/sdk.py +55 -0
  90. spendgate-0.1.0/src/spendgate/local/session.py +27 -0
  91. spendgate-0.1.0/src/spendgate/local/setup.py +82 -0
  92. spendgate-0.1.0/src/spendgate/local/transport.py +126 -0
  93. spendgate-0.1.0/src/spendgate/local/transport_httpx2.py +100 -0
  94. spendgate-0.1.0/src/spendgate/pipeline/__init__.py +1 -0
  95. spendgate-0.1.0/src/spendgate/pipeline/adapters.py +37 -0
  96. spendgate-0.1.0/src/spendgate/pipeline/answers.py +43 -0
  97. spendgate-0.1.0/src/spendgate/pipeline/auth.py +40 -0
  98. spendgate-0.1.0/src/spendgate/pipeline/closing.py +171 -0
  99. spendgate-0.1.0/src/spendgate/pipeline/intake.py +248 -0
  100. spendgate-0.1.0/src/spendgate/pipeline/leases.py +102 -0
  101. spendgate-0.1.0/src/spendgate/pipeline/model.py +174 -0
  102. spendgate-0.1.0/src/spendgate/pipeline/multipart.py +121 -0
  103. spendgate-0.1.0/src/spendgate/pipeline/pipeline.py +408 -0
  104. spendgate-0.1.0/src/spendgate/pipeline/replies.py +144 -0
  105. spendgate-0.1.0/src/spendgate/pipeline/routing.py +435 -0
  106. spendgate-0.1.0/src/spendgate/pipeline/settle.py +212 -0
  107. spendgate-0.1.0/src/spendgate/pipeline/steps.py +347 -0
  108. spendgate-0.1.0/src/spendgate/pipeline/streaming.py +335 -0
  109. spendgate-0.1.0/src/spendgate/pipeline/transport.py +98 -0
  110. spendgate-0.1.0/src/spendgate/pipeline/upkeep.py +46 -0
  111. spendgate-0.1.0/src/spendgate/pipeline/watch.py +48 -0
  112. spendgate-0.1.0/src/spendgate/providers/__init__.py +1 -0
  113. spendgate-0.1.0/src/spendgate/providers/anthropic.py +381 -0
  114. spendgate-0.1.0/src/spendgate/providers/azure_openai.py +146 -0
  115. spendgate-0.1.0/src/spendgate/providers/azure_openai_auth.py +74 -0
  116. spendgate-0.1.0/src/spendgate/providers/azure_speech.py +110 -0
  117. spendgate-0.1.0/src/spendgate/providers/azure_speech_shapes.py +216 -0
  118. spendgate-0.1.0/src/spendgate/providers/bedrock.py +231 -0
  119. spendgate-0.1.0/src/spendgate/providers/bedrock_request.py +239 -0
  120. spendgate-0.1.0/src/spendgate/providers/bedrock_stream.py +349 -0
  121. spendgate-0.1.0/src/spendgate/providers/elevenlabs.py +196 -0
  122. spendgate-0.1.0/src/spendgate/providers/google.py +326 -0
  123. spendgate-0.1.0/src/spendgate/providers/google_shapes.py +121 -0
  124. spendgate-0.1.0/src/spendgate/providers/google_stream.py +90 -0
  125. spendgate-0.1.0/src/spendgate/providers/openai.py +76 -0
  126. spendgate-0.1.0/src/spendgate/providers/openai_shapes.py +425 -0
  127. spendgate-0.1.0/src/spendgate/providers/openai_usage.py +143 -0
  128. spendgate-0.1.0/src/spendgate/providers/port.py +332 -0
  129. spendgate-0.1.0/src/spendgate/providers/sse.py +169 -0
  130. spendgate-0.1.0/src/spendgate/py.typed +0 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,188 @@
1
+ Metadata-Version: 2.4
2
+ Name: spendgate
3
+ Version: 0.1.0
4
+ Summary: A small, auditable LLM spend guard: SDK and proxy with hard budgets
5
+ Author: jmmfcoutinho
6
+ License-Expression: Apache-2.0
7
+ License-File: LICENSE
8
+ Requires-Dist: httpx>=0.28.1
9
+ Requires-Dist: pydantic>=2.14.3
10
+ Requires-Dist: pyyaml>=6.0.3
11
+ Requires-Dist: starlette>=1.10.0
12
+ Requires-Dist: uvicorn>=0.54.0
13
+ Requires-Dist: anthropic>=2.6.0 ; extra == 'anthropic'
14
+ Requires-Dist: azure-identity>=1.27.0 ; extra == 'azure'
15
+ Requires-Dist: botocore>=1.43.125 ; extra == 'bedrock'
16
+ Requires-Dist: google-auth>=2.62.1 ; extra == 'google'
17
+ Requires-Dist: requests>=2.34.2 ; extra == 'google'
18
+ Requires-Dist: openai>=3.46.0 ; extra == 'openai'
19
+ Requires-Dist: psycopg[binary,pool]>=3.3.8 ; extra == 'postgres'
20
+ Requires-Dist: tiktoken>=0.14.0 ; extra == 'tokenizer'
21
+ Requires-Python: >=3.13
22
+ Provides-Extra: anthropic
23
+ Provides-Extra: azure
24
+ Provides-Extra: bedrock
25
+ Provides-Extra: google
26
+ Provides-Extra: openai
27
+ Provides-Extra: postgres
28
+ Provides-Extra: tokenizer
29
+ Description-Content-Type: text/markdown
30
+
31
+ # spendgate
32
+
33
+ **Status: pre-1.0.** The configuration format, error codes and ledger schema may change between
34
+ minor versions. v0.1 targets local deployments: one machine, Docker Compose, SQLite or Postgres.
35
+
36
+ spendgate is a small, auditable guard for LLM spending: an OpenAI- and Anthropic-compatible proxy
37
+ plus a Python SDK and a no-server library mode. Before a call is forwarded it holds the worst-case
38
+ cost in every budget the call belongs to, refuses anything it cannot price, and settles the real
39
+ cost exactly once afterwards.
40
+
41
+ ## What it guarantees
42
+
43
+ [How money is protected](docs/how-money-is-protected.md) links each guarantee to the code and the
44
+ tests that prove it.
45
+
46
+ - **Fail closed.** An unknown or unpriced model, a missing exchange rate or output ceiling, an
47
+ unreachable ledger, a kill switch or a bad key refuses the call before the provider is contacted.
48
+ Price data cannot switch this off.
49
+ - **Hard holds.** The worst case is held before sending, in one transaction across the whole scope
50
+ chain, so `spent + held` never exceeds the cap plus recorded overruns. A call is free only if it
51
+ never reached the provider or the provider rejected it; anything else after sending is charged
52
+ the full hold unless usage was seen.
53
+ - **Overruns are recorded, never clamped.** Usage above the hold is booked as reported. A breach of
54
+ the cap blocks the budget until an operator acknowledges it (the default, `strict_overrun`).
55
+ - **Session limits.** A session id gets a budget, a step limit, a time limit and an idle expiry,
56
+ so a runaway loop ends with `session_limit_reached`.
57
+ - **Throttling costs nothing.** A provider 429 cools that deployment down and the call goes to the
58
+ next deployment of the same model inside the same reservation, at no charge.
59
+ - **A perimeter you can test.** In the reference Docker Compose setup only the proxy holds provider
60
+ keys and has internet access; apps on the internal network cannot reach a provider at all.
61
+
62
+ Library mode is cooperative: code that holds the provider key can skip it. Use the proxy with the
63
+ perimeter when something must be enforced. See [Running locally](docs/deploy/local.md).
64
+
65
+ ## Quickstart: proxy mode
66
+
67
+ Install with the SDK you use (`openai`, `anthropic`), then write a config. Providers, prices and
68
+ budgets are data; this one routes one model and gives it a 5 USD lifetime cap.
69
+
70
+ ```bash
71
+ pip install "spendgate[openai]"
72
+ ```
73
+
74
+ <!-- doctest: file spendgate.yaml -->
75
+ ```yaml
76
+ server: {listen: "127.0.0.1:8080", key_pepper: "env:SPENDGATE_KEY_PEPPER"}
77
+ ledger: {currency: USD, backend: {kind: sqlite, path: ./spendgate.db}}
78
+ providers:
79
+ openai: {base_url: "https://api.openai.com/v1", credential: "env:OPENAI_API_KEY"}
80
+ models:
81
+ gpt-6-luna: {catalog_id: openai/gpt-6-luna, deployments: [{provider: openai}]}
82
+ bootstrap:
83
+ scopes: ["org:demo"]
84
+ budgets: [{id: demo, scope: "org:demo", period: none, cap: "5 USD"}]
85
+ keys: [{name: quickstart, scope: "org:demo"}]
86
+ ```
87
+
88
+ Check it, create the virtual key, then start the proxy. The provider key stays here, in the
89
+ proxy's environment. `migrate --bootstrap` prints each key once; `serve` never prints a secret, so
90
+ none ends up in a container or service log.
91
+
92
+ <!-- doctest: shell -->
93
+ ```bash
94
+ export OPENAI_API_KEY=sk-... # your real provider key
95
+ export SPENDGATE_KEY_PEPPER=$(openssl rand -hex 32) # keep it: it protects stored keys
96
+ spendgate check-config --config spendgate.yaml
97
+ spendgate migrate --config spendgate.yaml --bootstrap
98
+ # bootstrap: created api key quickstart (org:demo): sg-... <- printed once; copy it
99
+ ```
100
+
101
+ <!-- doctest: serve -->
102
+ ```bash
103
+ spendgate serve --config spendgate.yaml
104
+ ```
105
+
106
+ In another terminal, call it with the virtual key (`sg-...`), never the provider key. The helper
107
+ sets the base URL and attribution headers; any unmodified OpenAI or Anthropic SDK also works.
108
+
109
+ <!-- doctest: python -->
110
+ ```python
111
+ import spendgate.client as sg
112
+
113
+ client = sg.openai("http://127.0.0.1:8080/v1", "sg-...", user="u-42", tags=["quickstart"])
114
+ with sg.raise_for_spendgate(): # BudgetExhausted, RateLimited, ModelNotPriced, ...
115
+ reply = client.chat.completions.create(
116
+ model="gpt-6-luna", max_tokens=50, messages=[{"role": "user", "content": "hi"}]
117
+ )
118
+ print(reply.choices[0].message.content)
119
+ ```
120
+
121
+ See who spent what:
122
+
123
+ <!-- doctest: shell -->
124
+ ```bash
125
+ spendgate usage --local --config spendgate.yaml --by model
126
+ ```
127
+
128
+ Next: [running locally with the perimeter](docs/deploy/local.md), [operations](docs/operations.md)
129
+ (keys, kill switch, backups) and the [configuration reference](docs/configuration.md).
130
+
131
+ ## Quickstart: library mode
132
+
133
+ No server and no key store: an `httpx` transport runs the same pipeline in your process against a
134
+ local SQLite ledger.
135
+
136
+ <!-- doctest: library -->
137
+ ```python
138
+ import spendgate.local as sg
139
+
140
+ client = sg.guard(cap="5 USD").openai() # needs OPENAI_API_KEY; ledger in your user data dir
141
+ client.chat.completions.create(model="gpt-6-luna", messages=[{"role": "user", "content": "hi"}])
142
+ ```
143
+
144
+ The cap lasts for the life of the ledger file and is shared by every script that opens it.
145
+ [Details and limits](docs/library-mode.md).
146
+
147
+ ## Feature matrix
148
+
149
+ Every row goes through the same hold, refusal and settlement rules. "stream" means the endpoint
150
+ streams and the stream is settled from its usage.
151
+
152
+ | Provider | Chat completions | Responses | Messages | Embeddings | Transcription | Speech |
153
+ |---|---|---|---|---|---|---|
154
+ | OpenAI | yes, stream | yes, stream | | yes | yes | yes, chunked |
155
+ | Azure OpenAI | yes, stream | yes, stream | | yes | yes | |
156
+ | Anthropic | | | yes, stream | | | |
157
+ | AWS Bedrock | yes, stream (translated) | | | | | |
158
+ | Google (Gemini API, Vertex AI) | yes, stream (translated) | | | | | |
159
+ | Azure Speech | | | | | yes | |
160
+ | ElevenLabs | | | | | | yes, chunked |
161
+
162
+ Endpoints are the OpenAI paths (`/v1/chat/completions`, `/v1/responses`, `/v1/embeddings`,
163
+ `/v1/audio/transcriptions`, `/v1/audio/speech`) and Anthropic's `/v1/messages`. Fields outside the
164
+ allowlist are refused rather than forwarded; see the [wire protocol](docs/protocol.md). Realtime
165
+ and hosted tools are not supported. Backends: SQLite (one host) and Postgres (shared).
166
+
167
+ ## Documentation
168
+
169
+ - [How money is protected](docs/how-money-is-protected.md): each guarantee with its code and test.
170
+ - [Documentation index](docs/README.md), [configuration reference](docs/configuration.md),
171
+ [operations](docs/operations.md).
172
+ - How to [add a model](docs/catalog-how-to.md) or [a provider](docs/provider-how-to.md).
173
+ - [spendgate and LiteLLM](docs/litellm-comparison.md): when to use which.
174
+ - [Roadmap](ROADMAP.md): what may come next.
175
+ - [Threat model](docs/threat-model.md), [contributing](CONTRIBUTING.md), [security policy](SECURITY.md).
176
+
177
+ ## Development
178
+
179
+ Requires [uv](https://docs.astral.sh/uv/) and Python 3.13 or newer.
180
+
181
+ ```bash
182
+ uv sync --all-extras
183
+ make check # every quality gate
184
+ ```
185
+
186
+ ## Licence
187
+
188
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,158 @@
1
+ # spendgate
2
+
3
+ **Status: pre-1.0.** The configuration format, error codes and ledger schema may change between
4
+ minor versions. v0.1 targets local deployments: one machine, Docker Compose, SQLite or Postgres.
5
+
6
+ spendgate is a small, auditable guard for LLM spending: an OpenAI- and Anthropic-compatible proxy
7
+ plus a Python SDK and a no-server library mode. Before a call is forwarded it holds the worst-case
8
+ cost in every budget the call belongs to, refuses anything it cannot price, and settles the real
9
+ cost exactly once afterwards.
10
+
11
+ ## What it guarantees
12
+
13
+ [How money is protected](docs/how-money-is-protected.md) links each guarantee to the code and the
14
+ tests that prove it.
15
+
16
+ - **Fail closed.** An unknown or unpriced model, a missing exchange rate or output ceiling, an
17
+ unreachable ledger, a kill switch or a bad key refuses the call before the provider is contacted.
18
+ Price data cannot switch this off.
19
+ - **Hard holds.** The worst case is held before sending, in one transaction across the whole scope
20
+ chain, so `spent + held` never exceeds the cap plus recorded overruns. A call is free only if it
21
+ never reached the provider or the provider rejected it; anything else after sending is charged
22
+ the full hold unless usage was seen.
23
+ - **Overruns are recorded, never clamped.** Usage above the hold is booked as reported. A breach of
24
+ the cap blocks the budget until an operator acknowledges it (the default, `strict_overrun`).
25
+ - **Session limits.** A session id gets a budget, a step limit, a time limit and an idle expiry,
26
+ so a runaway loop ends with `session_limit_reached`.
27
+ - **Throttling costs nothing.** A provider 429 cools that deployment down and the call goes to the
28
+ next deployment of the same model inside the same reservation, at no charge.
29
+ - **A perimeter you can test.** In the reference Docker Compose setup only the proxy holds provider
30
+ keys and has internet access; apps on the internal network cannot reach a provider at all.
31
+
32
+ Library mode is cooperative: code that holds the provider key can skip it. Use the proxy with the
33
+ perimeter when something must be enforced. See [Running locally](docs/deploy/local.md).
34
+
35
+ ## Quickstart: proxy mode
36
+
37
+ Install with the SDK you use (`openai`, `anthropic`), then write a config. Providers, prices and
38
+ budgets are data; this one routes one model and gives it a 5 USD lifetime cap.
39
+
40
+ ```bash
41
+ pip install "spendgate[openai]"
42
+ ```
43
+
44
+ <!-- doctest: file spendgate.yaml -->
45
+ ```yaml
46
+ server: {listen: "127.0.0.1:8080", key_pepper: "env:SPENDGATE_KEY_PEPPER"}
47
+ ledger: {currency: USD, backend: {kind: sqlite, path: ./spendgate.db}}
48
+ providers:
49
+ openai: {base_url: "https://api.openai.com/v1", credential: "env:OPENAI_API_KEY"}
50
+ models:
51
+ gpt-6-luna: {catalog_id: openai/gpt-6-luna, deployments: [{provider: openai}]}
52
+ bootstrap:
53
+ scopes: ["org:demo"]
54
+ budgets: [{id: demo, scope: "org:demo", period: none, cap: "5 USD"}]
55
+ keys: [{name: quickstart, scope: "org:demo"}]
56
+ ```
57
+
58
+ Check it, create the virtual key, then start the proxy. The provider key stays here, in the
59
+ proxy's environment. `migrate --bootstrap` prints each key once; `serve` never prints a secret, so
60
+ none ends up in a container or service log.
61
+
62
+ <!-- doctest: shell -->
63
+ ```bash
64
+ export OPENAI_API_KEY=sk-... # your real provider key
65
+ export SPENDGATE_KEY_PEPPER=$(openssl rand -hex 32) # keep it: it protects stored keys
66
+ spendgate check-config --config spendgate.yaml
67
+ spendgate migrate --config spendgate.yaml --bootstrap
68
+ # bootstrap: created api key quickstart (org:demo): sg-... <- printed once; copy it
69
+ ```
70
+
71
+ <!-- doctest: serve -->
72
+ ```bash
73
+ spendgate serve --config spendgate.yaml
74
+ ```
75
+
76
+ In another terminal, call it with the virtual key (`sg-...`), never the provider key. The helper
77
+ sets the base URL and attribution headers; any unmodified OpenAI or Anthropic SDK also works.
78
+
79
+ <!-- doctest: python -->
80
+ ```python
81
+ import spendgate.client as sg
82
+
83
+ client = sg.openai("http://127.0.0.1:8080/v1", "sg-...", user="u-42", tags=["quickstart"])
84
+ with sg.raise_for_spendgate(): # BudgetExhausted, RateLimited, ModelNotPriced, ...
85
+ reply = client.chat.completions.create(
86
+ model="gpt-6-luna", max_tokens=50, messages=[{"role": "user", "content": "hi"}]
87
+ )
88
+ print(reply.choices[0].message.content)
89
+ ```
90
+
91
+ See who spent what:
92
+
93
+ <!-- doctest: shell -->
94
+ ```bash
95
+ spendgate usage --local --config spendgate.yaml --by model
96
+ ```
97
+
98
+ Next: [running locally with the perimeter](docs/deploy/local.md), [operations](docs/operations.md)
99
+ (keys, kill switch, backups) and the [configuration reference](docs/configuration.md).
100
+
101
+ ## Quickstart: library mode
102
+
103
+ No server and no key store: an `httpx` transport runs the same pipeline in your process against a
104
+ local SQLite ledger.
105
+
106
+ <!-- doctest: library -->
107
+ ```python
108
+ import spendgate.local as sg
109
+
110
+ client = sg.guard(cap="5 USD").openai() # needs OPENAI_API_KEY; ledger in your user data dir
111
+ client.chat.completions.create(model="gpt-6-luna", messages=[{"role": "user", "content": "hi"}])
112
+ ```
113
+
114
+ The cap lasts for the life of the ledger file and is shared by every script that opens it.
115
+ [Details and limits](docs/library-mode.md).
116
+
117
+ ## Feature matrix
118
+
119
+ Every row goes through the same hold, refusal and settlement rules. "stream" means the endpoint
120
+ streams and the stream is settled from its usage.
121
+
122
+ | Provider | Chat completions | Responses | Messages | Embeddings | Transcription | Speech |
123
+ |---|---|---|---|---|---|---|
124
+ | OpenAI | yes, stream | yes, stream | | yes | yes | yes, chunked |
125
+ | Azure OpenAI | yes, stream | yes, stream | | yes | yes | |
126
+ | Anthropic | | | yes, stream | | | |
127
+ | AWS Bedrock | yes, stream (translated) | | | | | |
128
+ | Google (Gemini API, Vertex AI) | yes, stream (translated) | | | | | |
129
+ | Azure Speech | | | | | yes | |
130
+ | ElevenLabs | | | | | | yes, chunked |
131
+
132
+ Endpoints are the OpenAI paths (`/v1/chat/completions`, `/v1/responses`, `/v1/embeddings`,
133
+ `/v1/audio/transcriptions`, `/v1/audio/speech`) and Anthropic's `/v1/messages`. Fields outside the
134
+ allowlist are refused rather than forwarded; see the [wire protocol](docs/protocol.md). Realtime
135
+ and hosted tools are not supported. Backends: SQLite (one host) and Postgres (shared).
136
+
137
+ ## Documentation
138
+
139
+ - [How money is protected](docs/how-money-is-protected.md): each guarantee with its code and test.
140
+ - [Documentation index](docs/README.md), [configuration reference](docs/configuration.md),
141
+ [operations](docs/operations.md).
142
+ - How to [add a model](docs/catalog-how-to.md) or [a provider](docs/provider-how-to.md).
143
+ - [spendgate and LiteLLM](docs/litellm-comparison.md): when to use which.
144
+ - [Roadmap](ROADMAP.md): what may come next.
145
+ - [Threat model](docs/threat-model.md), [contributing](CONTRIBUTING.md), [security policy](SECURITY.md).
146
+
147
+ ## Development
148
+
149
+ Requires [uv](https://docs.astral.sh/uv/) and Python 3.13 or newer.
150
+
151
+ ```bash
152
+ uv sync --all-extras
153
+ make check # every quality gate
154
+ ```
155
+
156
+ ## Licence
157
+
158
+ [Apache-2.0](LICENSE).