frugal-sdk-python 0.5.0__tar.gz → 0.6.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.
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/PKG-INFO +33 -15
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/README.md +32 -14
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/docs/metrics-reference.md +21 -10
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/docs/quickstart.md +33 -17
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/config.py +234 -86
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/debug.py +6 -2
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/instrument.py +133 -79
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/model_cap.py +2 -2
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/otel.py +15 -25
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/providers.py +45 -21
- frugal_sdk_python-0.6.0/src/frugal_metrics/safe.py +58 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/semconv.py +4 -1
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/streams.py +5 -4
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/track.py +8 -6
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/_batch.py +20 -12
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/anthropic.py +4 -4
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/bedrock.py +4 -4
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/openai.py +4 -4
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/conftest.py +24 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_config.py +66 -3
- frugal_sdk_python-0.6.0/tests/test_node_parity.py +791 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_providers.py +31 -0
- frugal_sdk_python-0.6.0/tests/test_sibling_parity.py +79 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_anthropic_batches.py +52 -0
- frugal_sdk_python-0.5.0/src/frugal_metrics/safe.py +0 -45
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/.github/workflows/cd.yml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/.gitignore +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/LICENSES/Apache-2.0.txt +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/REUSE.toml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/README.md +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/collector/otel-collector-config.yaml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/docker-compose.yml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/grafana/dashboards/frugal-metrics.json +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/grafana/provisioning/dashboards/frugal.yml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/grafana/provisioning/datasources/prometheus.yml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/mockllm/Dockerfile +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/mockllm/package.json +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/mockllm/src/server.js +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/prometheus/consoles/index.html.example +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/prometheus/consoles/models.html +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/prometheus/consoles/overview.html +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/prometheus/consoles/sites.html +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/prometheus/prometheus.yml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/Dockerfile +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/clients.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/anon.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/classify.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/deferred.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/fail.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/pipeline.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/summarize.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/translate.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/via_blocked_lib.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/handlers/via_unblocked_lib.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/internal_lib/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/internal_lib/ai_helper.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/main.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/sibling_lib/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/app/sibling_lib/ai_helper.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/pyproject.toml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/sample-app/requirements.txt +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/examples/test-env/scripts/verify.sh +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/pyproject.toml +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/block_hash_lru.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/caller.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/duplicate_filter.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/prompt_analyzer.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/shutdown.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/_patching.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/src/frugal_metrics/wrappers/bedrock_models.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/__init__.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_bedrock_models.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_block_hash_lru.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_caller.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_caller_edge_cases.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_debug.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_duplicate_filter.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_model_cap.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_never_impact_host.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_otel_overflow_detection.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_prompt_analyzer.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_shutdown.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_track_ai_call.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_anthropic.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_bedrock.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_openai.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_openai_analyzer.py +0 -0
- {frugal_sdk_python-0.5.0 → frugal_sdk_python-0.6.0}/tests/test_wrap_openai_batches.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: frugal-sdk-python
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Instrumentation for Cost-to-Code attribution.
|
|
5
5
|
Project-URL: Homepage, https://frugal.co
|
|
6
6
|
Author: Frugal AI Inc.
|
|
@@ -62,7 +62,7 @@ All configuration is via environment variables. No `init()` call, no configurati
|
|
|
62
62
|
|
|
63
63
|
### Required
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
Console mode (`FRUGAL_METRICS_EXPORTER=console`) needs only `FRUGAL_PROJECT_ID`; every other mode needs both. If a required variable is unset, the wrapper silently returns the client unmodified:
|
|
66
66
|
|
|
67
67
|
| Variable | Purpose |
|
|
68
68
|
| ------------------- | ---------------------------------------------------------- |
|
|
@@ -71,23 +71,27 @@ Set both or the wrapper silently returns the client unmodified:
|
|
|
71
71
|
|
|
72
72
|
### Optional
|
|
73
73
|
|
|
74
|
+
The export endpoint and authentication come only from `FRUGAL_`-prefixed variables, so the SDK never crosses wires with the host's own OTel pipeline: the generic `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_EXPORTER_OTLP_HEADERS` are never read. (The OTel exporter library still reads its own generic TLS, compression, timeout and temporality variables.)
|
|
75
|
+
|
|
74
76
|
| Variable | Purpose |
|
|
75
77
|
| ---------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
76
78
|
| `FRUGAL_CUSTOMER_ID` | Customer ID issued by Frugal. When unset, the tenant label (auto-injected server-side by VMAuth from the bearer token) identifies the customer and `frugal.customer_id` is omitted. |
|
|
77
79
|
| `FRUGAL_REPO_ROOT` | Absolute path to the source root; the strip prefix for `caller_file`. Defaults to the process working directory. |
|
|
78
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector URL for Frugal metrics. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics
|
|
79
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`).
|
|
80
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector URL for Frugal metrics. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics` (also when set but empty). |
|
|
81
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`). Precedence: this variable wins; otherwise the header built from `FRUGAL_API_KEY` (`FRUGAL_METRICS_API_KEY` first), which is sent verbatim. Values set here are percent-decoded (`Bearer%20tok` → `Bearer tok`). A value with no `key=value` pair is treated as missing auth (the API key is not used instead); a blank (whitespace-only) value is treated as unset. |
|
|
80
82
|
| `FRUGAL_COMPONENTS` | Comma-separated logical components (e.g. `ai-handler,reco`). When set, each metric is ALSO emitted once per component (tagged `frugal.component`) on top of the base project series, so usage can be viewed for the whole project or split by component. Deduped, and only the first **10** entries are honored (hard cap, not configurable); the rest are dropped at startup with one debug warning. |
|
|
81
83
|
| `FRUGAL_CUSTOM_PROVIDERS` | Comma-separated allowlist of provider names beyond the built-in registry (e.g. `together_ai,fireworks`) that should pass through verbatim on `gen_ai.system` / `gen_ai.provider.name` instead of being coerced to `custom`. Capped at 10 (extras are coerced) to keep label cardinality bounded. |
|
|
82
|
-
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label
|
|
84
|
+
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`. The vocabulary is the first **20** comma-separated entries by raw position (hard cap, not configurable); empty entries are ignored and duplicates collapsed. `other` is reserved (the coercion sentinel) and is ignored with one warning. A value outside the allowlist is coerced to `other` with a bounded once-per-value warning; when the variable is unset, every value is `other`. One warning fires only when non-empty entries past the 20th are actually dropped (a trailing comma does not warn). |
|
|
85
|
+
| `FRUGAL_METRICS_EXPORTER` | `otlp` (default) or `console` (print metrics to stdout; console mode needs only `FRUGAL_PROJECT_ID`; the API key, endpoint and header variables are ignored). Case-insensitive; any other value warns once and uses `otlp`. |
|
|
86
|
+
| `FRUGAL_METRICS_PROJECT_ID` / `FRUGAL_METRICS_API_KEY` | Namespaced overrides read before `FRUGAL_PROJECT_ID` / `FRUGAL_API_KEY`, for apps that already use those names. An empty value falls through to the bare name. |
|
|
83
87
|
| `FRUGAL_PATH_BLOCK_LIST` | Comma-separated repo-relative paths to skip for caller attribution (see below). |
|
|
84
|
-
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence in milliseconds
|
|
88
|
+
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence in milliseconds, `1`-`2147483647`. Default `60000`; a value outside the range warns once and uses the default. |
|
|
85
89
|
| `FRUGAL_MAX_CALL_SITES` | Cardinality cap — max distinct `(caller_file, caller_function)` pairs to attribute; past it `frugal.caller_file` / `frugal.caller_function` become `<overflow>`. Default `500`. Set to `0` to disable. |
|
|
86
90
|
| `FRUGAL_MAX_MODELS` | Cardinality cap — max distinct model strings to attribute; past it `gen_ai.request.model` / `gen_ai.response.model` become `<overflow>`. One registry shared by both labels, so a request alias and its dated response snapshot each take a slot. Default `100`. Set to `0` to disable. |
|
|
87
91
|
| `FRUGAL_MAX_SERIES_PER_INSTRUMENT` | OpenTelemetry's per-instrument limit on distinct attribute **combinations**, raised from the SDK default. Default `50000`. Set to `0` for effectively unlimited (a 1,000,000 ceiling). Applied **only when the installed `opentelemetry-sdk` accepts a per-instrument cardinality limit** — current releases (through 1.42) do not implement one, so nothing is capped and setting this variable emits one `warn` telling you it has no effect. |
|
|
88
|
-
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls
|
|
92
|
+
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls that run the lightweight prompt analyzer: a decimal in [0, 1], plain or scientific notation (e.g. `0.25`, `1e-1`); anything else warns once and uses `0.1`. Default `0.1`. Set to `0` to disable, `1` for every call. |
|
|
89
93
|
| `FRUGAL_DUPLICATE_REQUEST_DETECTION` | Duplicate-request detection (the `frugal.gen_ai.duplicate_requests` metric) is **on by default** and hashes each chat request in-process (the hash never leaves the SDK). Set to `0`/`false`/`no`/`off` to disable — no hashing, no filter memory, no metric. |
|
|
90
|
-
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `
|
|
94
|
+
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `86400` (1 day). |
|
|
91
95
|
| `FRUGAL_DUPLICATE_CAPACITY` | Expected distinct request signatures per window (sizes the in-memory filter). Default `10000`, max `1000000`. Raise it if the SDK warns the filter is saturated. |
|
|
92
96
|
| `FRUGAL_DUPLICATE_FP_RATE` | Target false-positive rate of the duplicate filter. Default `0.01`, clamped to `[1e-6, 0.5]`. |
|
|
93
97
|
| `FRUGAL_DUPLICATE_GENERATIONS` | Rotation granularity of the duplicate filter's time window. Default `2`, max `16`. |
|
|
@@ -125,7 +129,8 @@ Every metric carries this base attribute set (except
|
|
|
125
129
|
`frugal.gen_ai.internal_errors`, which is emitted from failure paths where
|
|
126
130
|
these attributes may not exist yet and carries only `frugal.project_id`, the
|
|
127
131
|
stage/type, `frugal.instrument` on the `otel_cardinality_overflow` stage, and
|
|
128
|
-
`frugal.customer_id` / `frugal.custom_label` when set
|
|
132
|
+
`frugal.customer_id` / `frugal.custom_label` when set; `frugal.custom_label`
|
|
133
|
+
never on the setup_failed / emission stages):
|
|
129
134
|
|
|
130
135
|
- `frugal.project_id` — from your env vars (`frugal.customer_id` too when set); `frugal.component` is added on the per-component series when `FRUGAL_COMPONENTS` is set
|
|
131
136
|
- `frugal.caller_file`, `frugal.caller_function` — auto-detected from the call stack
|
|
@@ -133,7 +138,7 @@ stage/type, `frugal.instrument` on the `otel_cardinality_overflow` stage, and
|
|
|
133
138
|
- `gen_ai.request.model`, `gen_ai.response.model` — the model used; both draw on one shared registry and become `<overflow>` past `FRUGAL_MAX_MODELS` distinct model strings
|
|
134
139
|
- `gen_ai.operation.name` — `chat`, `text_completion`, `embeddings`, `generate_content`, `execute_tool`, `create_agent`, `invoke_agent`, or `other` (the sentinel an unrecognized operation coerces to)
|
|
135
140
|
- `frugal.batched` — `true` for batch-API paths (OpenAI Batch API submissions; Anthropic Message Batches submissions and results), `false` otherwise
|
|
136
|
-
- `frugal.custom_label` — only on `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`; the value must be in the `FRUGAL_CUSTOM_LABEL` allowlist or it is coerced to `other`
|
|
141
|
+
- `frugal.custom_label` — only on `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`; the value must be in the `FRUGAL_CUSTOM_LABEL` allowlist or it is coerced to `other` (with a once-per-value warning). `custom_label=None` means no label; a non-string value is coerced to `other`
|
|
137
142
|
|
|
138
143
|
The "Additional attributes" column above lists what each metric carries on top of the base set. See [`docs/metrics-reference.md`](docs/metrics-reference.md) for the values each attribute takes, sentinel values for caller fields, and the full internal-error-stage list.
|
|
139
144
|
|
|
@@ -228,7 +233,7 @@ except BaseException as exc:
|
|
|
228
233
|
|
|
229
234
|
`success()` and `error()` finalize the call exactly once and are idempotent — extra calls are ignored.
|
|
230
235
|
|
|
231
|
-
**Metadata** — `track_ai_call` / `start_ai_call` kwargs (all optional): `system` (→ `gen_ai.system`, default `"custom"`), `operation` (default `"chat"`), `request_model` (default `"<unknown>"`), `custom_label` (→ the `frugal.custom_label` label on every metric of the call; trimmed, and coerced to `other` unless in the `FRUGAL_CUSTOM_LABEL` allowlist). To also run the sampled prompt analyzer, pass any of `messages` / `system_prompt` / `tools` / `response_format` / `max_tokens`.
|
|
236
|
+
**Metadata** — `track_ai_call` / `start_ai_call` kwargs (all optional): `system` (→ `gen_ai.system`, default `"custom"`), `operation` (default `"chat"`), `request_model` (default `"<unknown>"`), `custom_label` (→ the `frugal.custom_label` label on every metric of the call except the `frugal.gen_ai.internal_errors` setup_failed / emission points; trimmed, and coerced to `other` unless in the `FRUGAL_CUSTOM_LABEL` allowlist). To also run the sampled prompt analyzer, pass any of `messages` / `system_prompt` / `tools` / `response_format` / `max_tokens`.
|
|
232
237
|
|
|
233
238
|
**Usage** — `set_usage(...)` / `success(...)` kwargs (all optional): `input_tokens`, `output_tokens`, `cache_read_tokens`, `cache_write_tokens`, `reasoning_tokens`, `agent_turns`, `response_model`.
|
|
234
239
|
|
|
@@ -327,14 +332,27 @@ It is **opt-in** because installing signal handlers is intrusive. If you run you
|
|
|
327
332
|
|
|
328
333
|
## Verifying the setup
|
|
329
334
|
|
|
330
|
-
Set
|
|
335
|
+
Set `FRUGAL_METRICS_EXPORTER=console` to print the metrics to stdout instead
|
|
336
|
+
of exporting them. Console mode needs only `FRUGAL_PROJECT_ID`; the API key,
|
|
337
|
+
endpoint and header variables are ignored. An empty
|
|
338
|
+
endpoint does not select the console exporter: it falls through to the default
|
|
339
|
+
Frugal endpoint.
|
|
340
|
+
|
|
341
|
+
```bash
|
|
342
|
+
export FRUGAL_PROJECT_ID=my-project
|
|
343
|
+
export FRUGAL_METRICS_EXPORTER=console
|
|
344
|
+
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
345
|
+
python my_script.py
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Or point the library at a local OTLP/HTTP collector to see what it exports:
|
|
331
349
|
|
|
332
350
|
```bash
|
|
333
|
-
export FRUGAL_API_KEY=
|
|
351
|
+
export FRUGAL_API_KEY=test-key
|
|
334
352
|
export FRUGAL_PROJECT_ID=my-project
|
|
335
|
-
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=
|
|
353
|
+
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/metrics
|
|
336
354
|
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
337
355
|
python my_script.py
|
|
338
356
|
```
|
|
339
357
|
|
|
340
|
-
|
|
358
|
+
The collector should receive metric records with `gen_ai.*` and `frugal.*` attributes every 5 seconds.
|
|
@@ -33,7 +33,7 @@ All configuration is via environment variables. No `init()` call, no configurati
|
|
|
33
33
|
|
|
34
34
|
### Required
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
Console mode (`FRUGAL_METRICS_EXPORTER=console`) needs only `FRUGAL_PROJECT_ID`; every other mode needs both. If a required variable is unset, the wrapper silently returns the client unmodified:
|
|
37
37
|
|
|
38
38
|
| Variable | Purpose |
|
|
39
39
|
| ------------------- | ---------------------------------------------------------- |
|
|
@@ -42,23 +42,27 @@ Set both or the wrapper silently returns the client unmodified:
|
|
|
42
42
|
|
|
43
43
|
### Optional
|
|
44
44
|
|
|
45
|
+
The export endpoint and authentication come only from `FRUGAL_`-prefixed variables, so the SDK never crosses wires with the host's own OTel pipeline: the generic `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_EXPORTER_OTLP_HEADERS` are never read. (The OTel exporter library still reads its own generic TLS, compression, timeout and temporality variables.)
|
|
46
|
+
|
|
45
47
|
| Variable | Purpose |
|
|
46
48
|
| ---------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
47
49
|
| `FRUGAL_CUSTOMER_ID` | Customer ID issued by Frugal. When unset, the tenant label (auto-injected server-side by VMAuth from the bearer token) identifies the customer and `frugal.customer_id` is omitted. |
|
|
48
50
|
| `FRUGAL_REPO_ROOT` | Absolute path to the source root; the strip prefix for `caller_file`. Defaults to the process working directory. |
|
|
49
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector URL for Frugal metrics. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics
|
|
50
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`).
|
|
51
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP collector URL for Frugal metrics. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics` (also when set but empty). |
|
|
52
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`). Precedence: this variable wins; otherwise the header built from `FRUGAL_API_KEY` (`FRUGAL_METRICS_API_KEY` first), which is sent verbatim. Values set here are percent-decoded (`Bearer%20tok` → `Bearer tok`). A value with no `key=value` pair is treated as missing auth (the API key is not used instead); a blank (whitespace-only) value is treated as unset. |
|
|
51
53
|
| `FRUGAL_COMPONENTS` | Comma-separated logical components (e.g. `ai-handler,reco`). When set, each metric is ALSO emitted once per component (tagged `frugal.component`) on top of the base project series, so usage can be viewed for the whole project or split by component. Deduped, and only the first **10** entries are honored (hard cap, not configurable); the rest are dropped at startup with one debug warning. |
|
|
52
54
|
| `FRUGAL_CUSTOM_PROVIDERS` | Comma-separated allowlist of provider names beyond the built-in registry (e.g. `together_ai,fireworks`) that should pass through verbatim on `gen_ai.system` / `gen_ai.provider.name` instead of being coerced to `custom`. Capped at 10 (extras are coerced) to keep label cardinality bounded. |
|
|
53
|
-
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label
|
|
55
|
+
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`. The vocabulary is the first **20** comma-separated entries by raw position (hard cap, not configurable); empty entries are ignored and duplicates collapsed. `other` is reserved (the coercion sentinel) and is ignored with one warning. A value outside the allowlist is coerced to `other` with a bounded once-per-value warning; when the variable is unset, every value is `other`. One warning fires only when non-empty entries past the 20th are actually dropped (a trailing comma does not warn). |
|
|
56
|
+
| `FRUGAL_METRICS_EXPORTER` | `otlp` (default) or `console` (print metrics to stdout; console mode needs only `FRUGAL_PROJECT_ID`; the API key, endpoint and header variables are ignored). Case-insensitive; any other value warns once and uses `otlp`. |
|
|
57
|
+
| `FRUGAL_METRICS_PROJECT_ID` / `FRUGAL_METRICS_API_KEY` | Namespaced overrides read before `FRUGAL_PROJECT_ID` / `FRUGAL_API_KEY`, for apps that already use those names. An empty value falls through to the bare name. |
|
|
54
58
|
| `FRUGAL_PATH_BLOCK_LIST` | Comma-separated repo-relative paths to skip for caller attribution (see below). |
|
|
55
|
-
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence in milliseconds
|
|
59
|
+
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence in milliseconds, `1`-`2147483647`. Default `60000`; a value outside the range warns once and uses the default. |
|
|
56
60
|
| `FRUGAL_MAX_CALL_SITES` | Cardinality cap — max distinct `(caller_file, caller_function)` pairs to attribute; past it `frugal.caller_file` / `frugal.caller_function` become `<overflow>`. Default `500`. Set to `0` to disable. |
|
|
57
61
|
| `FRUGAL_MAX_MODELS` | Cardinality cap — max distinct model strings to attribute; past it `gen_ai.request.model` / `gen_ai.response.model` become `<overflow>`. One registry shared by both labels, so a request alias and its dated response snapshot each take a slot. Default `100`. Set to `0` to disable. |
|
|
58
62
|
| `FRUGAL_MAX_SERIES_PER_INSTRUMENT` | OpenTelemetry's per-instrument limit on distinct attribute **combinations**, raised from the SDK default. Default `50000`. Set to `0` for effectively unlimited (a 1,000,000 ceiling). Applied **only when the installed `opentelemetry-sdk` accepts a per-instrument cardinality limit** — current releases (through 1.42) do not implement one, so nothing is capped and setting this variable emits one `warn` telling you it has no effect. |
|
|
59
|
-
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls
|
|
63
|
+
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls that run the lightweight prompt analyzer: a decimal in [0, 1], plain or scientific notation (e.g. `0.25`, `1e-1`); anything else warns once and uses `0.1`. Default `0.1`. Set to `0` to disable, `1` for every call. |
|
|
60
64
|
| `FRUGAL_DUPLICATE_REQUEST_DETECTION` | Duplicate-request detection (the `frugal.gen_ai.duplicate_requests` metric) is **on by default** and hashes each chat request in-process (the hash never leaves the SDK). Set to `0`/`false`/`no`/`off` to disable — no hashing, no filter memory, no metric. |
|
|
61
|
-
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `
|
|
65
|
+
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `86400` (1 day). |
|
|
62
66
|
| `FRUGAL_DUPLICATE_CAPACITY` | Expected distinct request signatures per window (sizes the in-memory filter). Default `10000`, max `1000000`. Raise it if the SDK warns the filter is saturated. |
|
|
63
67
|
| `FRUGAL_DUPLICATE_FP_RATE` | Target false-positive rate of the duplicate filter. Default `0.01`, clamped to `[1e-6, 0.5]`. |
|
|
64
68
|
| `FRUGAL_DUPLICATE_GENERATIONS` | Rotation granularity of the duplicate filter's time window. Default `2`, max `16`. |
|
|
@@ -96,7 +100,8 @@ Every metric carries this base attribute set (except
|
|
|
96
100
|
`frugal.gen_ai.internal_errors`, which is emitted from failure paths where
|
|
97
101
|
these attributes may not exist yet and carries only `frugal.project_id`, the
|
|
98
102
|
stage/type, `frugal.instrument` on the `otel_cardinality_overflow` stage, and
|
|
99
|
-
`frugal.customer_id` / `frugal.custom_label` when set
|
|
103
|
+
`frugal.customer_id` / `frugal.custom_label` when set; `frugal.custom_label`
|
|
104
|
+
never on the setup_failed / emission stages):
|
|
100
105
|
|
|
101
106
|
- `frugal.project_id` — from your env vars (`frugal.customer_id` too when set); `frugal.component` is added on the per-component series when `FRUGAL_COMPONENTS` is set
|
|
102
107
|
- `frugal.caller_file`, `frugal.caller_function` — auto-detected from the call stack
|
|
@@ -104,7 +109,7 @@ stage/type, `frugal.instrument` on the `otel_cardinality_overflow` stage, and
|
|
|
104
109
|
- `gen_ai.request.model`, `gen_ai.response.model` — the model used; both draw on one shared registry and become `<overflow>` past `FRUGAL_MAX_MODELS` distinct model strings
|
|
105
110
|
- `gen_ai.operation.name` — `chat`, `text_completion`, `embeddings`, `generate_content`, `execute_tool`, `create_agent`, `invoke_agent`, or `other` (the sentinel an unrecognized operation coerces to)
|
|
106
111
|
- `frugal.batched` — `true` for batch-API paths (OpenAI Batch API submissions; Anthropic Message Batches submissions and results), `false` otherwise
|
|
107
|
-
- `frugal.custom_label` — only on `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`; the value must be in the `FRUGAL_CUSTOM_LABEL` allowlist or it is coerced to `other`
|
|
112
|
+
- `frugal.custom_label` — only on `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`; the value must be in the `FRUGAL_CUSTOM_LABEL` allowlist or it is coerced to `other` (with a once-per-value warning). `custom_label=None` means no label; a non-string value is coerced to `other`
|
|
108
113
|
|
|
109
114
|
The "Additional attributes" column above lists what each metric carries on top of the base set. See [`docs/metrics-reference.md`](docs/metrics-reference.md) for the values each attribute takes, sentinel values for caller fields, and the full internal-error-stage list.
|
|
110
115
|
|
|
@@ -199,7 +204,7 @@ except BaseException as exc:
|
|
|
199
204
|
|
|
200
205
|
`success()` and `error()` finalize the call exactly once and are idempotent — extra calls are ignored.
|
|
201
206
|
|
|
202
|
-
**Metadata** — `track_ai_call` / `start_ai_call` kwargs (all optional): `system` (→ `gen_ai.system`, default `"custom"`), `operation` (default `"chat"`), `request_model` (default `"<unknown>"`), `custom_label` (→ the `frugal.custom_label` label on every metric of the call; trimmed, and coerced to `other` unless in the `FRUGAL_CUSTOM_LABEL` allowlist). To also run the sampled prompt analyzer, pass any of `messages` / `system_prompt` / `tools` / `response_format` / `max_tokens`.
|
|
207
|
+
**Metadata** — `track_ai_call` / `start_ai_call` kwargs (all optional): `system` (→ `gen_ai.system`, default `"custom"`), `operation` (default `"chat"`), `request_model` (default `"<unknown>"`), `custom_label` (→ the `frugal.custom_label` label on every metric of the call except the `frugal.gen_ai.internal_errors` setup_failed / emission points; trimmed, and coerced to `other` unless in the `FRUGAL_CUSTOM_LABEL` allowlist). To also run the sampled prompt analyzer, pass any of `messages` / `system_prompt` / `tools` / `response_format` / `max_tokens`.
|
|
203
208
|
|
|
204
209
|
**Usage** — `set_usage(...)` / `success(...)` kwargs (all optional): `input_tokens`, `output_tokens`, `cache_read_tokens`, `cache_write_tokens`, `reasoning_tokens`, `agent_turns`, `response_model`.
|
|
205
210
|
|
|
@@ -298,14 +303,27 @@ It is **opt-in** because installing signal handlers is intrusive. If you run you
|
|
|
298
303
|
|
|
299
304
|
## Verifying the setup
|
|
300
305
|
|
|
301
|
-
Set
|
|
306
|
+
Set `FRUGAL_METRICS_EXPORTER=console` to print the metrics to stdout instead
|
|
307
|
+
of exporting them. Console mode needs only `FRUGAL_PROJECT_ID`; the API key,
|
|
308
|
+
endpoint and header variables are ignored. An empty
|
|
309
|
+
endpoint does not select the console exporter: it falls through to the default
|
|
310
|
+
Frugal endpoint.
|
|
311
|
+
|
|
312
|
+
```bash
|
|
313
|
+
export FRUGAL_PROJECT_ID=my-project
|
|
314
|
+
export FRUGAL_METRICS_EXPORTER=console
|
|
315
|
+
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
316
|
+
python my_script.py
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Or point the library at a local OTLP/HTTP collector to see what it exports:
|
|
302
320
|
|
|
303
321
|
```bash
|
|
304
|
-
export FRUGAL_API_KEY=
|
|
322
|
+
export FRUGAL_API_KEY=test-key
|
|
305
323
|
export FRUGAL_PROJECT_ID=my-project
|
|
306
|
-
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=
|
|
324
|
+
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/metrics
|
|
307
325
|
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
308
326
|
python my_script.py
|
|
309
327
|
```
|
|
310
328
|
|
|
311
|
-
|
|
329
|
+
The collector should receive metric records with `gen_ai.*` and `frugal.*` attributes every 5 seconds.
|
|
@@ -120,8 +120,8 @@ Controls:
|
|
|
120
120
|
`false`/`0`/`no`/`off`, no filter is allocated, no signature is hashed, and the
|
|
121
121
|
metric is not emitted. Any other value keeps detection enabled and emits a
|
|
122
122
|
one-time `warn` diagnostic naming the unrecognized value.
|
|
123
|
-
- `FRUGAL_DUPLICATE_WINDOW_SECONDS` (default `3600`, clamped to `
|
|
124
|
-
|
|
123
|
+
- `FRUGAL_DUPLICATE_WINDOW_SECONDS` (default `3600`, clamped to `86400` — 1
|
|
124
|
+
day) — retention window. Because the filter rotates in `generations` slots,
|
|
125
125
|
a signature is remembered for **at least**
|
|
126
126
|
`window × (generations−1)/generations` and **at most** `window` seconds
|
|
127
127
|
(30–60 min at defaults) — treat the counterfactual cache TTL as that range,
|
|
@@ -133,6 +133,15 @@ Controls:
|
|
|
133
133
|
- `FRUGAL_DUPLICATE_GENERATIONS` (default `2`, clamped to `16`) — rotating-Bloom
|
|
134
134
|
generation count.
|
|
135
135
|
|
|
136
|
+
Exporter selection: `FRUGAL_METRICS_EXPORTER` (`otlp` default, or `console` to
|
|
137
|
+
print every metric to stdout via the OpenTelemetry console exporter; console
|
|
138
|
+
mode needs only `FRUGAL_PROJECT_ID`; the API key, endpoint and header variables
|
|
139
|
+
are ignored, and any other value warns once and uses `otlp`). An empty endpoint variable falls through to the next source and
|
|
140
|
+
never selects console.
|
|
141
|
+
|
|
142
|
+
`FRUGAL_METRIC_EXPORT_INTERVAL_MS` accepts `1` to `2147483647` (default `60000`);
|
|
143
|
+
a value outside that range warns once and uses the default.
|
|
144
|
+
|
|
136
145
|
Out-of-range or non-finite values for any of these fall back to the default (or
|
|
137
146
|
clamp to the bound) with a one-time named `warn` through the debug callback.
|
|
138
147
|
|
|
@@ -191,18 +200,18 @@ Reading `duplicate_requests` correctly:
|
|
|
191
200
|
|
|
192
201
|
| Attribute | Value |
|
|
193
202
|
| ------------------------- | ---------------------------------------------------------- |
|
|
194
|
-
| `frugal.customer_id` | From `FRUGAL_CUSTOMER_ID`
|
|
195
|
-
| `frugal.project_id` | From `FRUGAL_PROJECT_ID`
|
|
203
|
+
| `frugal.customer_id` | From `FRUGAL_CUSTOMER_ID` — present only when set |
|
|
204
|
+
| `frugal.project_id` | From `FRUGAL_METRICS_PROJECT_ID`, else `FRUGAL_PROJECT_ID` |
|
|
196
205
|
| `frugal.component` | From each entry in `FRUGAL_COMPONENTS` — present only on the per-component series, absent on the base project series |
|
|
197
206
|
| `frugal.caller_file` | Source file repo-relative to `FRUGAL_REPO_ROOT` |
|
|
198
207
|
| `frugal.caller_function` | Function/method name at the call site |
|
|
199
|
-
| `frugal.error.type` | Exception class name (error-path metrics only)
|
|
200
|
-
| `frugal.batched` | `true` when the metric came from a provider batch-API path, `false` otherwise. Present on every metric. Batch submissions (OpenAI `batches.create`, Anthropic `messages.batches.create`) emit `frugal.gen_ai.calls` with `batched="true"` — one point per request for Anthropic (the requests array is in hand, grouped by model), one per submission for OpenAI (per-request counts are not reported at submission time, so OpenAI batched call volume is a floor, never an overcount). Iterating Anthropic `messages.batches.results()` additionally emits per-entry `gen_ai.client.token.usage` (+ cache metrics) with `batched="true"`, attributed to the call site that submitted the batch when the submission happened in the same process (bounded in-process memory; falls back to the results call site). OpenAI batch token usage lives in the output file read through the generic files API and is not intercepted. The manual track API accepts a `batched` flag for hand-instrumented batch paths. An app on an SDK predating batch support reads `false` on genuinely batched traffic — the label reflects what the deployed SDK can see. |
|
|
208
|
+
| `frugal.error.type` | Exception class name (error-path metrics only); stripped from `frugal.gen_ai.history_depth` and `frugal.gen_ai.duplicate_requests` so success and error points stay additive |
|
|
209
|
+
| `frugal.batched` | `true` when the metric came from a provider batch-API path, `false` otherwise. Present on every metric of the call except the `frugal.gen_ai.internal_errors` points. Batch submissions (OpenAI `batches.create`, Anthropic `messages.batches.create`) emit `frugal.gen_ai.calls` with `batched="true"` — one point per request for Anthropic (the requests array is in hand, grouped by model), one per submission for OpenAI (per-request counts are not reported at submission time, so OpenAI batched call volume is a floor, never an overcount). Iterating Anthropic `messages.batches.results()` additionally emits per-entry `gen_ai.client.token.usage` (+ cache metrics) with `batched="true"`, attributed to the call site that submitted the batch when the submission happened in the same process (bounded in-process memory; falls back to the results call site). OpenAI batch token usage lives in the output file read through the generic files API and is not intercepted. The manual track API accepts a `batched` flag for hand-instrumented batch paths. An app on an SDK predating batch support reads `false` on genuinely batched traffic — the label reflects what the deployed SDK can see. |
|
|
201
210
|
| `frugal.structured_output_mode` | `none` \| `json_object` \| `json_schema` \| `tools`. Derived from request params by precedence: non-empty `tools` → `tools`; else `response_format.type` `json_object` / `json_schema` → the matching value; else `none`. On `frugal.gen_ai.output_cap_utilization` (when `max_tokens` set), and on `frugal.gen_ai.calls` for every call with a request payload (omitted when there is no payload). |
|
|
202
211
|
| `frugal.block` | `system` \| `user` \| `tools` \| `schema`. Only on `frugal.gen_ai.cache_prefix_stable`. |
|
|
203
212
|
| `frugal.stable` | `true` when the block hash matches the previous call at this site. Only on `frugal.gen_ai.cache_prefix_stable`. |
|
|
204
213
|
| `frugal.history_bucket` | Bounded bucket of `messages.length`: `0` \| `1-2` \| `3-5` \| `6-10` \| `11-20` \| `21-50` \| `51+`. On `frugal.gen_ai.calls` and `gen_ai.client.token.usage`, and only for chat calls (payload has a `messages` array); omitted otherwise. 100% (not analyzer-sampled). |
|
|
205
|
-
| `frugal.custom_label` | Custom label (e.g. a business unit) passed as `custom_label=` to `track_ai_call` / `start_ai_call` — never set by the provider wrappers. When passed, it rides
|
|
214
|
+
| `frugal.custom_label` | Custom label (e.g. a business unit) passed as `custom_label=` to `track_ai_call` / `start_ai_call` — never set by the provider wrappers. When passed, it rides every metric of that call (success and error paths alike), except the `frugal.gen_ai.internal_errors` `setup_failed`/`emission` points, which carry only project, customer and error type. The value is trimmed of surrounding whitespace (matching the Node SDK), then must match the closed vocabulary from the first 20 comma-separated entries of `FRUGAL_CUSTOM_LABEL` by raw position (hard cap, not configurable; empties ignored, duplicates collapsed) or it is coerced to `other` with a bounded once-per-value warning. `other` is reserved as the coercion sentinel: an `other` entry in the env var is ignored with a warning. With the variable unset, every value is `other`. A non-string value is coerced to `other` with a type warning; `None` means no label. A truncation warning fires only when non-empty entries past the 20th are dropped. Omitted when not passed. |
|
|
206
215
|
| `frugal.dup_state` | `first_seen` \| `repeat_seen` — whether this request's canonical signature was already seen within the duplicate-detection window. Only on `frugal.gen_ai.duplicate_requests`. |
|
|
207
216
|
|
|
208
217
|
### Sentinel values for `frugal.caller_file` / `frugal.caller_function`
|
|
@@ -257,9 +266,10 @@ attribute describing where our own code failed. Non-exhaustive list:
|
|
|
257
266
|
|
|
258
267
|
| Stage | When it fires |
|
|
259
268
|
| ----------------------- | ---------------------------------------------------------------------- |
|
|
260
|
-
| `setup_failed` | Our wrapper setup (OTel init, caller resolution) threw; original call continued normally |
|
|
269
|
+
| `setup_failed` | Our wrapper setup (OTel init, caller resolution) threw; original call continued normally. Emitted by the provider wrappers and the manual track API (`track_ai_call` / `start_ai_call`); carries `frugal.project_id`, `frugal.customer_id` (when set) and `frugal.internal_error.type` |
|
|
261
270
|
| `stream_proxy_failed` | Constructing the stream wrapper threw; original response returned unwrapped |
|
|
262
|
-
| `emission` | Emitting a success/error metric threw
|
|
271
|
+
| `emission` | Emitting a success/error metric threw. Emitted by the provider wrappers and the manual track API; carries the same project/customer identity and `frugal.internal_error.type` as `setup_failed` |
|
|
272
|
+
| `call_shape` | Deriving history_depth / history_bucket / structured_output_mode from the request payload threw; carries `frugal.internal_error.type`; core metrics still emitted |
|
|
263
273
|
| `duplicate_detection` | The duplicate-request path (signature, filter, or emission) threw — including the one-time filter allocation failure; carries `frugal.internal_error.type` with the exception class |
|
|
264
274
|
| `cardinality_cap_hit` | `FRUGAL_MAX_CALL_SITES` was just reached (once per process) |
|
|
265
275
|
| `model_cap_hit` | `FRUGAL_MAX_MODELS` was just reached (once per process); further distinct models are reported as `<overflow>` |
|
|
@@ -271,7 +281,8 @@ caller/provider/model attributes may not exist yet. Its points carry only
|
|
|
271
281
|
`frugal.project_id`, `frugal.internal_error.stage` (plus
|
|
272
282
|
`frugal.internal_error.type` where noted, and `frugal.instrument` on the
|
|
273
283
|
`otel_cardinality_overflow` stage), and — when set — `frugal.customer_id` and
|
|
274
|
-
`frugal.custom_label
|
|
284
|
+
`frugal.custom_label` (`setup_failed` and `emission` never carry the custom
|
|
285
|
+
label).
|
|
275
286
|
|
|
276
287
|
## Provider coverage
|
|
277
288
|
|
|
@@ -10,39 +10,42 @@ pip install openai anthropic
|
|
|
10
10
|
|
|
11
11
|
## Configure (env vars only)
|
|
12
12
|
|
|
13
|
-
Required —
|
|
13
|
+
Required — console mode (`FRUGAL_METRICS_EXPORTER=console`) needs only `FRUGAL_PROJECT_ID`; every other mode needs both. The library silently no-ops if a required variable is unset:
|
|
14
14
|
|
|
15
15
|
| Var | Value |
|
|
16
16
|
| ------------------- | ------------------------------------------------------------------- |
|
|
17
17
|
| `FRUGAL_API_KEY` | Per-customer bearer token issued by Frugal; builds the export `Authorization` header. (Or supply the bearer yourself via `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS`.) |
|
|
18
18
|
| `FRUGAL_PROJECT_ID` | The Frugal project this deployment belongs to. Stats are attributed per project, so a shared repo across microservices no longer collides. |
|
|
19
19
|
|
|
20
|
-
Optional
|
|
20
|
+
Optional — the export endpoint and authentication come only from
|
|
21
|
+
`FRUGAL_`-prefixed variables, so the SDK never crosses wires with the host's
|
|
22
|
+
own OTel pipeline: the generic `OTEL_EXPORTER_OTLP_ENDPOINT` and
|
|
23
|
+
`OTEL_EXPORTER_OTLP_HEADERS` are never read. (The OTel exporter library still
|
|
24
|
+
reads its own generic TLS, compression, timeout and temporality variables.)
|
|
21
25
|
|
|
22
26
|
| Var | Purpose |
|
|
23
27
|
| ------------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
24
28
|
| `FRUGAL_CUSTOMER_ID` | Customer ID issued by Frugal. When unset, the tenant label (auto-injected server-side by VMAuth from the bearer token) identifies the customer and `frugal.customer_id` is omitted. |
|
|
25
29
|
| `FRUGAL_REPO_ROOT` | Absolute path to the source root; the strip prefix for `caller_file`. Defaults to the process working directory. |
|
|
26
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | Frugal-specific OTLP collector URL. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics
|
|
27
|
-
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`).
|
|
30
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT` | Frugal-specific OTLP collector URL. Defaults to `https://metrics.frugal.co/opentelemetry/v1/metrics` (also when set but empty). |
|
|
31
|
+
| `FRUGAL_OTEL_EXPORTER_OTLP_HEADERS` | Export auth headers (e.g. `Authorization=Bearer ...`). Precedence: this variable wins; otherwise the header built from `FRUGAL_API_KEY` (`FRUGAL_METRICS_API_KEY` first), which is sent verbatim. Values set here are percent-decoded (`Bearer%20tok` → `Bearer tok`). A value with no `key=value` pair is treated as missing auth (the API key is not used instead); a blank (whitespace-only) value is treated as unset. |
|
|
28
32
|
| `FRUGAL_COMPONENTS` | Comma-separated logical components (e.g. `ai-handler,reco`). When set, each metric is ALSO emitted once per component (tagged `frugal.component`) on top of the base project series, so usage can be viewed for the whole project or split by component. The list is deduped (order-preserving) and only the first **10** entries are honored (hard cap, not configurable); the rest are dropped at startup with one debug warning. See [Cardinality caps](#cardinality-caps). |
|
|
29
|
-
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label
|
|
33
|
+
| `FRUGAL_CUSTOM_LABEL` | Comma-separated allowlist of values for the `frugal.custom_label` label (e.g. `billing,search`). Applies only to `track_ai_call` / `start_ai_call` calls that pass `custom_label=...`. The vocabulary is the first **20** comma-separated entries by raw position (hard cap, not configurable); empty entries are ignored and duplicates collapsed. `other` is reserved (the coercion sentinel) and is ignored with one warning. A value outside the allowlist is coerced to `other` with a bounded once-per-value warning; when the variable is unset, every value is `other`. One warning fires only when non-empty entries past the 20th are actually dropped (a trailing comma does not warn). |
|
|
34
|
+
| `FRUGAL_METRICS_EXPORTER` | `otlp` (default) or `console` (print metrics to stdout; console mode needs only `FRUGAL_PROJECT_ID`; the API key, endpoint and header variables are ignored). Case-insensitive; any other value warns once and uses `otlp`. |
|
|
35
|
+
| `FRUGAL_METRICS_PROJECT_ID` / `FRUGAL_METRICS_API_KEY` | Namespaced overrides read before `FRUGAL_PROJECT_ID` / `FRUGAL_API_KEY`, for apps that already use those names. An empty value falls through to the bare name. |
|
|
30
36
|
| `FRUGAL_PATH_BLOCK_LIST` | Comma-separated repo-relative paths to skip when attributing the caller (see below). |
|
|
31
|
-
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence
|
|
37
|
+
| `FRUGAL_METRIC_EXPORT_INTERVAL_MS` | Export cadence in ms, `1`-`2147483647`. Default `60000`; a value outside the range warns once and uses the default. |
|
|
32
38
|
| `FRUGAL_MAX_CALL_SITES` | Cardinality cap — max distinct `(caller_file, caller_function)` pairs to attribute before overflow. Default `500`, set to `0` to disable. See [Cardinality caps](#cardinality-caps). |
|
|
33
39
|
| `FRUGAL_MAX_MODELS` | Cardinality cap — max distinct model strings to attribute before `gen_ai.request.model` / `gen_ai.response.model` overflow to `<overflow>`. One registry shared by both labels, so a request alias and its dated response snapshot each take a slot. Default `100`, set to `0` to disable. See [Cardinality caps](#cardinality-caps). |
|
|
34
40
|
| `FRUGAL_MAX_SERIES_PER_INSTRUMENT` | OpenTelemetry's per-instrument limit on distinct attribute **combinations**, raised from the SDK default. Default `50000`, set to `0` for effectively unlimited (a 1,000,000 ceiling). See [Cardinality caps](#cardinality-caps). |
|
|
35
|
-
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls
|
|
41
|
+
| `FRUGAL_PROMPT_ANALYSIS_SAMPLE_RATE` | Fraction of calls where the lightweight prompt analyzer runs and emits derived metrics (`noise_ratio`, `cache_prefix_stable`, `few_shot_*`, …): a decimal in [0, 1], plain or scientific notation (e.g. `0.25`, `1e-1`); anything else warns once and uses `0.1`. Default `0.1`. Set to `0` to disable, `1` for every call. See [Prompt analysis](#prompt-analysis). |
|
|
36
42
|
| `FRUGAL_DUPLICATE_REQUEST_DETECTION` | Duplicate-request detection (`frugal.gen_ai.duplicate_requests`) is **on by default**: every chat request is canonicalized and SHA-256 hashed in-process to classify it `first_seen`/`repeat_seen`. Set to `0`/`false`/`no`/`off` to turn it off entirely (no hashing, no filter memory, no metric). |
|
|
37
|
-
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `
|
|
43
|
+
| `FRUGAL_DUPLICATE_WINDOW_SECONDS` | How long a request signature is remembered for duplicate detection. Default `3600`, max `86400` (1 day). |
|
|
38
44
|
| `FRUGAL_DUPLICATE_CAPACITY` | Expected distinct request signatures per window — sizes the in-memory duplicate filter. Default `10000`, max `1000000`. Raise it if the debug callback warns the filter is saturated. If the filter's memory can't be allocated at all, detection disables itself for the rest of the process (one-time `error` diagnostic + one `internal_errors` point). |
|
|
39
45
|
| `FRUGAL_DUPLICATE_FP_RATE` | Target false-positive rate of the duplicate filter. Default `0.01`, clamped to `[1e-6, 0.5]`. |
|
|
40
46
|
| `FRUGAL_DUPLICATE_GENERATIONS` | Rotation granularity of the duplicate filter's time window. Default `2`, max `16`. |
|
|
41
47
|
| `FRUGAL_DISABLED` | Kill switch. Set to `1` to disable without uninstalling. |
|
|
42
48
|
|
|
43
|
-
Standard `OTEL_EXPORTER_OTLP_*` env vars are honored as fallbacks if the
|
|
44
|
-
`FRUGAL_*` variants are unset.
|
|
45
|
-
|
|
46
49
|
## Wrap your clients
|
|
47
50
|
|
|
48
51
|
**OpenAI**
|
|
@@ -296,7 +299,8 @@ extra calls are ignored.
|
|
|
296
299
|
**Metadata** — `track_ai_call` / `start_ai_call` kwargs (all optional):
|
|
297
300
|
`system` (→ `gen_ai.system`, default `"custom"`), `operation` (default
|
|
298
301
|
`"chat"`), `request_model` (default `"<unknown>"`), `custom_label` (→ the
|
|
299
|
-
`frugal.custom_label` label on every metric of the call
|
|
302
|
+
`frugal.custom_label` label on every metric of the call except the
|
|
303
|
+
`frugal.gen_ai.internal_errors` setup_failed / emission points; values outside the
|
|
300
304
|
`FRUGAL_CUSTOM_LABEL` allowlist become `other`). To also run the sampled
|
|
301
305
|
prompt analyzer, pass any of `messages` / `system_prompt` / `tools` /
|
|
302
306
|
`response_format` / `max_tokens`.
|
|
@@ -514,16 +518,28 @@ your handler).
|
|
|
514
518
|
|
|
515
519
|
## Verifying the setup
|
|
516
520
|
|
|
517
|
-
|
|
518
|
-
|
|
521
|
+
Set `FRUGAL_METRICS_EXPORTER=console` to print the metrics to stdout instead
|
|
522
|
+
of exporting them. Console mode needs only `FRUGAL_PROJECT_ID`; the API key,
|
|
523
|
+
endpoint and header variables are ignored. An empty
|
|
524
|
+
endpoint does not select the console exporter: it falls through to the default
|
|
525
|
+
Frugal endpoint.
|
|
526
|
+
|
|
527
|
+
```bash
|
|
528
|
+
export FRUGAL_PROJECT_ID=my-project
|
|
529
|
+
export FRUGAL_METRICS_EXPORTER=console
|
|
530
|
+
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
531
|
+
python my_script.py
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
Or point the library at a local OTLP/HTTP collector to see what it exports:
|
|
519
535
|
|
|
520
536
|
```bash
|
|
521
|
-
export FRUGAL_API_KEY=
|
|
537
|
+
export FRUGAL_API_KEY=test-key
|
|
522
538
|
export FRUGAL_PROJECT_ID=my-project
|
|
523
|
-
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=
|
|
539
|
+
export FRUGAL_OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/metrics
|
|
524
540
|
export FRUGAL_METRIC_EXPORT_INTERVAL_MS=5000
|
|
525
541
|
python my_script.py
|
|
526
542
|
```
|
|
527
543
|
|
|
528
|
-
Every five seconds
|
|
544
|
+
Every five seconds the collector should receive metric records with
|
|
529
545
|
`gen_ai.*` and `frugal.*` attributes.
|