aid-runtime-python 0.1.95__py3-none-any.whl → 0.1.96__py3-none-any.whl

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.
aid_runtime/__init__.py CHANGED
@@ -72,6 +72,7 @@ from aid_runtime.cost import (
72
72
  )
73
73
  from aid_runtime.decisions import (
74
74
  DEFAULT_TYPESAFE_BASE_URL,
75
+ JEV_INPUT_USD_PER_MTOK,
75
76
  JEV_MOVING_ALIASES,
76
77
  TYPESAFE_API_KEY_ENVS,
77
78
  TYPESAFE_BASE_URL_ENV,
@@ -79,6 +80,7 @@ from aid_runtime.decisions import (
79
80
  DecisionClient,
80
81
  DecisionQuestion,
81
82
  DecisionResponse,
83
+ decision_cost_usd,
82
84
  decision_key,
83
85
  is_reproducible_model,
84
86
  )
@@ -295,7 +297,7 @@ from aid_runtime.types import (
295
297
  )
296
298
 
297
299
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
298
- __version__ = "0.1.95"
300
+ __version__ = "0.1.96"
299
301
 
300
302
  __all__ = [
301
303
  "ANTHROPIC_VERSION",
@@ -311,6 +313,7 @@ __all__ = [
311
313
  "GEN_AI_OPERATION_CHAT",
312
314
  "INDEX_KEY",
313
315
  "ITEM_KEY",
316
+ "JEV_INPUT_USD_PER_MTOK",
314
317
  "JEV_MOVING_ALIASES",
315
318
  "LIMITER_KEY",
316
319
  "MAX_STEP_DEPTH",
@@ -499,6 +502,7 @@ __all__ = [
499
502
  "chunker_for",
500
503
  "cite",
501
504
  "create_store",
505
+ "decision_cost_usd",
502
506
  "decision_key",
503
507
  "default_bedrock_base_url",
504
508
  "default_metrics",
aid_runtime/decisions.py CHANGED
@@ -38,9 +38,12 @@ from typing import Any, Literal
38
38
  import httpx
39
39
 
40
40
  from aid_runtime.errors import ConfigurationError, ProviderError, ProviderResponseError
41
+ from aid_runtime.tracing import llm_span, record_attributes, record_cost, record_usage
42
+ from aid_runtime.types import TokenUsage
41
43
 
42
44
  __all__ = [
43
45
  "DEFAULT_TYPESAFE_BASE_URL",
46
+ "JEV_INPUT_USD_PER_MTOK",
44
47
  "JEV_MOVING_ALIASES",
45
48
  "TYPESAFE_API_KEY_ENVS",
46
49
  "TYPESAFE_BASE_URL_ENV",
@@ -48,6 +51,7 @@ __all__ = [
48
51
  "DecisionClient",
49
52
  "DecisionQuestion",
50
53
  "DecisionResponse",
54
+ "decision_cost_usd",
51
55
  "decision_key",
52
56
  "is_reproducible_model",
53
57
  ]
@@ -64,6 +68,11 @@ TYPESAFE_BASE_URL_ENV = "TYPESAFE_BASE_URL"
64
68
  #: ``reproducible=True``.
65
69
  JEV_MOVING_ALIASES = frozenset({"jev-latest", "jev-preview"})
66
70
 
71
+ #: Jev bills input tokens at $42 per billion ($0.042 per million) and states that
72
+ #: output tokens are free. A call therefore needs an input row only, and a
73
+ #: decision run must never be reported as unpriceable.
74
+ JEV_INPUT_USD_PER_MTOK = 0.042
75
+
67
76
  QuestionType = Literal["choice", "score", "noul"]
68
77
 
69
78
  _QUESTION_TYPES: frozenset[str] = frozenset({"choice", "score", "noul"})
@@ -93,6 +102,19 @@ def is_reproducible_model(model: str) -> bool:
93
102
  return model not in JEV_MOVING_ALIASES
94
103
 
95
104
 
105
+ def decision_cost_usd(input_tokens: int | None) -> float:
106
+ """Price one decision call from its input tokens alone.
107
+
108
+ Output tokens are free by contract, so they are deliberately not part of the
109
+ arithmetic: a caller that passed a total would overcharge the run. ``None``
110
+ means the vendor reported no usage; :meth:`DecisionClient.decide` then records
111
+ no cost at all, so the call stays unknown rather than free.
112
+ """
113
+ if input_tokens is None:
114
+ return 0.0
115
+ return (input_tokens / 1_000_000) * JEV_INPUT_USD_PER_MTOK
116
+
117
+
96
118
  @dataclass(frozen=True, slots=True)
97
119
  class DecisionQuestion:
98
120
  """One typed question.
@@ -228,8 +250,22 @@ class DecisionClient:
228
250
  "state": state,
229
251
  "questions": {key: question.as_payload() for key, question in questions.items()},
230
252
  }
231
- body = await self._post(payload)
232
- return self._parse(body, questions)
253
+ # A decision is billed like any other model call, so it gets the same
254
+ # span: `aid cost` and the eval budget read `aid.cost.usd` off it.
255
+ with llm_span(model=model, provider=self._name, operation="decision") as span:
256
+ body = await self._post(payload)
257
+ response = self._parse(body, questions)
258
+ if response.input_tokens is not None:
259
+ usage = TokenUsage(
260
+ input_tokens=response.input_tokens,
261
+ output_tokens=response.output_tokens or 0,
262
+ )
263
+ record_usage(span, usage)
264
+ record_cost(span, decision_cost_usd(usage.input_tokens))
265
+ # The vendor echoes the version that actually answered, which is the
266
+ # only record of what a moving alias resolved to.
267
+ record_attributes(span, response_model=response.model)
268
+ return response
233
269
 
234
270
  async def close(self) -> None:
235
271
  if self._closed:
aid_runtime/tracing.py CHANGED
@@ -14,8 +14,8 @@ that must not decide a host application's telemetry setup for it.
14
14
  from __future__ import annotations
15
15
 
16
16
  import contextlib
17
- from collections.abc import Iterator
18
- from typing import TYPE_CHECKING, Any
17
+ from collections.abc import Iterator, Sequence
18
+ from typing import Any, Protocol
19
19
 
20
20
  from opentelemetry import trace
21
21
  from opentelemetry.trace import Span, SpanKind, Status, StatusCode
@@ -23,12 +23,10 @@ from opentelemetry.trace import Span, SpanKind, Status, StatusCode
23
23
  from aid_runtime.local_trace import finish_span, wrap_span
24
24
  from aid_runtime.types import TokenUsage
25
25
 
26
- if TYPE_CHECKING:
27
- from aid_runtime.agents.guardrails import GuardrailRecord
28
-
29
26
  __all__ = [
30
27
  "ATTRIBUTES",
31
28
  "GEN_AI_OPERATION_CHAT",
29
+ "GuardrailEvent",
32
30
  "agent_span",
33
31
  "aid_attributes",
34
32
  "get_tracer",
@@ -226,7 +224,29 @@ def record_hitl(
226
224
  span.add_event("aid.hitl", attributes)
227
225
 
228
226
 
229
- def record_guardrail(span: Span, record: GuardrailRecord) -> None:
227
+ class GuardrailEvent(Protocol):
228
+ """The fields :func:`record_guardrail` reads from a fired guardrail.
229
+
230
+ Structural on purpose. Importing ``GuardrailRecord`` would close a module cycle --
231
+ ``guardrails`` imports the decision client, which imports this module, which would
232
+ import ``guardrails`` again -- and a protocol keeps the annotation honest without
233
+ the import.
234
+ """
235
+
236
+ @property
237
+ def guardrail(self) -> str: ...
238
+
239
+ @property
240
+ def stage(self) -> str: ...
241
+
242
+ @property
243
+ def action(self) -> str: ...
244
+
245
+ @property
246
+ def policies(self) -> Sequence[str]: ...
247
+
248
+
249
+ def record_guardrail(span: Span, record: GuardrailEvent) -> None:
230
250
  """Record one guardrail hit as an ``aid.guardrail`` span event."""
231
251
  span.add_event(
232
252
  "aid.guardrail",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.95
3
+ Version: 0.1.96
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -1,9 +1,9 @@
1
- aid_runtime/__init__.py,sha256=hhoY33U-IBJSbNMTsPg_gNnhxyEpsP5aGTP1U6-q_ag,12421
1
+ aid_runtime/__init__.py,sha256=uwsi41AfQfY4kN68MjH06XNsfD4Ruj6vY28mdrFRDD4,12527
2
2
  aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
3
3
  aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
4
4
  aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
5
5
  aid_runtime/cost.py,sha256=Hc7X6mtkeanL9eJ1lEZZCQ9LxZurhL1it0gm3s9LpQE,5465
6
- aid_runtime/decisions.py,sha256=6rkpFODb0PAQ9cqtKuz5ZRA19C3Ljwz1aGWKcovVSc4,13859
6
+ aid_runtime/decisions.py,sha256=1V0d9EttfwTuh8jgtC5X3wQDTZrl20ZREcs6VqIdcHY,15662
7
7
  aid_runtime/errors.py,sha256=TKkZyZZcFTl5yxNKlA8jUF4ORpYHLGFEvc6r9FK4X70,12572
8
8
  aid_runtime/gateway.py,sha256=p0qsaQUAYrwSzlWwpUi1wY6pdOeU98QjsWLzq8045Fo,18267
9
9
  aid_runtime/local_trace.py,sha256=eg5LDsg1VfNmJNqs8KjtdzTjSqg7SWTXunLwg-b3IcM,7870
@@ -13,7 +13,7 @@ aid_runtime/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  aid_runtime/steps.py,sha256=44v8-ebN77VGZePmfhBYi0KORFBrH2GWFSzlfczaJhs,12047
14
14
  aid_runtime/structured.py,sha256=Uga-VoGJN56lUtr-M3nmUjAKGF32cAAk-p7WceUgPzg,5061
15
15
  aid_runtime/tools.py,sha256=7LeaPI5aJioNe3vexUv94tvIRpOwxj1Ujt723qvO3UI,9906
16
- aid_runtime/tracing.py,sha256=WEnmXRfRPEb5eRpH2JWZlnim7V4IHDv4SV2HS_qjUrU,9081
16
+ aid_runtime/tracing.py,sha256=q7v1GQ0FaYOq4uWfGMhloXwQb3fXjxE0ixi6Yij9OJQ,9624
17
17
  aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
18
18
  aid_runtime/agents/__init__.py,sha256=Qkcjm0YokmObQWRB3G_xFd_LI4sku_GajDbE_7TzyF8,2488
19
19
  aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
@@ -51,7 +51,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
51
51
  aid_runtime/retrieval/retriever.py,sha256=hVLP0xOLKioYsa-aS1akyJl5p-WbpVjm8OfCw26m4CE,13941
52
52
  aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
53
53
  aid_runtime/retrieval/types.py,sha256=8vKP21rmE9LglnB5gtv0dLBIkXzaPqdG3-GS98K0gpc,13498
54
- aid_runtime_python-0.1.95.dist-info/METADATA,sha256=9zG6VmRnHRX23EssZU6sMfAiasijfh7EgHEcTGKA6HA,17005
55
- aid_runtime_python-0.1.95.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
56
- aid_runtime_python-0.1.95.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
57
- aid_runtime_python-0.1.95.dist-info/RECORD,,
54
+ aid_runtime_python-0.1.96.dist-info/METADATA,sha256=ezB_vP8FTKeo4AlhZQI5f5eXj_lrDFRBFBlWCf3XWnw,17005
55
+ aid_runtime_python-0.1.96.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
56
+ aid_runtime_python-0.1.96.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
57
+ aid_runtime_python-0.1.96.dist-info/RECORD,,