Utilities
Provider and mk_provider_client construct async fastspec.OpenAPIClient instances with retries=0 and follow_redirects=False so _retry_call is the sole retry layer and redirects never forward auth headers. Custom base_url overrides skip provider.env unless explicit credentials are passed.
Built-in per-token pricing and context limits live in model_info with no external downloads; extend or override rates locally with register_model_info, and compute tiered USD cost with calc_cost. _classify_error upgrades context-limit APIError failures to non-retryable ContextWindowExceededError.
Usage
def Usage(
prompt_tokens:int=0, # Input token count
completion_tokens:int=0, # Output token count (usually 0 for decision models)
total_tokens:int=0, # Total token count
cached_tokens:int=0, # Input tokens served from cache
cache_creation_tokens:int=0, # Input tokens written to cache
reasoning_tokens:int=0, # Reasoning tokens if reported
raw:dict=None, # Original provider usage dict
):Normalized token usage across decision providers.
Provider
def Provider(
name:str, # Canonical provider name, e.g. 'jev', 'gemini', 'openai'
spec:fastspec.spec.SpecParser, # Parsed `fastspec` specification
env:str, # Environment variable holding the default API key
auth, # `'bearer'`, a header name (e.g. `'x-goog-api-key'`), or callable `(api_key, oauth_token) -> dict`
op:str, # Dotted `OpenAPIClient` operation path, e.g. `'systemone.evaluate'` or `'decisions.evaluate'`
mk_payload, # Callable `(state, questions, model=..., **kwargs) -> dict`
req_hdrs=None, # Optional callable `(body, headers=None, **kwargs) -> dict`
norm_response=None, # Callable `(resp, model=None, questions=None) -> Decision`
default_model:str=None, # Default model used when `decide()`/`adecide()` omits `model`
opt_keys:tuple=(), # Provider-specific kwarg names accepted by `mk_payload` or `req_hdrs`
base_url:str=None, # Default base URL; defaults to `spec.base_url`
):Declarative provider definition bundling spec, auth, payload translation, response normalization, and default model.
Provider.op_fn
def op_fn(
cli:fastspec.oapi.OpenAPIClient
):Return the bound OpenAPIClient operation callable named by self.op.
norm_provider_name
def norm_provider_name(
name:str | None
)->str | None:Map provider aliases (such as 'typesafe') to their canonical provider name.
register_provider
def register_provider(
p:fastdecisions.utils.Provider
)->fastdecisions.utils.Provider:Register p in providers under p.name and return it.
mk_provider_client
def mk_provider_client(
provider:fastdecisions.utils.Provider | str, # Registered provider name or `Provider` instance
api_key:str=None, # Explicit API key; reads `provider.env` only when using the default `base_url`
oauth_token:str=None, # Optional OAuth bearer token (mutually exclusive with `api_key`)
base_url:str=None, # Optional base URL override (never sends built-in env keys without explicit creds)
xtra_hdrs:dict=None, # Additional headers merged onto every request
timeout:float=60.0, # Request timeout in seconds
)->fastspec.oapi.OpenAPIClient:Build an async fastspec.OpenAPIClient for provider with retries=0 and follow_redirects=False.
get_api_key
def get_api_key(
api_key:str | None, # Explicit key override, or None to read from the environment
env_var:str, # Environment variable name holding the provider key
)->str:Return api_key or read env_var, raising ValueError if neither is set.
split_vendor
def split_vendor(
model:str
):Split 'vendor/model' into (provider, model) with aliases normalized, or (None, model) when unprefixed.
resolve_model
def resolve_model(
model:str, # Bare model name, alias, or `'vendor/alias'`
aliases:dict=None, # Alias map; defaults to `model_aliases`
)->str:Expand a short model alias while preserving any 'vendor/' prefix.
resize_b64
def resize_b64(
b64:str, # Base64-encoded image payload
max_sz:int, # Maximum allowed long-edge dimension in pixels
)->str:Downscale a base64 image so its longest edge is at most max_sz, preserving aspect ratio.
infer_api_name
def infer_api_name(
model:str
):Infer the canonical API name ('jev', 'gemini', or 'openai') from model.
register_model_info
def register_model_info(
model:str, # Model identifier, e.g. 'jev-1.14.0'
provider:str=None, # Provider name; inferred from `model` when omitted
**info
):Register or override built-in metadata for (provider, model).
get_model_info
def get_model_info(
model:str, # Model identifier, e.g. 'jev-1.14.0', 'gemini-decision-1', 'gpt-6-luna'
provider:str=None, # Optional provider override; inferred from `model` when omitted
):Return built-in metadata for (provider, model), or an empty AttrDict when unknown.
price_tier
def price_tier(
meta:dict, n_tokens:int
)->str:Return the _above_<N>k_tokens tier suffix applying to n_tokens, or ''.
tier_rate
def tier_rate(
meta:dict, # Model metadata dict from `get_model_info`
key:str, # Base cost key, e.g. 'input_cost_per_token'
tier:str='', # Context-tier suffix from `price_tier`, e.g. '_above_272k_tokens'
default:float=0, # Fallback rate when neither tiered nor base `key` is present
)->float:Look up key at tier, falling back to the untiered key, then default.
calc_cost
def calc_cost(
usage:fastdecisions.utils.Usage, # Normalized token usage
meta:dict, # Model pricing metadata from `get_model_info`
)->float:Compute USD cost from usage and meta across tiered uncached, cached, cache-write, and output rates.
ContextWindowExceededError
def ContextWindowExceededError(
message:str, *, provider:str='', model:str='', endpoint:str='', status_code=None, error_type:str='', code=None,
request_id:str='', retryable=None, raw=None, response=None
):Raised when a provider rejects a request for exceeding its token limit.
load_spec
def load_spec(
spec_dict:dict
)->fastspec.spec.SpecParser:Construct a SpecParser offline from a distilled to_dict() specification.
load_bundled_spec
def load_bundled_spec(
name:str
)->fastspec.spec.SpecParser:Load a bundled distilled spec from fastdecisions/specs/{name}.json offline.