Catalogs and token counts

protoverta.fetch_catalog(base_url, headers, policy=None, *, upstream_open=None)[source]

Fetch and validate a provider’s complete model catalog.

Redirects are refused so credentials never reach another host. Pages, models and bytes are bounded by policy; returns a Catalog (models sorted by ID) or raises CatalogError.

Parameters:
Return type:

Catalog

class protoverta.Catalog(models=(), skipped=0, dropped_fields=0)[source]

Bases: tuple[ModelInfo, …]

Models from fetch_catalog(), sorted by ID.

A plain tuple of ModelInfo that also reports what "skip" mode left out: skipped entries and dropped_fields of kept models.

Parameters:
Return type:

Self

class protoverta.CatalogPolicy(pagination='none', max_pages=100, max_models=10000, max_bytes=8388608, timeout=30.0, path='/models', name_field='display_name', context_field=None, output_field=None, modalities_field=None, collection_field='data', id_field='id', query_params=(), page_param='page', total_pages_field='pagination.total_pages', first_page=1, current_page_field=None, invalid_entries='reject')[source]

Bases: object

How to read one provider’s model catalog: path, pagination, field names and size limits.

Defaults match the OpenAI-style GET /models list. Token limits are only read when context_field/output_field name the provider’s fields.

Parameters:
  • pagination (str)

  • max_pages (int)

  • max_models (int)

  • max_bytes (int)

  • timeout (float)

  • path (str)

  • name_field (str)

  • context_field (str | None)

  • output_field (str | None)

  • modalities_field (str | None)

  • collection_field (str | None)

  • id_field (str)

  • query_params (tuple[tuple[str, str], ...])

  • page_param (str)

  • total_pages_field (str)

  • first_page (int)

  • current_page_field (str | None)

  • invalid_entries (str)

invalid_entries: str = 'reject'

"reject" fails the whole catalog on one invalid entry. "skip" drops entries without a valid ID and ignores malformed optional metadata (name, token limits, modalities) of otherwise valid models.

class protoverta.ModelInfo(id, display_name, context_window_tokens=None, max_output_tokens=None, input_modalities=(), capability_status='unknown')[source]

Bases: object

One model from a provider catalog, with the limits and input modalities the provider itself reported.

Parameters:
  • id (str)

  • display_name (str)

  • context_window_tokens (int | None)

  • max_output_tokens (int | None)

  • input_modalities (tuple[str, ...])

  • capability_status (str)

exception protoverta.CatalogError[source]

Bases: ValueError

A provider model catalog could not be fetched or failed validation.

protoverta.count_input(config, body, source, *, upstream_open=None, budget=None)[source]

Return explicit count and provenance; unsupported media is never guessed.

Parameters:
Return type:

tuple[int, str]

class protoverta.NativeTokenCounter(config, *, upstream_open=None)[source]

Bases: object

Count input tokens with the provider’s own endpoint (Messages count_tokens or Responses input_tokens); never an estimate.

Parameters:
  • config (GatewayConfig)

  • upstream_open (Callable[..., Any] | None)

class protoverta.ModelTokenCounter(model, protocol, *, render_request, encode, exact=False)[source]

Bases: object

Explicit local tokenizer/template plugin bound to one exact model/protocol.

The operator supplies a complete input renderer and encoder, including tool templates. Nothing is downloaded/imported dynamically. Exactness is asserted by that operator, never inferred from a tokenizer family or model spelling.

Parameters:
protoverta.assess_context(body, protocol, limit, *, policy='estimate', counter=None)[source]

Byte-count heuristic is a labelled proxy, NOT a guaranteed upper bound.

Exact operator callbacks must cover the complete model-specific input, including tools/system/history/multimodal overhead. They are trusted code; this library never calls a count endpoint or downloads a tokenizer by default.

Parameters:
Return type:

ContextAssessment

class protoverta.ContextAssessment(input_tokens, reserved_output_tokens, context_window_tokens, exact, verdict, count_kind='utf8-json-byte-estimate')[source]

Bases: object

The result of a context-window preflight: counted input, reserved output, the model’s window and a verdict.

headers() renders it as X-Protoverta-Context-* response headers.

Parameters:
  • input_tokens (int | None)

  • reserved_output_tokens (int | None)

  • context_window_tokens (int | None)

  • exact (bool)

  • verdict (str)

  • count_kind (str)

class protoverta.TokenCount(tokens, exact=False)[source]

Bases: object

A token count and whether it is exact (an operator or provider count) rather than an estimate.

Parameters: