aurex-sdk 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.
- aurex_sdk-0.1.0/PKG-INFO +284 -0
- aurex_sdk-0.1.0/README.md +265 -0
- aurex_sdk-0.1.0/aurex_sdk/__init__.py +32 -0
- aurex_sdk-0.1.0/aurex_sdk/auditor.py +3504 -0
- aurex_sdk-0.1.0/aurex_sdk/cli.py +254 -0
- aurex_sdk-0.1.0/aurex_sdk/cloud_sync.py +60 -0
- aurex_sdk-0.1.0/aurex_sdk/decisions.py +150 -0
- aurex_sdk-0.1.0/aurex_sdk/exceptions.py +90 -0
- aurex_sdk-0.1.0/aurex_sdk/heuristics.py +398 -0
- aurex_sdk-0.1.0/aurex_sdk/hook.py +45 -0
- aurex_sdk-0.1.0/aurex_sdk/ledger_utils.py +80 -0
- aurex_sdk-0.1.0/aurex_sdk/resilience.py +652 -0
- aurex_sdk-0.1.0/aurex_sdk/semantic.py +21 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/PKG-INFO +284 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/SOURCES.txt +19 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/dependency_links.txt +1 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/entry_points.txt +2 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/requires.txt +18 -0
- aurex_sdk-0.1.0/aurex_sdk.egg-info/top_level.txt +1 -0
- aurex_sdk-0.1.0/pyproject.toml +27 -0
- aurex_sdk-0.1.0/setup.cfg +4 -0
aurex_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aurex-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Aurex Python SDK — auto-instrument OpenAI, Anthropic, LiteLLM
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Provides-Extra: openai
|
|
8
|
+
Requires-Dist: openai>=1.0.0; extra == "openai"
|
|
9
|
+
Provides-Extra: anthropic
|
|
10
|
+
Requires-Dist: anthropic>=0.25.0; extra == "anthropic"
|
|
11
|
+
Provides-Extra: litellm
|
|
12
|
+
Requires-Dist: litellm>=1.0.0; extra == "litellm"
|
|
13
|
+
Provides-Extra: google
|
|
14
|
+
Requires-Dist: google-genai>=1.0.0; extra == "google"
|
|
15
|
+
Provides-Extra: google-legacy
|
|
16
|
+
Requires-Dist: google-generativeai>=0.3.0; extra == "google-legacy"
|
|
17
|
+
Provides-Extra: huggingface
|
|
18
|
+
Requires-Dist: huggingface_hub>=0.19.0; extra == "huggingface"
|
|
19
|
+
|
|
20
|
+
# Aurex Python SDK
|
|
21
|
+
|
|
22
|
+
The lightweight, zero-PII Python SDK for Aurex AI cost-optimization and budget circuit-breaking.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
Install the SDK in your project:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install aurex-sdk
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or for local development / editable mode:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install -e sdk/python
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- **Zero-code Auto-Instrumentation**: Automatically patch LLM providers (OpenAI, Anthropic, Google Gemini, LiteLLM) via a site-packages hook.
|
|
43
|
+
- **Real-Time Guardrails (`pre_call_check`)**: Detect and block/redirect loops, system prompt waste, and expensive models before they occur.
|
|
44
|
+
- **In-Memory Budget Circuit Breaker**: Prevent budget overrun with context token limits and rate guards.
|
|
45
|
+
- **Local-First & Privacy-Focused**: Telemetry is batched asynchronously and kept offline in `.aurex/ledger.jsonl`. Prompts are not synced by default.
|
|
46
|
+
- **Crash Safety**: In-flight events are saved to `pending.jsonl` to ensure durability.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
### 1. Zero-Code Auto-Hook (Recommended)
|
|
53
|
+
|
|
54
|
+
Run the CLI command once to install the import hook into your Python site-packages. This intercepts LLM SDK calls automatically with zero code modification.
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Install the hook
|
|
58
|
+
aurex install-hook
|
|
59
|
+
|
|
60
|
+
# Configure env vars and run your application
|
|
61
|
+
export AUREX_ENABLED=1
|
|
62
|
+
export AUREX_API_KEY=aux_dev_yourkey
|
|
63
|
+
python your_app.py
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To remove the hook:
|
|
67
|
+
```bash
|
|
68
|
+
aurex uninstall-hook
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. Manual Integration
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from aurex_sdk import AurexAuditor, AurexConfig
|
|
75
|
+
|
|
76
|
+
# Initialize the auditor singleton
|
|
77
|
+
auditor = AurexAuditor(AurexConfig(
|
|
78
|
+
api_key="aux_dev_yourkey",
|
|
79
|
+
project="production-service",
|
|
80
|
+
storage_mode="file", # "file" or "memory"
|
|
81
|
+
ledger_path=".aurex/ledger.jsonl",
|
|
82
|
+
cloud_sync=True
|
|
83
|
+
))
|
|
84
|
+
|
|
85
|
+
# Patch all supported providers (OpenAI, Anthropic, Google Gemini, LiteLLM)
|
|
86
|
+
auditor.patch_all()
|
|
87
|
+
|
|
88
|
+
# Or patch manually/selectively
|
|
89
|
+
# auditor.patch_openai()
|
|
90
|
+
# auditor.patch_anthropic()
|
|
91
|
+
# auditor.patch_google()
|
|
92
|
+
# auditor.patch_litellm()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### `AurexConfig` Parameters
|
|
100
|
+
|
|
101
|
+
| Option | Type | Default | Description |
|
|
102
|
+
| :--- | :--- | :--- | :--- |
|
|
103
|
+
| `api_key` | `str` | `None` / `env(AUREX_API_KEY)` | Your Aurex Project API key. |
|
|
104
|
+
| `project` | `str` | `"default"` | Logical project identifier. |
|
|
105
|
+
| `ledger_path` | `str` | `".aurex/ledger.jsonl"` | Path to write the local telemetry ledger to. |
|
|
106
|
+
| `cloud_sync` | `bool` | `False` | Sync telemetry events asynchronously to the cloud dashboard. |
|
|
107
|
+
| `cloud_endpoint`| `str` | `None` | Endpoint for cloud syncing. Overrides default API endpoint. |
|
|
108
|
+
| `base_url` | `str` | `None` / `env(AUREX_BASE_URL)` | Base URL of backend API (defaults to `http://localhost:8000/api/v1`). |
|
|
109
|
+
| `flush_interval_seconds` | `float` | `30.0` | Telemetry sync queue flush interval. |
|
|
110
|
+
| `max_batch_size` | `int` | `100` | Maximum batch size of synced events. |
|
|
111
|
+
| `local_only` | `bool` | `True` | Runs fully locally without cloud sync. (Turn off for cloud sync) |
|
|
112
|
+
| `storage_mode` | `str` | `"file"` | `"file"` to write to disk ledger, or `"memory"` for memory-only store. |
|
|
113
|
+
| `sync_prompt_hashes` | `bool` | `False` / `env(AUREX_SYNC_PROMPT_HASHES)` | Sync prompt/response hashes to the cloud dashboard. |
|
|
114
|
+
| `budget_guard` | `dict` | `None` | Configuration dictionary for budget limits (see below). |
|
|
115
|
+
| `pricing_path` | `str` | `None` / `env(AUREX_PRICING_PATH)` | Path to a custom JSON pricing configuration. |
|
|
116
|
+
| `default_pricing` | `dict` | `None` | Fallback cost dict with keys `input` and `output` (USD per 1M tokens) for unknown models. |
|
|
117
|
+
| `simple_prompt_token_limit` | `int` | `120` | Threshold token count below which prompts are classified as simple. |
|
|
118
|
+
| `runaway_context_multiplier` | `float` | `2.2` | Scaling multiplier representing runaway context growth checks. |
|
|
119
|
+
| `loop_similarity_threshold` | `float` | `0.8` | Jaccard similarity threshold for semantic loop checks. |
|
|
120
|
+
| `downgrade_map` | `dict` | `None` | Custom model mappings representing downgrade paths (e.g. `{"gpt-4o": "gpt-4o-mini"}`). |
|
|
121
|
+
|
|
122
|
+
### Dynamic Pricing & Programmatic Registration
|
|
123
|
+
|
|
124
|
+
You can specify custom pricing using a local JSON file or register rates programmatically:
|
|
125
|
+
|
|
126
|
+
1. **Custom JSON File (`pricing_path` or `env(AUREX_PRICING_PATH)`)**:
|
|
127
|
+
Create a JSON pricing file:
|
|
128
|
+
```json
|
|
129
|
+
{
|
|
130
|
+
"my-custom-model": { "input": 12.50, "output": 35.00 },
|
|
131
|
+
"finetuned-gpt-*": { "input": 3.00, "output": 12.00 }
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
2. **Programmatic Registration**:
|
|
136
|
+
```python
|
|
137
|
+
auditor.register_model_pricing("custom-model", input_cost=10.0, output_cost=30.0)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Budget Guard Configuration (`budget_guard`)
|
|
141
|
+
|
|
142
|
+
Pass a dictionary to `budget_guard` to enforce runtime limits:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
config = AurexConfig(
|
|
146
|
+
budget_guard={
|
|
147
|
+
"max_cost_per_minute": 5.00,
|
|
148
|
+
"max_cost_per_session": 50.00,
|
|
149
|
+
"max_requests_per_minute": 100,
|
|
150
|
+
"max_context_tokens": 128000,
|
|
151
|
+
"on_budget_exceeded": "throw", # "throw" | "warn" | "webhook"
|
|
152
|
+
"webhook_url": "https://api.yourdomain.com/aurex-alert",
|
|
153
|
+
"dry_run": False
|
|
154
|
+
}
|
|
155
|
+
)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Context Overrides (Per-Call Options)
|
|
161
|
+
|
|
162
|
+
When using wrapper methods (`wrap` / `awrap`), you can pass request-scoped options (e.g., `tags`, custom thresholds, loop sensitivity) to insulate different execution contexts:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
resp = auditor.wrap(
|
|
166
|
+
lambda params: client.chat.completions.create(**params),
|
|
167
|
+
lambda: {
|
|
168
|
+
"model": "gpt-4o",
|
|
169
|
+
"messages": [{"role": "user", "content": "Query..."}]
|
|
170
|
+
},
|
|
171
|
+
options={
|
|
172
|
+
"workflow_id": "tenant_abc_session",
|
|
173
|
+
"tags": {"tier": "enterprise"},
|
|
174
|
+
"simple_prompt_token_limit": 50, # override default threshold
|
|
175
|
+
"budget_guard": {
|
|
176
|
+
"max_cost_per_session": 2.00 # custom budget limit for this call
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Hugging Face Native Integration
|
|
185
|
+
|
|
186
|
+
Auto-instrument Hugging Face text generation and chat completions. Register the optional dependency in your environment:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
pip install aurex-sdk[huggingface]
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Call `patch_all()` or `patch_huggingface()` to instrument standard `huggingface_hub` client calls:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
from aurex_sdk import AurexAuditor
|
|
196
|
+
from huggingface_hub import InferenceClient
|
|
197
|
+
|
|
198
|
+
auditor = AurexAuditor()
|
|
199
|
+
auditor.patch_huggingface()
|
|
200
|
+
|
|
201
|
+
client = InferenceClient(model="meta-llama/Llama-3-8b-Instruct")
|
|
202
|
+
resp = client.chat_completion(messages=[{"role": "user", "content": "Hello"}])
|
|
203
|
+
# Tracked at $0.00 (Hugging Face serverless defaults to free cost)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
To configure pricing for dedicated Hugging Face endpoints, use the dynamic pricing configuration or register the endpoint name under wildcard pricing.
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Real-Time Guards (`pre_call_check`)
|
|
213
|
+
|
|
214
|
+
For manual LLM flows, execute a pre-call check to catch loops and budget overruns before calling the LLM API.
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from aurex_sdk import AurexAuditor, AurexDecision, AurexAction
|
|
218
|
+
|
|
219
|
+
auditor = AurexAuditor()
|
|
220
|
+
|
|
221
|
+
# Perform the check
|
|
222
|
+
decision: AurexDecision = auditor.pre_call_check(
|
|
223
|
+
model="gpt-4o",
|
|
224
|
+
prompt="Generate code for Antigravity...",
|
|
225
|
+
system_prompt="You are a senior dev...",
|
|
226
|
+
workflow_id="user_session_123",
|
|
227
|
+
messages=[{"role": "user", "content": "..."}]
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
if decision.action == "block":
|
|
231
|
+
raise Exception(f"Request blocked: {decision.reason}")
|
|
232
|
+
elif decision.action == "downgrade":
|
|
233
|
+
# Use cheaper model suggestion
|
|
234
|
+
model = decision.suggested_model # e.g. "gpt-4o-mini"
|
|
235
|
+
print(f"Downgrading to {model} because: {decision.reason}")
|
|
236
|
+
elif decision.action == "compress":
|
|
237
|
+
# Use compressed messages to avoid system prompt waste
|
|
238
|
+
messages = decision.compressed_messages
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## CLI Commands
|
|
244
|
+
|
|
245
|
+
The Python SDK comes with a built-in CLI command `aurex` (or `python -m aurex_sdk.cli`).
|
|
246
|
+
|
|
247
|
+
### `aurex install-hook`
|
|
248
|
+
Installs the zero-code auto-instrumentation `.pth` hook.
|
|
249
|
+
- `--target`: Optional explicit path to `site-packages`.
|
|
250
|
+
|
|
251
|
+
### `aurex uninstall-hook`
|
|
252
|
+
Removes the hook.
|
|
253
|
+
|
|
254
|
+
### `aurex audit`
|
|
255
|
+
Analyze a local ledger offline to run heuristics and check against CI policies (Build Breaker).
|
|
256
|
+
- `--ledger`: Path to ledger file (default: `.aurex/ledger.jsonl`).
|
|
257
|
+
- `--fail-under`: Score threshold (0-100) below which the build will fail.
|
|
258
|
+
- `--max-waste-usd`: Maximum allowed wasted cost before failing.
|
|
259
|
+
- `--max-spend-usd`: Maximum allowed budget cost before failing.
|
|
260
|
+
- `--mode`: CI/CD action mode: `warn` (default) or `block` (returns exit code `1` on failure).
|
|
261
|
+
|
|
262
|
+
Example:
|
|
263
|
+
```bash
|
|
264
|
+
aurex audit --ledger .aurex/ledger.jsonl --fail-under 85 --max-waste-usd 10.00 --mode block
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Health & Monitoring
|
|
270
|
+
|
|
271
|
+
Check the status of the auditor thread and queue size:
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
health = auditor.get_health()
|
|
275
|
+
print(health)
|
|
276
|
+
# Output:
|
|
277
|
+
# {
|
|
278
|
+
# "status": "healthy",
|
|
279
|
+
# "queue_size": 0,
|
|
280
|
+
# "pending_file_exists": False,
|
|
281
|
+
# "thread_active": True,
|
|
282
|
+
# "events_logged": 42
|
|
283
|
+
# }
|
|
284
|
+
```
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# Aurex Python SDK
|
|
2
|
+
|
|
3
|
+
The lightweight, zero-PII Python SDK for Aurex AI cost-optimization and budget circuit-breaking.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the SDK in your project:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install aurex-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or for local development / editable mode:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install -e sdk/python
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- **Zero-code Auto-Instrumentation**: Automatically patch LLM providers (OpenAI, Anthropic, Google Gemini, LiteLLM) via a site-packages hook.
|
|
24
|
+
- **Real-Time Guardrails (`pre_call_check`)**: Detect and block/redirect loops, system prompt waste, and expensive models before they occur.
|
|
25
|
+
- **In-Memory Budget Circuit Breaker**: Prevent budget overrun with context token limits and rate guards.
|
|
26
|
+
- **Local-First & Privacy-Focused**: Telemetry is batched asynchronously and kept offline in `.aurex/ledger.jsonl`. Prompts are not synced by default.
|
|
27
|
+
- **Crash Safety**: In-flight events are saved to `pending.jsonl` to ensure durability.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
### 1. Zero-Code Auto-Hook (Recommended)
|
|
34
|
+
|
|
35
|
+
Run the CLI command once to install the import hook into your Python site-packages. This intercepts LLM SDK calls automatically with zero code modification.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Install the hook
|
|
39
|
+
aurex install-hook
|
|
40
|
+
|
|
41
|
+
# Configure env vars and run your application
|
|
42
|
+
export AUREX_ENABLED=1
|
|
43
|
+
export AUREX_API_KEY=aux_dev_yourkey
|
|
44
|
+
python your_app.py
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
To remove the hook:
|
|
48
|
+
```bash
|
|
49
|
+
aurex uninstall-hook
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Manual Integration
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from aurex_sdk import AurexAuditor, AurexConfig
|
|
56
|
+
|
|
57
|
+
# Initialize the auditor singleton
|
|
58
|
+
auditor = AurexAuditor(AurexConfig(
|
|
59
|
+
api_key="aux_dev_yourkey",
|
|
60
|
+
project="production-service",
|
|
61
|
+
storage_mode="file", # "file" or "memory"
|
|
62
|
+
ledger_path=".aurex/ledger.jsonl",
|
|
63
|
+
cloud_sync=True
|
|
64
|
+
))
|
|
65
|
+
|
|
66
|
+
# Patch all supported providers (OpenAI, Anthropic, Google Gemini, LiteLLM)
|
|
67
|
+
auditor.patch_all()
|
|
68
|
+
|
|
69
|
+
# Or patch manually/selectively
|
|
70
|
+
# auditor.patch_openai()
|
|
71
|
+
# auditor.patch_anthropic()
|
|
72
|
+
# auditor.patch_google()
|
|
73
|
+
# auditor.patch_litellm()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## API Reference
|
|
79
|
+
|
|
80
|
+
### `AurexConfig` Parameters
|
|
81
|
+
|
|
82
|
+
| Option | Type | Default | Description |
|
|
83
|
+
| :--- | :--- | :--- | :--- |
|
|
84
|
+
| `api_key` | `str` | `None` / `env(AUREX_API_KEY)` | Your Aurex Project API key. |
|
|
85
|
+
| `project` | `str` | `"default"` | Logical project identifier. |
|
|
86
|
+
| `ledger_path` | `str` | `".aurex/ledger.jsonl"` | Path to write the local telemetry ledger to. |
|
|
87
|
+
| `cloud_sync` | `bool` | `False` | Sync telemetry events asynchronously to the cloud dashboard. |
|
|
88
|
+
| `cloud_endpoint`| `str` | `None` | Endpoint for cloud syncing. Overrides default API endpoint. |
|
|
89
|
+
| `base_url` | `str` | `None` / `env(AUREX_BASE_URL)` | Base URL of backend API (defaults to `http://localhost:8000/api/v1`). |
|
|
90
|
+
| `flush_interval_seconds` | `float` | `30.0` | Telemetry sync queue flush interval. |
|
|
91
|
+
| `max_batch_size` | `int` | `100` | Maximum batch size of synced events. |
|
|
92
|
+
| `local_only` | `bool` | `True` | Runs fully locally without cloud sync. (Turn off for cloud sync) |
|
|
93
|
+
| `storage_mode` | `str` | `"file"` | `"file"` to write to disk ledger, or `"memory"` for memory-only store. |
|
|
94
|
+
| `sync_prompt_hashes` | `bool` | `False` / `env(AUREX_SYNC_PROMPT_HASHES)` | Sync prompt/response hashes to the cloud dashboard. |
|
|
95
|
+
| `budget_guard` | `dict` | `None` | Configuration dictionary for budget limits (see below). |
|
|
96
|
+
| `pricing_path` | `str` | `None` / `env(AUREX_PRICING_PATH)` | Path to a custom JSON pricing configuration. |
|
|
97
|
+
| `default_pricing` | `dict` | `None` | Fallback cost dict with keys `input` and `output` (USD per 1M tokens) for unknown models. |
|
|
98
|
+
| `simple_prompt_token_limit` | `int` | `120` | Threshold token count below which prompts are classified as simple. |
|
|
99
|
+
| `runaway_context_multiplier` | `float` | `2.2` | Scaling multiplier representing runaway context growth checks. |
|
|
100
|
+
| `loop_similarity_threshold` | `float` | `0.8` | Jaccard similarity threshold for semantic loop checks. |
|
|
101
|
+
| `downgrade_map` | `dict` | `None` | Custom model mappings representing downgrade paths (e.g. `{"gpt-4o": "gpt-4o-mini"}`). |
|
|
102
|
+
|
|
103
|
+
### Dynamic Pricing & Programmatic Registration
|
|
104
|
+
|
|
105
|
+
You can specify custom pricing using a local JSON file or register rates programmatically:
|
|
106
|
+
|
|
107
|
+
1. **Custom JSON File (`pricing_path` or `env(AUREX_PRICING_PATH)`)**:
|
|
108
|
+
Create a JSON pricing file:
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"my-custom-model": { "input": 12.50, "output": 35.00 },
|
|
112
|
+
"finetuned-gpt-*": { "input": 3.00, "output": 12.00 }
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
2. **Programmatic Registration**:
|
|
117
|
+
```python
|
|
118
|
+
auditor.register_model_pricing("custom-model", input_cost=10.0, output_cost=30.0)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Budget Guard Configuration (`budget_guard`)
|
|
122
|
+
|
|
123
|
+
Pass a dictionary to `budget_guard` to enforce runtime limits:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
config = AurexConfig(
|
|
127
|
+
budget_guard={
|
|
128
|
+
"max_cost_per_minute": 5.00,
|
|
129
|
+
"max_cost_per_session": 50.00,
|
|
130
|
+
"max_requests_per_minute": 100,
|
|
131
|
+
"max_context_tokens": 128000,
|
|
132
|
+
"on_budget_exceeded": "throw", # "throw" | "warn" | "webhook"
|
|
133
|
+
"webhook_url": "https://api.yourdomain.com/aurex-alert",
|
|
134
|
+
"dry_run": False
|
|
135
|
+
}
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## Context Overrides (Per-Call Options)
|
|
142
|
+
|
|
143
|
+
When using wrapper methods (`wrap` / `awrap`), you can pass request-scoped options (e.g., `tags`, custom thresholds, loop sensitivity) to insulate different execution contexts:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
resp = auditor.wrap(
|
|
147
|
+
lambda params: client.chat.completions.create(**params),
|
|
148
|
+
lambda: {
|
|
149
|
+
"model": "gpt-4o",
|
|
150
|
+
"messages": [{"role": "user", "content": "Query..."}]
|
|
151
|
+
},
|
|
152
|
+
options={
|
|
153
|
+
"workflow_id": "tenant_abc_session",
|
|
154
|
+
"tags": {"tier": "enterprise"},
|
|
155
|
+
"simple_prompt_token_limit": 50, # override default threshold
|
|
156
|
+
"budget_guard": {
|
|
157
|
+
"max_cost_per_session": 2.00 # custom budget limit for this call
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Hugging Face Native Integration
|
|
166
|
+
|
|
167
|
+
Auto-instrument Hugging Face text generation and chat completions. Register the optional dependency in your environment:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
pip install aurex-sdk[huggingface]
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Call `patch_all()` or `patch_huggingface()` to instrument standard `huggingface_hub` client calls:
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from aurex_sdk import AurexAuditor
|
|
177
|
+
from huggingface_hub import InferenceClient
|
|
178
|
+
|
|
179
|
+
auditor = AurexAuditor()
|
|
180
|
+
auditor.patch_huggingface()
|
|
181
|
+
|
|
182
|
+
client = InferenceClient(model="meta-llama/Llama-3-8b-Instruct")
|
|
183
|
+
resp = client.chat_completion(messages=[{"role": "user", "content": "Hello"}])
|
|
184
|
+
# Tracked at $0.00 (Hugging Face serverless defaults to free cost)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
To configure pricing for dedicated Hugging Face endpoints, use the dynamic pricing configuration or register the endpoint name under wildcard pricing.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Real-Time Guards (`pre_call_check`)
|
|
194
|
+
|
|
195
|
+
For manual LLM flows, execute a pre-call check to catch loops and budget overruns before calling the LLM API.
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
from aurex_sdk import AurexAuditor, AurexDecision, AurexAction
|
|
199
|
+
|
|
200
|
+
auditor = AurexAuditor()
|
|
201
|
+
|
|
202
|
+
# Perform the check
|
|
203
|
+
decision: AurexDecision = auditor.pre_call_check(
|
|
204
|
+
model="gpt-4o",
|
|
205
|
+
prompt="Generate code for Antigravity...",
|
|
206
|
+
system_prompt="You are a senior dev...",
|
|
207
|
+
workflow_id="user_session_123",
|
|
208
|
+
messages=[{"role": "user", "content": "..."}]
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
if decision.action == "block":
|
|
212
|
+
raise Exception(f"Request blocked: {decision.reason}")
|
|
213
|
+
elif decision.action == "downgrade":
|
|
214
|
+
# Use cheaper model suggestion
|
|
215
|
+
model = decision.suggested_model # e.g. "gpt-4o-mini"
|
|
216
|
+
print(f"Downgrading to {model} because: {decision.reason}")
|
|
217
|
+
elif decision.action == "compress":
|
|
218
|
+
# Use compressed messages to avoid system prompt waste
|
|
219
|
+
messages = decision.compressed_messages
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## CLI Commands
|
|
225
|
+
|
|
226
|
+
The Python SDK comes with a built-in CLI command `aurex` (or `python -m aurex_sdk.cli`).
|
|
227
|
+
|
|
228
|
+
### `aurex install-hook`
|
|
229
|
+
Installs the zero-code auto-instrumentation `.pth` hook.
|
|
230
|
+
- `--target`: Optional explicit path to `site-packages`.
|
|
231
|
+
|
|
232
|
+
### `aurex uninstall-hook`
|
|
233
|
+
Removes the hook.
|
|
234
|
+
|
|
235
|
+
### `aurex audit`
|
|
236
|
+
Analyze a local ledger offline to run heuristics and check against CI policies (Build Breaker).
|
|
237
|
+
- `--ledger`: Path to ledger file (default: `.aurex/ledger.jsonl`).
|
|
238
|
+
- `--fail-under`: Score threshold (0-100) below which the build will fail.
|
|
239
|
+
- `--max-waste-usd`: Maximum allowed wasted cost before failing.
|
|
240
|
+
- `--max-spend-usd`: Maximum allowed budget cost before failing.
|
|
241
|
+
- `--mode`: CI/CD action mode: `warn` (default) or `block` (returns exit code `1` on failure).
|
|
242
|
+
|
|
243
|
+
Example:
|
|
244
|
+
```bash
|
|
245
|
+
aurex audit --ledger .aurex/ledger.jsonl --fail-under 85 --max-waste-usd 10.00 --mode block
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
---
|
|
249
|
+
|
|
250
|
+
## Health & Monitoring
|
|
251
|
+
|
|
252
|
+
Check the status of the auditor thread and queue size:
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
health = auditor.get_health()
|
|
256
|
+
print(health)
|
|
257
|
+
# Output:
|
|
258
|
+
# {
|
|
259
|
+
# "status": "healthy",
|
|
260
|
+
# "queue_size": 0,
|
|
261
|
+
# "pending_file_exists": False,
|
|
262
|
+
# "thread_active": True,
|
|
263
|
+
# "events_logged": 42
|
|
264
|
+
# }
|
|
265
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Aurex Zero-Trust, Local-First SDK
|
|
3
|
+
Provides asynchronous, zero-PII auditing for LLM costs and credit scoring.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .auditor import AurexAuditor, AurexConfig
|
|
7
|
+
from .decisions import AurexDecision, AurexAction
|
|
8
|
+
from .exceptions import (
|
|
9
|
+
AurexError,
|
|
10
|
+
AurexBudgetExceeded,
|
|
11
|
+
AurexBlocked,
|
|
12
|
+
AurexConfigError,
|
|
13
|
+
AurexRecovered,
|
|
14
|
+
AurexResilienceExhausted,
|
|
15
|
+
)
|
|
16
|
+
from .resilience import ResiliencePolicy, classify_response, parse_resilience_config
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"AurexAuditor",
|
|
20
|
+
"AurexConfig",
|
|
21
|
+
"AurexDecision",
|
|
22
|
+
"AurexAction",
|
|
23
|
+
"AurexError",
|
|
24
|
+
"AurexBudgetExceeded",
|
|
25
|
+
"AurexBlocked",
|
|
26
|
+
"AurexConfigError",
|
|
27
|
+
"AurexRecovered",
|
|
28
|
+
"AurexResilienceExhausted",
|
|
29
|
+
"ResiliencePolicy",
|
|
30
|
+
"classify_response",
|
|
31
|
+
"parse_resilience_config",
|
|
32
|
+
]
|