tragac 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.
- tragac-0.1.0/MANIFEST.in +2 -0
- tragac-0.1.0/PKG-INFO +177 -0
- tragac-0.1.0/README.md +166 -0
- tragac-0.1.0/pyproject.toml +23 -0
- tragac-0.1.0/setup.cfg +4 -0
- tragac-0.1.0/tragac/__init__.py +15 -0
- tragac-0.1.0/tragac/cli.py +606 -0
- tragac-0.1.0/tragac/contract.py +104 -0
- tragac-0.1.0/tragac/transport.py +65 -0
- tragac-0.1.0/tragac/usage.py +64 -0
- tragac-0.1.0/tragac/wrap.py +553 -0
- tragac-0.1.0/tragac.egg-info/PKG-INFO +177 -0
- tragac-0.1.0/tragac.egg-info/SOURCES.txt +14 -0
- tragac-0.1.0/tragac.egg-info/dependency_links.txt +1 -0
- tragac-0.1.0/tragac.egg-info/entry_points.txt +2 -0
- tragac-0.1.0/tragac.egg-info/top_level.txt +1 -0
tragac-0.1.0/MANIFEST.in
ADDED
tragac-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tragac
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: One line around your AI client: Tragac decides and meters each request. Early access.
|
|
5
|
+
License: MIT
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# tragac (Python)
|
|
13
|
+
|
|
14
|
+
One line around your existing AI client, so Tragac decides each request and
|
|
15
|
+
meters what it cost.
|
|
16
|
+
|
|
17
|
+
Not on PyPI yet. Install from this repository:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install ./packages/python
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Setup command
|
|
24
|
+
|
|
25
|
+
`tragac init` runs the guided setup from the project folder: it checks the key,
|
|
26
|
+
writes `TRAGAC_KEY` into `.env`, adds `.env` to `.gitignore`, offers to add one
|
|
27
|
+
import line beside your AI client, then runs the Connect page's six checks.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install ./packages/python
|
|
31
|
+
tragac init
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
A test key is the default; a live key is only used after you type the word
|
|
35
|
+
`live`. No file is edited without showing the change and getting a yes, and the
|
|
36
|
+
command never calls an AI provider. See [the Python CLI module](tragac/cli.py)
|
|
37
|
+
for the exact steps.
|
|
38
|
+
|
|
39
|
+
## One line
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import os
|
|
43
|
+
from openai import OpenAI
|
|
44
|
+
import tragac
|
|
45
|
+
|
|
46
|
+
client = tragac.wrap(OpenAI(), customer_id=current_user.id)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
That is the whole setup. `wrap` returns the same client, so every existing call
|
|
50
|
+
site keeps working:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
answer = client.chat.completions.create(
|
|
54
|
+
model="gpt-4o-mini",
|
|
55
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
56
|
+
)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For each call the wrapper sends `evaluate` before the provider, calls the
|
|
60
|
+
provider with the model Tragac chose, then sends `usage` with the token counts,
|
|
61
|
+
the attempt number and the latency in milliseconds.
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
| Variable | Meaning |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `TRAGAC_KEY` | Your project key. Required. Sent as `Authorization: Bearer`, never logged. |
|
|
68
|
+
| `TRAGAC_API_BASE` | API base. Defaults to `https://tragac.vercel.app`. |
|
|
69
|
+
|
|
70
|
+
Keep the key on the server. A missing key is not an error: the call goes through
|
|
71
|
+
and nothing is reported (see *Fail open*).
|
|
72
|
+
|
|
73
|
+
## Arguments
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
tragac.wrap(
|
|
77
|
+
client,
|
|
78
|
+
customer_id=current_user.id, # a value, or a function called per request
|
|
79
|
+
agent="billing", # optional agent label
|
|
80
|
+
plan="pro", # optional plan label
|
|
81
|
+
session_id=session_id, # optional, stored with the decision
|
|
82
|
+
parent_agent="root", # optional, places the agent in the tree
|
|
83
|
+
customer_name="Ada", # optional, the customer's display name
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Only the arguments you pass are sent: without `agent` and `plan`, the evaluate
|
|
88
|
+
request carries no labels at all, and `session_id`, `parent_agent` and
|
|
89
|
+
`customer_name` are simply absent.
|
|
90
|
+
|
|
91
|
+
`agent` and `plan` travel in `metadata` under the keys your project chose on its
|
|
92
|
+
Connect page. The defaults are `agent` and `plan`; if you chose different keys,
|
|
93
|
+
pass them too:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
tragac.wrap(OpenAI(), customer_id=uid, agent="billing", plan="pro",
|
|
97
|
+
agent_key="agent_name", plan_key="tier")
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Further options: `provider` (overrides the provider name inferred from the
|
|
101
|
+
client class, e.g. `"openai"`), `retries` (extra attempts, default `0`) and
|
|
102
|
+
`timeout` (the evaluate budget in seconds, default `3`). A slow Tragac can delay
|
|
103
|
+
the AI call by up to that long before it is allowed through.
|
|
104
|
+
|
|
105
|
+
## Errors
|
|
106
|
+
|
|
107
|
+
The only exception the package raises on purpose is `tragac.Blocked`:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
try:
|
|
111
|
+
answer = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
|
|
112
|
+
except tragac.Blocked as blocked:
|
|
113
|
+
# blocked.reason: CUSTOMER_BANNED, CREDITS_EXHAUSTED, AGENT_PAUSED,
|
|
114
|
+
# AGENT_BUDGET_EXCEEDED or PROJECT_ARCHIVED
|
|
115
|
+
show_the_customer(blocked.reason)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The provider is never called when a request is blocked, and nothing is reported
|
|
119
|
+
as usage. `blocked.decision` holds the full Tragac answer.
|
|
120
|
+
|
|
121
|
+
Provider exceptions are yours: they are re-raised unchanged, after the failed
|
|
122
|
+
attempt has been reported.
|
|
123
|
+
|
|
124
|
+
## Streaming
|
|
125
|
+
|
|
126
|
+
Streaming works as before; the wrapper forwards every chunk unchanged and sends
|
|
127
|
+
one usage report when the stream ends.
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
stream = client.chat.completions.create(model="gpt-4o-mini", messages=messages, stream=True)
|
|
131
|
+
for chunk in stream:
|
|
132
|
+
print(chunk.choices[0].delta.content or "", end="")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Token counts come from the provider's final usage block. If the provider sends
|
|
136
|
+
none, output tokens are counted as content fragments and input tokens as `0`. A
|
|
137
|
+
stream the caller stops early, or that fails mid-way, is reported as `failed`.
|
|
138
|
+
|
|
139
|
+
## Async clients
|
|
140
|
+
|
|
141
|
+
`AsyncOpenAI` works the same way; the wrapper detects it and awaits the provider.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
client = tragac.wrap(AsyncOpenAI(), customer_id=current_user.id)
|
|
145
|
+
answer = await client.chat.completions.create(model="gpt-4o-mini", messages=messages)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Retries
|
|
149
|
+
|
|
150
|
+
`retries=2` makes up to three provider attempts for one Tragac request. All of
|
|
151
|
+
them reuse one `request_id` and report their own `attempt` (1, 2, 3), so Tragac
|
|
152
|
+
charges once per successful attempt and nothing twice.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
client = tragac.wrap(OpenAI(), customer_id=uid, retries=2)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Fail open
|
|
159
|
+
|
|
160
|
+
A Tragac problem never breaks your AI call. If Tragac cannot be reached within
|
|
161
|
+
the timeout, answers with an error, or has no key configured, the provider call
|
|
162
|
+
runs as usual and the usage report waits in a bounded background queue that
|
|
163
|
+
retries quietly. Only a real `BLOCK` decision stops a call.
|
|
164
|
+
|
|
165
|
+
`tragac.flush()` tries the queue immediately; you normally never need it.
|
|
166
|
+
|
|
167
|
+
## What is sent
|
|
168
|
+
|
|
169
|
+
`evaluate`: `customer_id`, `request_id`, `provider`, `requested_model`,
|
|
170
|
+
`timestamp`, `metadata` (labels only when set), plus `session_id` and
|
|
171
|
+
`customer_name` when set.
|
|
172
|
+
|
|
173
|
+
`usage`: `request_id`, `status`, `provider`, `actual_model`, `input_tokens`,
|
|
174
|
+
`output_tokens`, `attempt`, `latency_ms`, plus `error_reason` on a failure and
|
|
175
|
+
`parent_agent` when set. Token counts and latency are integers.
|
|
176
|
+
|
|
177
|
+
The key never appears in a body, a log line or a message.
|
tragac-0.1.0/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# tragac (Python)
|
|
2
|
+
|
|
3
|
+
One line around your existing AI client, so Tragac decides each request and
|
|
4
|
+
meters what it cost.
|
|
5
|
+
|
|
6
|
+
Not on PyPI yet. Install from this repository:
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install ./packages/python
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Setup command
|
|
13
|
+
|
|
14
|
+
`tragac init` runs the guided setup from the project folder: it checks the key,
|
|
15
|
+
writes `TRAGAC_KEY` into `.env`, adds `.env` to `.gitignore`, offers to add one
|
|
16
|
+
import line beside your AI client, then runs the Connect page's six checks.
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install ./packages/python
|
|
20
|
+
tragac init
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
A test key is the default; a live key is only used after you type the word
|
|
24
|
+
`live`. No file is edited without showing the change and getting a yes, and the
|
|
25
|
+
command never calls an AI provider. See [the Python CLI module](tragac/cli.py)
|
|
26
|
+
for the exact steps.
|
|
27
|
+
|
|
28
|
+
## One line
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import os
|
|
32
|
+
from openai import OpenAI
|
|
33
|
+
import tragac
|
|
34
|
+
|
|
35
|
+
client = tragac.wrap(OpenAI(), customer_id=current_user.id)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
That is the whole setup. `wrap` returns the same client, so every existing call
|
|
39
|
+
site keeps working:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
answer = client.chat.completions.create(
|
|
43
|
+
model="gpt-4o-mini",
|
|
44
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
For each call the wrapper sends `evaluate` before the provider, calls the
|
|
49
|
+
provider with the model Tragac chose, then sends `usage` with the token counts,
|
|
50
|
+
the attempt number and the latency in milliseconds.
|
|
51
|
+
|
|
52
|
+
## Configuration
|
|
53
|
+
|
|
54
|
+
| Variable | Meaning |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
| `TRAGAC_KEY` | Your project key. Required. Sent as `Authorization: Bearer`, never logged. |
|
|
57
|
+
| `TRAGAC_API_BASE` | API base. Defaults to `https://tragac.vercel.app`. |
|
|
58
|
+
|
|
59
|
+
Keep the key on the server. A missing key is not an error: the call goes through
|
|
60
|
+
and nothing is reported (see *Fail open*).
|
|
61
|
+
|
|
62
|
+
## Arguments
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
tragac.wrap(
|
|
66
|
+
client,
|
|
67
|
+
customer_id=current_user.id, # a value, or a function called per request
|
|
68
|
+
agent="billing", # optional agent label
|
|
69
|
+
plan="pro", # optional plan label
|
|
70
|
+
session_id=session_id, # optional, stored with the decision
|
|
71
|
+
parent_agent="root", # optional, places the agent in the tree
|
|
72
|
+
customer_name="Ada", # optional, the customer's display name
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Only the arguments you pass are sent: without `agent` and `plan`, the evaluate
|
|
77
|
+
request carries no labels at all, and `session_id`, `parent_agent` and
|
|
78
|
+
`customer_name` are simply absent.
|
|
79
|
+
|
|
80
|
+
`agent` and `plan` travel in `metadata` under the keys your project chose on its
|
|
81
|
+
Connect page. The defaults are `agent` and `plan`; if you chose different keys,
|
|
82
|
+
pass them too:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
tragac.wrap(OpenAI(), customer_id=uid, agent="billing", plan="pro",
|
|
86
|
+
agent_key="agent_name", plan_key="tier")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Further options: `provider` (overrides the provider name inferred from the
|
|
90
|
+
client class, e.g. `"openai"`), `retries` (extra attempts, default `0`) and
|
|
91
|
+
`timeout` (the evaluate budget in seconds, default `3`). A slow Tragac can delay
|
|
92
|
+
the AI call by up to that long before it is allowed through.
|
|
93
|
+
|
|
94
|
+
## Errors
|
|
95
|
+
|
|
96
|
+
The only exception the package raises on purpose is `tragac.Blocked`:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
try:
|
|
100
|
+
answer = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
|
|
101
|
+
except tragac.Blocked as blocked:
|
|
102
|
+
# blocked.reason: CUSTOMER_BANNED, CREDITS_EXHAUSTED, AGENT_PAUSED,
|
|
103
|
+
# AGENT_BUDGET_EXCEEDED or PROJECT_ARCHIVED
|
|
104
|
+
show_the_customer(blocked.reason)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The provider is never called when a request is blocked, and nothing is reported
|
|
108
|
+
as usage. `blocked.decision` holds the full Tragac answer.
|
|
109
|
+
|
|
110
|
+
Provider exceptions are yours: they are re-raised unchanged, after the failed
|
|
111
|
+
attempt has been reported.
|
|
112
|
+
|
|
113
|
+
## Streaming
|
|
114
|
+
|
|
115
|
+
Streaming works as before; the wrapper forwards every chunk unchanged and sends
|
|
116
|
+
one usage report when the stream ends.
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
stream = client.chat.completions.create(model="gpt-4o-mini", messages=messages, stream=True)
|
|
120
|
+
for chunk in stream:
|
|
121
|
+
print(chunk.choices[0].delta.content or "", end="")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Token counts come from the provider's final usage block. If the provider sends
|
|
125
|
+
none, output tokens are counted as content fragments and input tokens as `0`. A
|
|
126
|
+
stream the caller stops early, or that fails mid-way, is reported as `failed`.
|
|
127
|
+
|
|
128
|
+
## Async clients
|
|
129
|
+
|
|
130
|
+
`AsyncOpenAI` works the same way; the wrapper detects it and awaits the provider.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
client = tragac.wrap(AsyncOpenAI(), customer_id=current_user.id)
|
|
134
|
+
answer = await client.chat.completions.create(model="gpt-4o-mini", messages=messages)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Retries
|
|
138
|
+
|
|
139
|
+
`retries=2` makes up to three provider attempts for one Tragac request. All of
|
|
140
|
+
them reuse one `request_id` and report their own `attempt` (1, 2, 3), so Tragac
|
|
141
|
+
charges once per successful attempt and nothing twice.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
client = tragac.wrap(OpenAI(), customer_id=uid, retries=2)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Fail open
|
|
148
|
+
|
|
149
|
+
A Tragac problem never breaks your AI call. If Tragac cannot be reached within
|
|
150
|
+
the timeout, answers with an error, or has no key configured, the provider call
|
|
151
|
+
runs as usual and the usage report waits in a bounded background queue that
|
|
152
|
+
retries quietly. Only a real `BLOCK` decision stops a call.
|
|
153
|
+
|
|
154
|
+
`tragac.flush()` tries the queue immediately; you normally never need it.
|
|
155
|
+
|
|
156
|
+
## What is sent
|
|
157
|
+
|
|
158
|
+
`evaluate`: `customer_id`, `request_id`, `provider`, `requested_model`,
|
|
159
|
+
`timestamp`, `metadata` (labels only when set), plus `session_id` and
|
|
160
|
+
`customer_name` when set.
|
|
161
|
+
|
|
162
|
+
`usage`: `request_id`, `status`, `provider`, `actual_model`, `input_tokens`,
|
|
163
|
+
`output_tokens`, `attempt`, `latency_ms`, plus `error_reason` on a failure and
|
|
164
|
+
`parent_agent` when set. Token counts and latency are integers.
|
|
165
|
+
|
|
166
|
+
The key never appears in a body, a log line or a message.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tragac"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "One line around your AI client: Tragac decides and meters each request. Early access."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
dependencies = []
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.scripts]
|
|
20
|
+
tragac = "tragac.cli:main"
|
|
21
|
+
|
|
22
|
+
[tool.setuptools.packages.find]
|
|
23
|
+
include = ["tragac*"]
|
tragac-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Tragac for Python: one line around your AI client.
|
|
2
|
+
|
|
3
|
+
import tragac
|
|
4
|
+
from openai import OpenAI
|
|
5
|
+
|
|
6
|
+
client = tragac.wrap(OpenAI(), customer_id=current_user.id)
|
|
7
|
+
|
|
8
|
+
The key is read from ``TRAGAC_KEY`` and is never logged. A Tragac problem never
|
|
9
|
+
breaks the founder's AI call: if Tragac cannot answer, the call runs as usual.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .wrap import Blocked, flush, wrap
|
|
13
|
+
|
|
14
|
+
__all__ = ["wrap", "Blocked", "flush"]
|
|
15
|
+
__version__ = "0.1.0"
|